Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,54 @@ def encode_image(image_path):
|
|
28 |
print(f"Error encoding image: {e}")
|
29 |
raise e
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def bot_streaming(message, history, together_api_key, max_new_tokens=250, temperature=0.7):
|
32 |
# Initialize history if it's None
|
33 |
if history is None:
|
@@ -76,7 +124,7 @@ def bot_streaming(message, history, together_api_key, max_new_tokens=250, temper
|
|
76 |
{
|
77 |
"type": "image_url",
|
78 |
"image_url": {
|
79 |
-
"url": f"data:image/png;base64,{
|
80 |
},
|
81 |
},
|
82 |
],
|
@@ -203,7 +251,7 @@ with gr.Blocks() as demo:
|
|
203 |
clear = gr.Button("Clear")
|
204 |
|
205 |
msg.submit(
|
206 |
-
|
207 |
inputs=[msg, chatbot, together_api_key, max_new_tokens, temperature],
|
208 |
outputs=chatbot
|
209 |
)
|
|
|
28 |
print(f"Error encoding image: {e}")
|
29 |
raise e
|
30 |
|
31 |
+
def old_bot_streaming(message, history, max_new_tokens=250, api_key=None, max_history=5):
|
32 |
+
if client is None:
|
33 |
+
initialize_client(api_key)
|
34 |
+
|
35 |
+
txt = message["text"]
|
36 |
+
messages = []
|
37 |
+
images = []
|
38 |
+
|
39 |
+
for i, msg in enumerate(history[-max_history:]):
|
40 |
+
if isinstance(msg[0], tuple):
|
41 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": history[i+1][0]}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encode_image(msg[0][0])}"}}]})
|
42 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": history[i+1][1]}]})
|
43 |
+
elif isinstance(history[i-1], tuple) and isinstance(msg[0], str):
|
44 |
+
pass
|
45 |
+
elif isinstance(history[i-1][0], str) and isinstance(msg[0], str):
|
46 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": msg[0]}]})
|
47 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": msg[1]}]})
|
48 |
+
|
49 |
+
if len(message["files"]) == 1:
|
50 |
+
if isinstance(message["files"][0], str): # examples
|
51 |
+
image_path = message["files"][0]
|
52 |
+
else: # regular input
|
53 |
+
image_path = message["files"][0]["path"]
|
54 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": txt}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encode_image(image_path)}"}}]})
|
55 |
+
else:
|
56 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": txt}]})
|
57 |
+
|
58 |
+
try:
|
59 |
+
stream = client.chat.completions.create(
|
60 |
+
model="meta-llama/Llama-Vision-Free",
|
61 |
+
messages=messages,
|
62 |
+
max_tokens=max_new_tokens,
|
63 |
+
stream=True,
|
64 |
+
)
|
65 |
+
|
66 |
+
buffer = ""
|
67 |
+
for chunk in stream:
|
68 |
+
if chunk.choices[0].delta.content is not None:
|
69 |
+
buffer += chunk.choices[0].delta.content
|
70 |
+
time.sleep(0.01)
|
71 |
+
yield buffer
|
72 |
+
|
73 |
+
except together.error.InvalidRequestError as e:
|
74 |
+
if "Request Entity Too Large" in str(e):
|
75 |
+
yield "The image is too large. Please try with a smaller image or compress the existing one."
|
76 |
+
else:
|
77 |
+
yield f"An error occurred: {str(e)}"
|
78 |
+
|
79 |
def bot_streaming(message, history, together_api_key, max_new_tokens=250, temperature=0.7):
|
80 |
# Initialize history if it's None
|
81 |
if history is None:
|
|
|
124 |
{
|
125 |
"type": "image_url",
|
126 |
"image_url": {
|
127 |
+
"url": f"data:image/png;base64,{encode_image(image_path)}",
|
128 |
},
|
129 |
},
|
130 |
],
|
|
|
251 |
clear = gr.Button("Clear")
|
252 |
|
253 |
msg.submit(
|
254 |
+
old_bot_streaming,
|
255 |
inputs=[msg, chatbot, together_api_key, max_new_tokens, temperature],
|
256 |
outputs=chatbot
|
257 |
)
|