Jiaqi-hkust commited on
Commit
e7d0281
·
verified ·
1 Parent(s): bb022be

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +52 -50
app.py CHANGED
@@ -182,6 +182,58 @@ custom_css = """
182
  #chatbot { height: 650px !important; overflow-y: auto; }
183
  """
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  def create_chat_ui():
186
 
187
  with gr.Blocks(title="Robust-R1") as demo:
@@ -245,56 +297,6 @@ def create_chat_ui():
245
  else:
246
  gr.Markdown("*No example images available, please manually upload images for testing*")
247
 
248
- async def respond(user_msg, history, temp, tokens):
249
- print("user_msg:")
250
- print(user_msg)
251
- text = user_msg.get("text", "").strip()
252
- files = user_msg.get("files", [])
253
- # 按照参考代码的格式:文件使用 {"path": x},文本直接使用字符串
254
- user_message = {"role": "user", "content": []}
255
-
256
- # 添加图像文件,使用 {"path": file_path} 格式
257
- for file_path in files:
258
- if file_path:
259
- # 确保使用绝对路径
260
- abs_path = os.path.abspath(file_path) if not os.path.isabs(file_path) else file_path
261
- user_message["content"].append({"path": abs_path})
262
-
263
- # 添加文本,直接使用字符串
264
- if text:
265
- user_message["content"].append(text)
266
-
267
- # 如果只有文本没有文件,content 保持为列表;如果都没有,content 为空列表
268
- # 如果只有文本,也可以直接使用字符串(参考 Gradio 的常见用法)
269
- if not files and text:
270
- user_message["content"] = text
271
- print("user_message:")
272
- print(user_message)
273
- history.append(user_message)
274
- yield history, gr.MultimodalTextbox(value=None, interactive=False)
275
-
276
- history.append({"role": "assistant", "content": ""})
277
-
278
- try:
279
- previous_history = history[:-2] if len(history) >= 2 else []
280
-
281
- handler = get_model_handler()
282
- generated_text = ""
283
- for chunk in handler.predict(user_msg, previous_history, temp, tokens):
284
- generated_text = chunk
285
-
286
- safe_text = generated_text.replace("<", "&lt;").replace(">", "&gt;")
287
-
288
- history[-1]["content"] = safe_text
289
- yield history, gr.MultimodalTextbox(interactive=False)
290
-
291
- except Exception as e:
292
- import traceback
293
- traceback.print_exc()
294
- history[-1]["content"] = f"❌ Inference error: {str(e)}"
295
- yield history, gr.MultimodalTextbox(interactive=True)
296
-
297
- yield history, gr.MultimodalTextbox(value=None, interactive=True)
298
 
299
  chat_input.submit(
300
  respond,
 
182
  #chatbot { height: 650px !important; overflow-y: auto; }
183
  """
184
 
185
+ @gpu_decorator
186
+ async def respond(user_msg, history, temp, tokens):
187
+ print("user_msg:")
188
+ print(user_msg)
189
+ text = user_msg.get("text", "").strip()
190
+ files = user_msg.get("files", [])
191
+ # 按照参考代码的格式:文件使用 {"path": x},文本直接使用字符串
192
+ user_message = {"role": "user", "content": []}
193
+
194
+ # 添加图像文件,使用 {"path": file_path} 格式
195
+ for file_path in files:
196
+ if file_path:
197
+ # 确保使用绝对路径
198
+ abs_path = os.path.abspath(file_path) if not os.path.isabs(file_path) else file_path
199
+ user_message["content"].append({"path": abs_path})
200
+
201
+ # 添加文本,直接使用字符串
202
+ if text:
203
+ user_message["content"].append(text)
204
+
205
+ # 如果只有文本没有文件,content 保持为列表;如果都没有,content 为空列表
206
+ # 如果只有文本,也可以直接使用字符串(参考 Gradio 的常见用法)
207
+ if not files and text:
208
+ user_message["content"] = text
209
+ print("user_message:")
210
+ print(user_message)
211
+ history.append(user_message)
212
+ yield history, gr.MultimodalTextbox(value=None, interactive=False)
213
+
214
+ history.append({"role": "assistant", "content": ""})
215
+
216
+ try:
217
+ previous_history = history[:-2] if len(history) >= 2 else []
218
+
219
+ handler = get_model_handler()
220
+ generated_text = ""
221
+ for chunk in handler.predict(user_msg, previous_history, temp, tokens):
222
+ generated_text = chunk
223
+
224
+ safe_text = generated_text.replace("<", "&lt;").replace(">", "&gt;")
225
+
226
+ history[-1]["content"] = safe_text
227
+ yield history, gr.MultimodalTextbox(interactive=False)
228
+
229
+ except Exception as e:
230
+ import traceback
231
+ traceback.print_exc()
232
+ history[-1]["content"] = f"❌ Inference error: {str(e)}"
233
+ yield history, gr.MultimodalTextbox(interactive=True)
234
+
235
+ yield history, gr.MultimodalTextbox(value=None, interactive=True)
236
+
237
  def create_chat_ui():
238
 
239
  with gr.Blocks(title="Robust-R1") as demo:
 
297
  else:
298
  gr.Markdown("*No example images available, please manually upload images for testing*")
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  chat_input.submit(
302
  respond,