Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,65 +8,78 @@ import base64
|
|
8 |
# Initialize the Together AI client
|
9 |
client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))
|
10 |
|
11 |
-
def encode_image(
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
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
|
22 |
# This is an image message
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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": message}
|
29 |
]
|
30 |
})
|
31 |
else:
|
32 |
-
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
|
71 |
# Create the Gradio interface
|
72 |
with gr.Blocks() as demo:
|
@@ -76,17 +89,17 @@ with gr.Blocks() as demo:
|
|
76 |
clear = gr.Button("Clear")
|
77 |
|
78 |
def user(user_message, image, history):
|
79 |
-
if image
|
80 |
-
return "", None, history + [["Image
|
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 user_message.startswith("Image
|
88 |
-
image =
|
89 |
-
user_message =
|
90 |
|
91 |
bot_message = chat_with_image(user_message, image, history[:-1])
|
92 |
history[-1][1] = ""
|
|
|
8 |
# Initialize the Together AI client
|
9 |
client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))
|
10 |
|
11 |
+
def encode_image(image_path):
|
12 |
+
try:
|
13 |
+
with Image.open(image_path) as img:
|
14 |
+
buffered = io.BytesIO()
|
15 |
+
img.save(buffered, format="PNG")
|
16 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error encoding image: {e}")
|
19 |
+
return None
|
20 |
|
21 |
def chat_with_image(message, image, history):
|
22 |
# Prepare the messages
|
23 |
messages = [{"role": "system", "content": "You are a helpful assistant that can analyze images and text."}]
|
24 |
|
25 |
for human, assistant in history:
|
26 |
+
if human.startswith("Image: "):
|
27 |
# This is an image message
|
28 |
+
image_path = human.split(": ", 1)[1]
|
29 |
+
encoded_image = encode_image(image_path)
|
30 |
+
if encoded_image:
|
31 |
+
messages.append({
|
32 |
+
"role": "user",
|
33 |
+
"content": [
|
34 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
|
35 |
+
{"type": "text", "text": "What's in this image?"}
|
36 |
+
]
|
37 |
+
})
|
38 |
+
else:
|
39 |
+
messages.append({"role": "user", "content": "I tried to upload an image, but there was an error."})
|
40 |
+
else:
|
41 |
+
# This is a text-only message
|
42 |
+
messages.append({"role": "user", "content": human})
|
43 |
+
messages.append({"role": "assistant", "content": assistant})
|
44 |
+
|
45 |
+
# Add the current message
|
46 |
+
if image:
|
47 |
+
encoded_image = encode_image(image)
|
48 |
+
if encoded_image:
|
49 |
messages.append({
|
50 |
"role": "user",
|
51 |
"content": [
|
52 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
|
53 |
+
{"type": "text", "text": message or "What's in this image?"}
|
54 |
]
|
55 |
})
|
56 |
else:
|
57 |
+
messages.append({"role": "user", "content": "I tried to upload an image, but there was an error."})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
else:
|
59 |
messages.append({"role": "user", "content": message})
|
60 |
|
61 |
# Call the Together AI API
|
62 |
+
try:
|
63 |
+
response = client.chat.completions.create(
|
64 |
+
model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",
|
65 |
+
messages=messages,
|
66 |
+
max_tokens=512,
|
67 |
+
temperature=0.7,
|
68 |
+
top_p=0.7,
|
69 |
+
top_k=50,
|
70 |
+
repetition_penalty=1,
|
71 |
+
stop=["<|eot_id|>", "<|eom_id|>"],
|
72 |
+
stream=True
|
73 |
+
)
|
74 |
+
|
75 |
+
# Accumulate the response
|
76 |
+
full_response = ""
|
77 |
+
for chunk in response:
|
78 |
+
if chunk.choices[0].delta.content is not None:
|
79 |
+
full_response += chunk.choices[0].delta.content
|
80 |
+
yield full_response
|
81 |
+
except Exception as e:
|
82 |
+
yield f"An error occurred: {str(e)}"
|
83 |
|
84 |
# Create the Gradio interface
|
85 |
with gr.Blocks() as demo:
|
|
|
89 |
clear = gr.Button("Clear")
|
90 |
|
91 |
def user(user_message, image, history):
|
92 |
+
if image:
|
93 |
+
return "", None, history + [[f"Image: {image}", None]]
|
94 |
else:
|
95 |
return "", None, history + [[user_message, None]]
|
96 |
|
97 |
def bot(history):
|
98 |
user_message = history[-1][0]
|
99 |
image = None
|
100 |
+
if user_message.startswith("Image: "):
|
101 |
+
image = user_message.split(": ", 1)[1]
|
102 |
+
user_message = "What's in this image?"
|
103 |
|
104 |
bot_message = chat_with_image(user_message, image, history[:-1])
|
105 |
history[-1][1] = ""
|