Spaces:
Running on Zero
Running on Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -109,28 +109,44 @@ def generate_streaming(messages, max_new_tokens, max_denoising_steps, enable_thi
|
|
| 109 |
yield build_highlight(final_text, ""), final_text, final_text
|
| 110 |
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
def to_model_messages(history):
|
| 113 |
-
"""Convert Gradio messages history into processor chat format with images."""
|
| 114 |
messages = []
|
| 115 |
for msg in history:
|
| 116 |
role = msg["role"]
|
| 117 |
content = msg["content"]
|
|
|
|
| 118 |
if isinstance(content, str):
|
| 119 |
-
parts
|
| 120 |
-
elif isinstance(content, (
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
return messages
|
| 135 |
|
| 136 |
|
|
|
|
| 109 |
yield build_highlight(final_text, ""), final_text, final_text
|
| 110 |
|
| 111 |
|
| 112 |
+
def _file_path(item):
|
| 113 |
+
"""Extract a local file path from a Gradio content part / file dict."""
|
| 114 |
+
for key in ("file", "path", "url"):
|
| 115 |
+
val = item.get(key)
|
| 116 |
+
if isinstance(val, dict):
|
| 117 |
+
val = val.get("path") or val.get("url")
|
| 118 |
+
if val:
|
| 119 |
+
return val
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
+
|
| 123 |
def to_model_messages(history):
|
| 124 |
+
"""Convert Gradio (messages format) history into processor chat format with images."""
|
| 125 |
messages = []
|
| 126 |
for msg in history:
|
| 127 |
role = msg["role"]
|
| 128 |
content = msg["content"]
|
| 129 |
+
parts = []
|
| 130 |
if isinstance(content, str):
|
| 131 |
+
parts.append({"type": "text", "text": content})
|
| 132 |
+
elif isinstance(content, tuple): # legacy (path, alt) file tuple
|
| 133 |
+
parts.append({"type": "image", "url": content[0]})
|
| 134 |
+
elif isinstance(content, dict): # single file part, e.g. {"path": ...}
|
| 135 |
+
p = _file_path(content)
|
| 136 |
+
if p:
|
| 137 |
+
parts.append({"type": "image", "url": p})
|
| 138 |
+
elif isinstance(content, list):
|
| 139 |
+
for item in content:
|
| 140 |
+
if not isinstance(item, dict):
|
| 141 |
+
parts.append({"type": "text", "text": str(item)})
|
| 142 |
+
elif item.get("type") == "text" or "text" in item:
|
| 143 |
+
parts.append({"type": "text", "text": item.get("text", "")})
|
| 144 |
+
else:
|
| 145 |
+
p = _file_path(item)
|
| 146 |
+
if p:
|
| 147 |
+
parts.append({"type": "image", "url": p})
|
| 148 |
+
if parts:
|
| 149 |
+
messages.append({"role": role, "content": parts})
|
| 150 |
return messages
|
| 151 |
|
| 152 |
|