Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -39,7 +39,7 @@ tokenizer = AutoTokenizer.from_pretrained("ManishThota/Sparrow", trust_remote_co
|
|
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)
|
@@ -50,20 +50,22 @@ def predict_answer(image, question):
|
|
50 |
#Generate the answer
|
51 |
output_ids = model.generate(
|
52 |
input_ids,
|
53 |
-
max_new_tokens=
|
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)
|
61 |
return answer
|
62 |
|
63 |
# Define the Gradio interface
|
64 |
iface = gr.Interface(
|
65 |
fn=gradio_predict,
|
66 |
-
inputs=[gr.Image(type="pil", label="Upload or Drag an Image"),
|
|
|
|
|
67 |
outputs=gr.TextArea(label="Answer"),
|
68 |
title="Sparrow-based Visual Question Answering",
|
69 |
description="An interactive chat model that can answer questions about images.",
|
|
|
39 |
|
40 |
# return generated_text
|
41 |
|
42 |
+
def predict_answer(image, question, max_tokens):
|
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)
|
|
|
50 |
#Generate the answer
|
51 |
output_ids = model.generate(
|
52 |
input_ids,
|
53 |
+
max_new_tokens=max_tokens,
|
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, max_tokens=25):
|
60 |
+
answer = predict_answer(image, question, max_tokens)
|
61 |
return answer
|
62 |
|
63 |
# Define the Gradio interface
|
64 |
iface = gr.Interface(
|
65 |
fn=gradio_predict,
|
66 |
+
inputs=[gr.Image(type="pil", label="Upload or Drag an Image"),
|
67 |
+
gr.Textbox(label="Question", placeholder="e.g. What are the colors of the bus in the image?", scale=4),
|
68 |
+
gr.Slider(minimum=1, maximum=100, default=25, label="Max Number of Tokens")],
|
69 |
outputs=gr.TextArea(label="Answer"),
|
70 |
title="Sparrow-based Visual Question Answering",
|
71 |
description="An interactive chat model that can answer questions about images.",
|