Spaces:
Paused
Paused
Commit ·
4dbf459
1
Parent(s): 0fd3146
better handling of images
Browse files
app.py
CHANGED
|
@@ -27,6 +27,52 @@ def _image_to_data_url(image_path: str, max_side: int = 1120, quality: int = 85)
|
|
| 27 |
return "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode()
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def respond(
|
| 31 |
message,
|
| 32 |
history: list[dict],
|
|
@@ -57,7 +103,7 @@ def respond(
|
|
| 57 |
content = text
|
| 58 |
|
| 59 |
messages = [{"role": "system", "content": system_message}]
|
| 60 |
-
messages.extend(history)
|
| 61 |
messages.append({"role": "user", "content": content})
|
| 62 |
|
| 63 |
answer = run_agent(
|
|
|
|
| 27 |
return "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode()
|
| 28 |
|
| 29 |
|
| 30 |
+
def _normalize_content_for_api(content):
|
| 31 |
+
"""
|
| 32 |
+
Gradio multimodal history uses {'type': 'file', 'file': FileData}; the HF
|
| 33 |
+
chat API expects {'type': 'image_url', 'image_url': {'url': ...}}.
|
| 34 |
+
"""
|
| 35 |
+
if isinstance(content, str):
|
| 36 |
+
return content
|
| 37 |
+
if not isinstance(content, list):
|
| 38 |
+
return content
|
| 39 |
+
out: list = []
|
| 40 |
+
for part in content:
|
| 41 |
+
if isinstance(part, str):
|
| 42 |
+
out.append({"type": "text", "text": part})
|
| 43 |
+
continue
|
| 44 |
+
if not isinstance(part, dict):
|
| 45 |
+
continue
|
| 46 |
+
ptype = part.get("type")
|
| 47 |
+
if ptype == "text":
|
| 48 |
+
out.append({"type": "text", "text": part.get("text", "")})
|
| 49 |
+
elif ptype == "file":
|
| 50 |
+
fd = part.get("file")
|
| 51 |
+
if isinstance(fd, dict) and fd.get("path"):
|
| 52 |
+
out.append(
|
| 53 |
+
{
|
| 54 |
+
"type": "image_url",
|
| 55 |
+
"image_url": {"url": _image_to_data_url(fd["path"])},
|
| 56 |
+
}
|
| 57 |
+
)
|
| 58 |
+
elif ptype == "image_url":
|
| 59 |
+
out.append(part)
|
| 60 |
+
if not out:
|
| 61 |
+
return ""
|
| 62 |
+
if len(out) == 1 and out[0].get("type") == "text":
|
| 63 |
+
return out[0].get("text", "")
|
| 64 |
+
return out
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def _normalize_history_message(msg: dict) -> dict:
|
| 68 |
+
role = msg.get("role")
|
| 69 |
+
if role not in ("user", "assistant", "system"):
|
| 70 |
+
return msg
|
| 71 |
+
content = msg.get("content")
|
| 72 |
+
normalized = _normalize_content_for_api(content)
|
| 73 |
+
return {**msg, "content": normalized}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
def respond(
|
| 77 |
message,
|
| 78 |
history: list[dict],
|
|
|
|
| 103 |
content = text
|
| 104 |
|
| 105 |
messages = [{"role": "system", "content": system_message}]
|
| 106 |
+
messages.extend(_normalize_history_message(m) for m in history)
|
| 107 |
messages.append({"role": "user", "content": content})
|
| 108 |
|
| 109 |
answer = run_agent(
|