Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
|
3 |
import os
|
4 |
import re
|
|
|
5 |
import tempfile
|
6 |
from collections.abc import Iterator
|
7 |
from threading import Thread
|
@@ -149,9 +150,11 @@ def process_video(video_path: str) -> list[dict]:
|
|
149 |
|
150 |
|
151 |
def encode_image_to_base64(image_path):
|
152 |
-
import
|
|
|
153 |
with open(image_path, "rb") as image_file:
|
154 |
-
|
|
|
155 |
|
156 |
|
157 |
def process_interleaved_images(message: dict) -> list:
|
@@ -163,24 +166,21 @@ def process_interleaved_images(message: dict) -> list:
|
|
163 |
|
164 |
for part in parts:
|
165 |
if part == "<image>":
|
166 |
-
# If we have accumulated text, add it first
|
167 |
if current_text.strip():
|
168 |
final_content.append({"type": "text", "text": current_text.strip()})
|
169 |
current_text = ""
|
170 |
-
|
171 |
-
# Add the image
|
172 |
final_content.append({
|
173 |
"type": "image_url",
|
174 |
-
"image_url": {"url":
|
175 |
})
|
176 |
image_index += 1
|
177 |
else:
|
178 |
current_text += part
|
179 |
-
|
180 |
-
# Add any remaining text
|
181 |
if current_text.strip():
|
182 |
final_content.append({"type": "text", "text": current_text.strip()})
|
183 |
-
|
184 |
return final_content
|
185 |
|
186 |
|
@@ -189,27 +189,53 @@ def process_new_user_message(message: dict):
|
|
189 |
return [{"role": "user", "content": message["text"]}]
|
190 |
|
191 |
if message["files"][0].endswith(".mp4"):
|
192 |
-
# For video, return text message followed by frame messages
|
193 |
text_message = {"role": "user", "content": message["text"]}
|
194 |
video_messages = process_video(message["files"][0])
|
195 |
return [text_message] + video_messages
|
196 |
|
197 |
if "<image>" in message["text"]:
|
198 |
-
# For interleaved text and images
|
199 |
content = process_interleaved_images(message)
|
200 |
return [{"role": "user", "content": content}]
|
201 |
|
202 |
# For text with images appended
|
203 |
content = [{"type": "text", "text": message["text"]}]
|
204 |
for path in message["files"]:
|
|
|
205 |
content.append({
|
206 |
"type": "image_url",
|
207 |
-
"image_url": {"url":
|
208 |
})
|
209 |
|
210 |
return [{"role": "user", "content": content}]
|
211 |
|
212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
def process_history(history: list[dict]) -> list[dict]:
|
214 |
messages = []
|
215 |
|
|
|
2 |
|
3 |
import os
|
4 |
import re
|
5 |
+
import base64
|
6 |
import tempfile
|
7 |
from collections.abc import Iterator
|
8 |
from threading import Thread
|
|
|
150 |
|
151 |
|
152 |
def encode_image_to_base64(image_path):
|
153 |
+
import mimetypes
|
154 |
+
mime_type, _ = mimetypes.guess_type(image_path)
|
155 |
with open(image_path, "rb") as image_file:
|
156 |
+
encoded = base64.b64encode(image_file.read()).decode("utf-8")
|
157 |
+
return f"data:{mime_type};base64,{encoded}"
|
158 |
|
159 |
|
160 |
def process_interleaved_images(message: dict) -> list:
|
|
|
166 |
|
167 |
for part in parts:
|
168 |
if part == "<image>":
|
|
|
169 |
if current_text.strip():
|
170 |
final_content.append({"type": "text", "text": current_text.strip()})
|
171 |
current_text = ""
|
172 |
+
encoded_image = encode_image_to_base64(message["files"][image_index])
|
|
|
173 |
final_content.append({
|
174 |
"type": "image_url",
|
175 |
+
"image_url": {"url": encoded_image}
|
176 |
})
|
177 |
image_index += 1
|
178 |
else:
|
179 |
current_text += part
|
180 |
+
|
|
|
181 |
if current_text.strip():
|
182 |
final_content.append({"type": "text", "text": current_text.strip()})
|
183 |
+
|
184 |
return final_content
|
185 |
|
186 |
|
|
|
189 |
return [{"role": "user", "content": message["text"]}]
|
190 |
|
191 |
if message["files"][0].endswith(".mp4"):
|
|
|
192 |
text_message = {"role": "user", "content": message["text"]}
|
193 |
video_messages = process_video(message["files"][0])
|
194 |
return [text_message] + video_messages
|
195 |
|
196 |
if "<image>" in message["text"]:
|
|
|
197 |
content = process_interleaved_images(message)
|
198 |
return [{"role": "user", "content": content}]
|
199 |
|
200 |
# For text with images appended
|
201 |
content = [{"type": "text", "text": message["text"]}]
|
202 |
for path in message["files"]:
|
203 |
+
encoded_image = encode_image_to_base64(path)
|
204 |
content.append({
|
205 |
"type": "image_url",
|
206 |
+
"image_url": {"url": encoded_image}
|
207 |
})
|
208 |
|
209 |
return [{"role": "user", "content": content}]
|
210 |
|
211 |
|
212 |
+
def process_history(history: list[dict]) -> list[dict]:
|
213 |
+
messages = []
|
214 |
+
|
215 |
+
for item in history:
|
216 |
+
if item["role"] == "assistant":
|
217 |
+
messages.append({"role": "assistant", "content": item["content"]})
|
218 |
+
else:
|
219 |
+
content = item["content"]
|
220 |
+
if isinstance(content, str):
|
221 |
+
messages.append({"role": "user", "content": content})
|
222 |
+
else:
|
223 |
+
# Assume content[0] is a file path
|
224 |
+
encoded_image = encode_image_to_base64(content[0])
|
225 |
+
messages.append({
|
226 |
+
"role": "user",
|
227 |
+
"content": [
|
228 |
+
{
|
229 |
+
"type": "image_url",
|
230 |
+
"image_url": {"url": encoded_image}
|
231 |
+
}
|
232 |
+
]
|
233 |
+
})
|
234 |
+
|
235 |
+
return messages
|
236 |
+
|
237 |
+
|
238 |
+
|
239 |
def process_history(history: list[dict]) -> list[dict]:
|
240 |
messages = []
|
241 |
|