Update app.py
Browse files
app.py
CHANGED
@@ -226,11 +226,18 @@ def encode_image_to_base64(image_path):
|
|
226 |
elif file_extension == "webp":
|
227 |
mime_type = "image/webp"
|
228 |
return f"data:{mime_type};base64,{encoded_string}"
|
229 |
-
elif
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
else: # Handle file object or other types
|
235 |
logger.error(f"Unsupported image type: {type(image_path)}")
|
236 |
return None
|
@@ -283,7 +290,9 @@ def prepare_message_with_media(text, images=None, documents=None):
|
|
283 |
for doc in documents:
|
284 |
if doc is None:
|
285 |
continue
|
286 |
-
|
|
|
|
|
287 |
if doc_text:
|
288 |
document_texts.append(doc_text)
|
289 |
|
@@ -304,15 +313,27 @@ def prepare_message_with_media(text, images=None, documents=None):
|
|
304 |
content = [{"type": "text", "text": text}]
|
305 |
|
306 |
# Add images if any
|
307 |
-
if images
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
if encoded_image:
|
314 |
content.append({
|
315 |
-
"type": "image_url",
|
316 |
"image_url": {"url": encoded_image}
|
317 |
})
|
318 |
|
@@ -331,7 +352,7 @@ def format_to_message_dict(history):
|
|
331 |
return messages
|
332 |
|
333 |
def process_uploaded_images(files):
|
334 |
-
"""Process uploaded image files
|
335 |
file_paths = []
|
336 |
for file in files:
|
337 |
if hasattr(file, 'name'):
|
@@ -640,12 +661,10 @@ def create_app():
|
|
640 |
with gr.Row():
|
641 |
# Image upload
|
642 |
with gr.Accordion("Upload Images (for vision models)", open=False):
|
643 |
-
images = gr.
|
644 |
-
label="Uploaded Images",
|
645 |
-
|
646 |
-
|
647 |
-
height="auto",
|
648 |
-
object_fit="contain"
|
649 |
)
|
650 |
|
651 |
image_upload_btn = gr.UploadButton(
|
@@ -907,7 +926,7 @@ def create_app():
|
|
907 |
|
908 |
# Process uploaded images
|
909 |
image_upload_btn.upload(
|
910 |
-
fn=
|
911 |
inputs=image_upload_btn,
|
912 |
outputs=images
|
913 |
)
|
|
|
226 |
elif file_extension == "webp":
|
227 |
mime_type = "image/webp"
|
228 |
return f"data:{mime_type};base64,{encoded_string}"
|
229 |
+
elif hasattr(image_path, 'name'): # Handle Gradio file objects directly
|
230 |
+
with open(image_path.name, "rb") as image_file:
|
231 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
232 |
+
file_extension = image_path.name.split('.')[-1].lower()
|
233 |
+
mime_type = f"image/{file_extension}"
|
234 |
+
if file_extension in ["jpg", "jpeg"]:
|
235 |
+
mime_type = "image/jpeg"
|
236 |
+
elif file_extension == "png":
|
237 |
+
mime_type = "image/png"
|
238 |
+
elif file_extension == "webp":
|
239 |
+
mime_type = "image/webp"
|
240 |
+
return f"data:{mime_type};base64,{encoded_string}"
|
241 |
else: # Handle file object or other types
|
242 |
logger.error(f"Unsupported image type: {type(image_path)}")
|
243 |
return None
|
|
|
290 |
for doc in documents:
|
291 |
if doc is None:
|
292 |
continue
|
293 |
+
# Make sure to handle file objects properly
|
294 |
+
doc_path = doc.name if hasattr(doc, 'name') else doc
|
295 |
+
doc_text = extract_text_from_file(doc_path)
|
296 |
if doc_text:
|
297 |
document_texts.append(doc_text)
|
298 |
|
|
|
313 |
content = [{"type": "text", "text": text}]
|
314 |
|
315 |
# Add images if any
|
316 |
+
if images:
|
317 |
+
# Check if images is a list of image paths or file objects
|
318 |
+
if isinstance(images, list):
|
319 |
+
for img in images:
|
320 |
+
if img is None:
|
321 |
+
continue
|
322 |
+
|
323 |
+
encoded_image = encode_image_to_base64(img)
|
324 |
+
if encoded_image:
|
325 |
+
content.append({
|
326 |
+
"type": "image_url",
|
327 |
+
"image_url": {"url": encoded_image}
|
328 |
+
})
|
329 |
+
else:
|
330 |
+
# For single image or Gallery component
|
331 |
+
logger.warning(f"Images is not a list: {type(images)}")
|
332 |
+
# Try to handle as single image
|
333 |
+
encoded_image = encode_image_to_base64(images)
|
334 |
if encoded_image:
|
335 |
content.append({
|
336 |
+
"type": "image_url",
|
337 |
"image_url": {"url": encoded_image}
|
338 |
})
|
339 |
|
|
|
352 |
return messages
|
353 |
|
354 |
def process_uploaded_images(files):
|
355 |
+
"""Process uploaded image files"""
|
356 |
file_paths = []
|
357 |
for file in files:
|
358 |
if hasattr(file, 'name'):
|
|
|
661 |
with gr.Row():
|
662 |
# Image upload
|
663 |
with gr.Accordion("Upload Images (for vision models)", open=False):
|
664 |
+
images = gr.File(
|
665 |
+
label="Uploaded Images",
|
666 |
+
file_types=["image"],
|
667 |
+
file_count="multiple"
|
|
|
|
|
668 |
)
|
669 |
|
670 |
image_upload_btn = gr.UploadButton(
|
|
|
926 |
|
927 |
# Process uploaded images
|
928 |
image_upload_btn.upload(
|
929 |
+
fn=lambda files: files,
|
930 |
inputs=image_upload_btn,
|
931 |
outputs=images
|
932 |
)
|