Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,29 +14,41 @@ def encode_image(image):
|
|
14 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
15 |
|
16 |
def chat_with_image(message, image, history):
|
17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if image is not None:
|
19 |
encoded_image = encode_image(Image.open(image))
|
20 |
-
|
21 |
"role": "user",
|
22 |
"content": [
|
23 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
|
24 |
{"type": "text", "text": message}
|
25 |
]
|
26 |
-
}
|
27 |
else:
|
28 |
-
|
29 |
-
|
30 |
-
# Prepare the messages
|
31 |
-
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
32 |
-
for human, assistant in history:
|
33 |
-
messages.append({"role": "user", "content": human})
|
34 |
-
messages.append({"role": "assistant", "content": assistant})
|
35 |
-
messages.append(image_message)
|
36 |
|
37 |
# Call the Together AI API
|
38 |
response = client.chat.completions.create(
|
39 |
-
model="meta-llama/Llama-Vision-
|
40 |
messages=messages,
|
41 |
max_tokens=512,
|
42 |
temperature=0.7,
|
@@ -64,12 +76,18 @@ with gr.Blocks() as demo:
|
|
64 |
clear = gr.Button("Clear")
|
65 |
|
66 |
def user(user_message, image, history):
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
def bot(history):
|
70 |
-
user_message
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
73 |
bot_message = chat_with_image(user_message, image, history[:-1])
|
74 |
history[-1][1] = ""
|
75 |
for character in bot_message:
|
|
|
14 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
15 |
|
16 |
def chat_with_image(message, image, history):
|
17 |
+
# Prepare the messages
|
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, dict) and 'image' in human:
|
22 |
+
# This is an image message
|
23 |
+
encoded_image = encode_image(Image.open(human['image']))
|
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['text']}
|
29 |
+
]
|
30 |
+
})
|
31 |
+
else:
|
32 |
+
# This is a text-only message
|
33 |
+
messages.append({"role": "user", "content": human})
|
34 |
+
messages.append({"role": "assistant", "content": assistant})
|
35 |
+
|
36 |
+
# Add the current message
|
37 |
if image is not None:
|
38 |
encoded_image = encode_image(Image.open(image))
|
39 |
+
messages.append({
|
40 |
"role": "user",
|
41 |
"content": [
|
42 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
|
43 |
{"type": "text", "text": message}
|
44 |
]
|
45 |
+
})
|
46 |
else:
|
47 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Call the Together AI API
|
50 |
response = client.chat.completions.create(
|
51 |
+
model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",
|
52 |
messages=messages,
|
53 |
max_tokens=512,
|
54 |
temperature=0.7,
|
|
|
76 |
clear = gr.Button("Clear")
|
77 |
|
78 |
def user(user_message, image, history):
|
79 |
+
if image is not None:
|
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:
|