Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,31 +13,48 @@ model = AutoModelForCausalLM.from_pretrained("ManishThota/Sparrow", torch_dtype
|
|
| 13 |
trust_remote_code=True).to(device)
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained("ManishThota/Sparrow", trust_remote_code=True)
|
| 15 |
|
| 16 |
-
def predict_answer(image, question):
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
encoding = tokenizer(image, question, return_tensors='pt').to(device)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# input_ids,
|
| 33 |
-
# max_new_tokens=100,
|
| 34 |
-
# images=image_tensor,
|
| 35 |
-
# use_cache=True)[0]
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
return
|
| 41 |
|
| 42 |
def gradio_predict(image, question):
|
| 43 |
answer = predict_answer(image, question)
|
|
|
|
| 13 |
trust_remote_code=True).to(device)
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained("ManishThota/Sparrow", trust_remote_code=True)
|
| 15 |
|
| 16 |
+
# def predict_answer(image, question):
|
| 17 |
+
# # Convert PIL image to RGB if not already
|
| 18 |
+
# image = image.convert("RGB")
|
| 19 |
+
|
| 20 |
+
# # # Format the text input for the model
|
| 21 |
+
# # text = f"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\n{question} ASSISTANT:"
|
| 22 |
+
|
| 23 |
+
# # Tokenize the text input
|
| 24 |
+
# encoding = tokenizer(image, question, return_tensors='pt').to(device)
|
| 25 |
+
|
| 26 |
+
# out = model.generate(**encoding)
|
| 27 |
+
# # Preprocess the image for the model
|
| 28 |
+
# generated_text = tokenizer.decode(out[0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
# # # Generate the answer
|
| 31 |
+
# # output_ids = model.generate(
|
| 32 |
+
# # input_ids,
|
| 33 |
+
# # max_new_tokens=100,
|
| 34 |
+
# # images=image_tensor,
|
| 35 |
+
# # use_cache=True)[0]
|
| 36 |
|
| 37 |
+
# # # Decode the generated tokens to get the answer
|
| 38 |
+
# # answer = tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
| 39 |
|
| 40 |
+
# return generated_text
|
|
|
|
| 41 |
|
| 42 |
+
def predict_answer(image, question):
|
| 43 |
+
#Set inputs
|
| 44 |
+
text = f"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\n{question}? ASSISTANT:"
|
| 45 |
+
image = Image.open(image)
|
| 46 |
|
| 47 |
+
input_ids = tokenizer(text, return_tensors='pt').input_ids
|
| 48 |
+
image_tensor = model.image_preprocess(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
#Generate the answer
|
| 51 |
+
output_ids = model.generate(
|
| 52 |
+
input_ids,
|
| 53 |
+
max_new_tokens=25,
|
| 54 |
+
images=image_tensor,
|
| 55 |
+
use_cache=True)[0]
|
| 56 |
|
| 57 |
+
return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
| 58 |
|
| 59 |
def gradio_predict(image, question):
|
| 60 |
answer = predict_answer(image, question)
|