Spaces:
Runtime error
Runtime error
hassanelmghari
commited on
Commit
•
c2ebbce
1
Parent(s):
26df791
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,6 @@ import requests
|
|
4 |
import os
|
5 |
from together import Together
|
6 |
import base64
|
7 |
-
from threading import Thread
|
8 |
-
import time
|
9 |
import io
|
10 |
|
11 |
# Initialize Together client
|
@@ -20,15 +18,10 @@ def initialize_client(api_key=None):
|
|
20 |
else:
|
21 |
raise ValueError("Please provide a Together API Key")
|
22 |
|
23 |
-
def encode_image(image_path
|
24 |
with Image.open(image_path) as img:
|
25 |
-
img.thumbnail(max_size)
|
26 |
-
if img.mode in ('RGBA', 'LA'):
|
27 |
-
background = Image.new(img.mode[:-1], img.size, (255, 255, 255))
|
28 |
-
background.paste(img, mask=img.split()[-1])
|
29 |
-
img = background
|
30 |
buffered = io.BytesIO()
|
31 |
-
img.save(buffered, format="
|
32 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
33 |
|
34 |
def bot_streaming(message, history, together_api_key, max_new_tokens=250, temperature=0.7):
|
@@ -45,18 +38,27 @@ def bot_streaming(message, history, together_api_key, max_new_tokens=250, temper
|
|
45 |
|
46 |
# Add history to messages
|
47 |
for user_msg, assistant_msg in history:
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Prepare the current message
|
52 |
current_message = {"role": "user", "content": []}
|
53 |
|
54 |
# Add text content
|
55 |
-
if message.get("text"):
|
56 |
current_message["content"].append({"type": "text", "text": message["text"]})
|
|
|
|
|
57 |
|
58 |
# Add image content if present
|
59 |
-
if message.get("files") and len(message["files"]) > 0:
|
60 |
image_path = message["files"][0]["path"] if isinstance(message["files"][0], dict) else message["files"][0]
|
61 |
image_base64 = encode_image(image_path)
|
62 |
current_message["content"].append({
|
@@ -90,6 +92,7 @@ def bot_streaming(message, history, together_api_key, max_new_tokens=250, temper
|
|
90 |
else:
|
91 |
yield f"An error occurred: {str(e)}"
|
92 |
|
|
|
93 |
with gr.Blocks() as demo:
|
94 |
gr.Markdown("# Meta Llama-3.2-11B-Vision-Instruct (FREE)")
|
95 |
gr.Markdown("Try the new Llama 3.2 11B Vision API by Meta for free through Together AI. Upload an image, and start chatting about it. Just paste in your Together AI API key and get started!")
|
@@ -97,7 +100,7 @@ with gr.Blocks() as demo:
|
|
97 |
with gr.Row():
|
98 |
together_api_key = gr.Textbox(
|
99 |
label="Together API Key",
|
100 |
-
placeholder="Enter your
|
101 |
type="password"
|
102 |
)
|
103 |
|
|
|
4 |
import os
|
5 |
from together import Together
|
6 |
import base64
|
|
|
|
|
7 |
import io
|
8 |
|
9 |
# Initialize Together client
|
|
|
18 |
else:
|
19 |
raise ValueError("Please provide a Together API Key")
|
20 |
|
21 |
+
def encode_image(image_path):
|
22 |
with Image.open(image_path) as img:
|
|
|
|
|
|
|
|
|
|
|
23 |
buffered = io.BytesIO()
|
24 |
+
img.save(buffered, format="PNG")
|
25 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
26 |
|
27 |
def bot_streaming(message, history, together_api_key, max_new_tokens=250, temperature=0.7):
|
|
|
38 |
|
39 |
# Add history to messages
|
40 |
for user_msg, assistant_msg in history:
|
41 |
+
if isinstance(user_msg, tuple): # Handle image messages
|
42 |
+
text = user_msg[1] if len(user_msg) > 1 else ""
|
43 |
+
messages.append({"role": "user", "content": [
|
44 |
+
{"type": "text", "text": text},
|
45 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encode_image(user_msg[0])}"}}
|
46 |
+
]})
|
47 |
+
else:
|
48 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": user_msg}]})
|
49 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": assistant_msg}]})
|
50 |
|
51 |
# Prepare the current message
|
52 |
current_message = {"role": "user", "content": []}
|
53 |
|
54 |
# Add text content
|
55 |
+
if isinstance(message, dict) and message.get("text"):
|
56 |
current_message["content"].append({"type": "text", "text": message["text"]})
|
57 |
+
elif isinstance(message, str):
|
58 |
+
current_message["content"].append({"type": "text", "text": message})
|
59 |
|
60 |
# Add image content if present
|
61 |
+
if isinstance(message, dict) and message.get("files") and len(message["files"]) > 0:
|
62 |
image_path = message["files"][0]["path"] if isinstance(message["files"][0], dict) else message["files"][0]
|
63 |
image_base64 = encode_image(image_path)
|
64 |
current_message["content"].append({
|
|
|
92 |
else:
|
93 |
yield f"An error occurred: {str(e)}"
|
94 |
|
95 |
+
# The rest of your Gradio interface code remains the same
|
96 |
with gr.Blocks() as demo:
|
97 |
gr.Markdown("# Meta Llama-3.2-11B-Vision-Instruct (FREE)")
|
98 |
gr.Markdown("Try the new Llama 3.2 11B Vision API by Meta for free through Together AI. Upload an image, and start chatting about it. Just paste in your Together AI API key and get started!")
|
|
|
100 |
with gr.Row():
|
101 |
together_api_key = gr.Textbox(
|
102 |
label="Together API Key",
|
103 |
+
placeholder="Enter your TOGETHER_API_KEY here",
|
104 |
type="password"
|
105 |
)
|
106 |
|