Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,14 +18,14 @@ def chat_with_image(message, image, history):
|
|
| 18 |
messages = [{"role": "system", "content": "You are a helpful assistant that can analyze images and text."}]
|
| 19 |
|
| 20 |
for human, assistant in history:
|
| 21 |
-
if isinstance(human,
|
| 22 |
# This is an image message
|
| 23 |
-
encoded_image = encode_image(Image.open(human[
|
| 24 |
messages.append({
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
|
| 28 |
-
{"type": "text", "text": human[
|
| 29 |
]
|
| 30 |
})
|
| 31 |
else:
|
|
@@ -76,22 +76,14 @@ with gr.Blocks() as demo:
|
|
| 76 |
clear = gr.Button("Clear")
|
| 77 |
|
| 78 |
def user(user_message, image, history):
|
| 79 |
-
|
| 80 |
-
return "", None, history + [{"text": user_message, "image": image}, None]
|
| 81 |
-
else:
|
| 82 |
-
return "", None, history + [[user_message, None]]
|
| 83 |
|
| 84 |
def bot(history):
|
| 85 |
-
user_message = history[-1][0]
|
| 86 |
-
image = None
|
| 87 |
-
if isinstance(user_message, dict):
|
| 88 |
-
image = user_message['image']
|
| 89 |
-
user_message = user_message['text']
|
| 90 |
-
|
| 91 |
bot_message = chat_with_image(user_message, image, history[:-1])
|
| 92 |
-
history[-1][1]
|
| 93 |
for character in bot_message:
|
| 94 |
-
history[-1][1]
|
| 95 |
yield history
|
| 96 |
|
| 97 |
msg.submit(user, [msg, image, chatbot], [msg, image, chatbot], queue=False).then(
|
|
|
|
| 18 |
messages = [{"role": "system", "content": "You are a helpful assistant that can analyze images and text."}]
|
| 19 |
|
| 20 |
for human, assistant in history:
|
| 21 |
+
if isinstance(human, tuple) and human[1] is not None:
|
| 22 |
# This is an image message
|
| 23 |
+
encoded_image = encode_image(Image.open(human[1]))
|
| 24 |
messages.append({
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
|
| 28 |
+
{"type": "text", "text": human[0]}
|
| 29 |
]
|
| 30 |
})
|
| 31 |
else:
|
|
|
|
| 76 |
clear = gr.Button("Clear")
|
| 77 |
|
| 78 |
def user(user_message, image, history):
|
| 79 |
+
return "", None, history + [((user_message, image), None)]
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
def bot(history):
|
| 82 |
+
user_message, image = history[-1][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
bot_message = chat_with_image(user_message, image, history[:-1])
|
| 84 |
+
history[-1] = (history[-1][0], "")
|
| 85 |
for character in bot_message:
|
| 86 |
+
history[-1] = (history[-1][0], history[-1][1] + character)
|
| 87 |
yield history
|
| 88 |
|
| 89 |
msg.submit(user, [msg, image, chatbot], [msg, image, chatbot], queue=False).then(
|