Spaces:
Runtime error
Runtime error
hassanelmghari
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -31,83 +31,112 @@ def encode_image(image_path, max_size=(800, 800), quality=85):
|
|
31 |
img.save(buffered, format="JPEG", quality=quality)
|
32 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
33 |
|
34 |
-
def bot_streaming(message, history, together_api_key, max_new_tokens=250, max_history=5):
|
35 |
if client is None:
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
txt = message
|
39 |
messages = []
|
40 |
images = []
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
try:
|
62 |
stream = client.chat.completions.create(
|
63 |
model="meta-llama/Llama-Vision-Free",
|
64 |
messages=messages,
|
65 |
max_tokens=max_new_tokens,
|
|
|
66 |
stream=True,
|
67 |
)
|
68 |
|
69 |
buffer = ""
|
70 |
for chunk in stream:
|
71 |
-
if chunk.choices[0].delta.content is not None:
|
72 |
buffer += chunk.choices[0].delta.content
|
73 |
time.sleep(0.01)
|
74 |
yield buffer
|
75 |
|
|
|
|
|
|
|
76 |
except Exception as e:
|
77 |
if "Request Entity Too Large" in str(e):
|
78 |
yield "The image is too large. Please try with a smaller image or compress the existing one."
|
79 |
else:
|
80 |
yield f"An error occurred: {str(e)}"
|
81 |
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
gr.Markdown("# Meta Llama-3.2-11B-Vision-Instruct (FREE)")
|
84 |
-
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
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
multimodal=True
|
108 |
-
)
|
109 |
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
demo.launch(debug=True)
|
|
|
31 |
img.save(buffered, format="JPEG", quality=quality)
|
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, max_history=5):
|
35 |
if client is None:
|
36 |
+
try:
|
37 |
+
initialize_client(together_api_key)
|
38 |
+
except Exception as e:
|
39 |
+
yield f"Error initializing client: {str(e)}"
|
40 |
+
return
|
41 |
|
42 |
+
txt = message.get("text", "")
|
43 |
messages = []
|
44 |
images = []
|
45 |
|
46 |
+
try:
|
47 |
+
for i, msg in enumerate(history[-max_history:]):
|
48 |
+
if isinstance(msg[0], tuple):
|
49 |
+
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])}"}}]})
|
50 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": history[i+1][1]}]})
|
51 |
+
elif isinstance(history[i-1][0], tuple) and isinstance(msg[0], str):
|
52 |
+
pass
|
53 |
+
elif isinstance(history[i-1][0], str) and isinstance(msg[0], str):
|
54 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": msg[0]}]})
|
55 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": msg[1]}]})
|
56 |
|
57 |
+
if "files" in message and len(message["files"]) == 1:
|
58 |
+
if isinstance(message["files"][0], str): # examples
|
59 |
+
image_path = message["files"][0]
|
60 |
+
else: # regular input
|
61 |
+
image_path = message["files"][0]["path"]
|
62 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": txt}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encode_image(image_path)}"}}]})
|
63 |
+
else:
|
64 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": txt}]})
|
65 |
|
|
|
66 |
stream = client.chat.completions.create(
|
67 |
model="meta-llama/Llama-Vision-Free",
|
68 |
messages=messages,
|
69 |
max_tokens=max_new_tokens,
|
70 |
+
temperature=temperature,
|
71 |
stream=True,
|
72 |
)
|
73 |
|
74 |
buffer = ""
|
75 |
for chunk in stream:
|
76 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content is not None:
|
77 |
buffer += chunk.choices[0].delta.content
|
78 |
time.sleep(0.01)
|
79 |
yield buffer
|
80 |
|
81 |
+
if not buffer:
|
82 |
+
yield "No response generated. Please try again."
|
83 |
+
|
84 |
except Exception as e:
|
85 |
if "Request Entity Too Large" in str(e):
|
86 |
yield "The image is too large. Please try with a smaller image or compress the existing one."
|
87 |
else:
|
88 |
yield f"An error occurred: {str(e)}"
|
89 |
|
90 |
+
css = """
|
91 |
+
#chatbot-container {
|
92 |
+
height: calc(100vh - 230px);
|
93 |
+
overflow-y: auto;
|
94 |
+
}
|
95 |
+
#chatbot-textbox {
|
96 |
+
position: fixed;
|
97 |
+
bottom: 20px;
|
98 |
+
left: 20px;
|
99 |
+
right: 20px;
|
100 |
+
}
|
101 |
+
"""
|
102 |
+
|
103 |
+
with gr.Blocks(css=css) as demo:
|
104 |
gr.Markdown("# Meta Llama-3.2-11B-Vision-Instruct (FREE)")
|
105 |
+
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!")
|
106 |
|
107 |
+
with gr.Row():
|
108 |
+
together_api_key = gr.Textbox(
|
109 |
+
label="Together API Key",
|
110 |
+
placeholder="Enter your TOGETHER_API_KEY here",
|
111 |
+
type="password"
|
112 |
+
)
|
113 |
|
114 |
+
with gr.Row():
|
115 |
+
max_new_tokens = gr.Slider(
|
116 |
+
minimum=10,
|
117 |
+
maximum=500,
|
118 |
+
value=250,
|
119 |
+
step=10,
|
120 |
+
label="Maximum number of new tokens",
|
121 |
+
)
|
122 |
+
temperature = gr.Number(
|
123 |
+
value=0.7,
|
124 |
+
minimum=0,
|
125 |
+
maximum=1,
|
126 |
+
step=0.1,
|
127 |
+
label="Temperature"
|
128 |
+
)
|
|
|
|
|
129 |
|
130 |
+
with gr.Column(elem_id="chatbot-container"):
|
131 |
+
chatbot = gr.ChatInterface(
|
132 |
+
fn=bot_streaming,
|
133 |
+
textbox=gr.MultimodalTextbox(elem_id="chatbot-textbox"),
|
134 |
+
additional_inputs=[together_api_key, max_new_tokens, temperature],
|
135 |
+
cache_examples=False,
|
136 |
+
stop_btn="Stop Generation",
|
137 |
+
fill_height=True,
|
138 |
+
multimodal=True
|
139 |
+
)
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
demo.launch(debug=True)
|