fixing double image triggers
Browse files- app.py +14 -6
- requirements.txt +1 -1
app.py
CHANGED
@@ -51,17 +51,22 @@ from transformers import pipeline
|
|
51 |
captioner = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
|
52 |
|
53 |
import PIL
|
54 |
-
import numpy
|
55 |
# an image has been selected. it comes to this fn as a numpy ndarray
|
56 |
# convert it to a PIL image and feed to the captioner
|
57 |
# return the resulting caption
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
if img.any():
|
61 |
im = PIL.Image.fromarray(img)
|
62 |
caption = captioner(im, max_new_tokens=20)
|
63 |
result = caption[0]['generated_text']
|
64 |
-
|
|
|
65 |
|
66 |
# class wrapping the chat
|
67 |
class ChatWrapper:
|
@@ -184,11 +189,14 @@ with block:
|
|
184 |
#submit.click(chat, inputs=[openai_api_key_textbox, result_box, state, agent_state], outputs=[caption_box, state])
|
185 |
#result_box.submit(chat, inputs=[openai_api_key_textbox, result_box, state, agent_state], outputs=[caption_box, state])
|
186 |
|
|
|
|
|
|
|
187 |
# if image has changed, feed it to "image_supplied", and pass result to "result_box"
|
188 |
image_box.change(
|
189 |
image_supplied,
|
190 |
-
inputs=[image_box],
|
191 |
-
outputs=[result_box]
|
192 |
)
|
193 |
|
194 |
# if api key in input box has changed, update the key in app
|
|
|
51 |
captioner = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
|
52 |
|
53 |
import PIL
|
|
|
54 |
# an image has been selected. it comes to this fn as a numpy ndarray
|
55 |
# convert it to a PIL image and feed to the captioner
|
56 |
# return the resulting caption
|
57 |
+
#
|
58 |
+
# NOTE: due to an error in gradio, this fn is triggered twice for each img change
|
59 |
+
# only process the first fn call, and keep the first caption result
|
60 |
+
async def image_supplied(img, count: int, last_cap: str):
|
61 |
+
if img is None: return "", count, ""
|
62 |
+
count += 1
|
63 |
+
if (count & 1) == 0: return last_cap, count, last_cap
|
64 |
if img.any():
|
65 |
im = PIL.Image.fromarray(img)
|
66 |
caption = captioner(im, max_new_tokens=20)
|
67 |
result = caption[0]['generated_text']
|
68 |
+
#print(f"caption={result}")
|
69 |
+
return result, count, result
|
70 |
|
71 |
# class wrapping the chat
|
72 |
class ChatWrapper:
|
|
|
189 |
#submit.click(chat, inputs=[openai_api_key_textbox, result_box, state, agent_state], outputs=[caption_box, state])
|
190 |
#result_box.submit(chat, inputs=[openai_api_key_textbox, result_box, state, agent_state], outputs=[caption_box, state])
|
191 |
|
192 |
+
count = gr.State(value=0)
|
193 |
+
last_cap = gr.State(value="")
|
194 |
+
|
195 |
# if image has changed, feed it to "image_supplied", and pass result to "result_box"
|
196 |
image_box.change(
|
197 |
image_supplied,
|
198 |
+
inputs=[image_box, count, last_cap],
|
199 |
+
outputs=[result_box, count, last_cap]
|
200 |
)
|
201 |
|
202 |
# if api key in input box has changed, update the key in app
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
openai
|
2 |
-
gradio
|
3 |
langchain
|
4 |
transformers
|
5 |
torch
|
|
|
1 |
openai
|
2 |
+
gradio>=3.35.2
|
3 |
langchain
|
4 |
transformers
|
5 |
torch
|