multimodalart HF Staff commited on
Commit
252def7
·
verified ·
1 Parent(s): 08f4b6e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +32 -16
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 = [{"type": "text", "text": content}]
120
- elif isinstance(content, (list, tuple)):
121
- # Gradio file tuple (path, alt) or list of content dicts
122
- parts = []
123
- if isinstance(content, tuple):
124
- parts.append({"type": "image", "url": content[0]})
125
- else:
126
- for item in content:
127
- if isinstance(item, dict) and item.get("type") == "text":
128
- parts.append({"type": "text", "text": item["text"]})
129
- elif isinstance(item, dict) and "path" in item:
130
- parts.append({"type": "image", "url": item["path"]})
131
- else:
132
- parts = [{"type": "text", "text": str(content)}]
133
- messages.append({"role": role, "content": parts})
 
 
 
 
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