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

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +35 -13
  2. requirements.txt +9 -9
app.py CHANGED
@@ -14,13 +14,15 @@ sys_prompt = """First output the the types of degradations in image briefly in <
14
  project_dir = os.path.dirname(os.path.abspath(__file__))
15
 
16
  is_spaces = os.getenv("SPACE_ID") is not None
 
 
17
  if not is_spaces:
18
- temp_dir = os.path.join(project_dir, ".gradio_temp")
19
  os.makedirs(temp_dir, exist_ok=True)
20
  os.environ["GRADIO_TEMP_DIR"] = temp_dir
21
 
22
  MODEL_PATH = os.getenv("MODEL_PATH", "Jiaqi-hkust/Robust-R1-RL")
23
 
 
24
  print(f"==========================================")
25
  print(f"Initializing application...")
26
  print(f"==========================================")
@@ -175,13 +177,14 @@ def get_model_handler():
175
  model_handler = ModelHandler(MODEL_PATH)
176
  return model_handler
177
 
 
 
 
 
 
178
  def create_chat_ui():
179
- custom_css = """
180
- .gradio-container { font-family: 'Inter', sans-serif; }
181
- #chatbot { height: 650px !important; overflow-y: auto; }
182
- """
183
 
184
- with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="Robust-R1") as demo:
185
 
186
  with gr.Row():
187
  gr.Markdown("# 🤖Robust-R1:Degradation-Aware Reasoning for Robust Visual Understanding")
@@ -191,7 +194,6 @@ def create_chat_ui():
191
  chatbot = gr.Chatbot(
192
  elem_id="chatbot",
193
  label="Chat",
194
- type="messages",
195
  avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=Qwen"),
196
  height=650
197
  )
@@ -244,14 +246,30 @@ def create_chat_ui():
244
  gr.Markdown("*No example images available, please manually upload images for testing*")
245
 
246
  async def respond(user_msg, history, temp, tokens):
 
 
247
  text = user_msg.get("text", "").strip()
248
  files = user_msg.get("files", [])
249
- user_content = list(files)
250
- if text: user_content.append(text)
 
 
 
 
 
 
 
251
 
252
- if not files and text: user_message = {"role": "user", "content": text}
253
- else: user_message = {"role": "user", "content": user_content}
 
254
 
 
 
 
 
 
 
255
  history.append(user_message)
256
  yield history, gr.MultimodalTextbox(value=None, interactive=False)
257
 
@@ -295,15 +313,19 @@ if __name__ == "__main__":
295
  if is_spaces:
296
  print(f"🚀 Running on Hugging Face Spaces: {os.getenv('SPACE_ID')}")
297
  demo.launch(
 
 
298
  show_error=True,
299
- allowed_paths=[project_dir] if project_dir else None
300
  )
301
  else:
302
  print(f"🚀 Service is starting, please visit: http://localhost:7860")
303
  demo.launch(
 
 
304
  server_name="0.0.0.0",
305
  server_port=7860,
306
  share=False,
307
  show_error=True,
308
- allowed_paths=[project_dir]
309
  )
 
14
  project_dir = os.path.dirname(os.path.abspath(__file__))
15
 
16
  is_spaces = os.getenv("SPACE_ID") is not None
17
+ # temp_dir = os.path.join(project_dir, ".gradio_temp")
18
+ temp_dir = project_dir
19
  if not is_spaces:
 
20
  os.makedirs(temp_dir, exist_ok=True)
21
  os.environ["GRADIO_TEMP_DIR"] = temp_dir
22
 
23
  MODEL_PATH = os.getenv("MODEL_PATH", "Jiaqi-hkust/Robust-R1-RL")
24
 
25
+
26
  print(f"==========================================")
27
  print(f"Initializing application...")
28
  print(f"==========================================")
 
177
  model_handler = ModelHandler(MODEL_PATH)
178
  return model_handler
179
 
180
+ custom_css = """
181
+ .gradio-container { font-family: 'Inter', sans-serif; }
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:
188
 
189
  with gr.Row():
190
  gr.Markdown("# 🤖Robust-R1:Degradation-Aware Reasoning for Robust Visual Understanding")
 
194
  chatbot = gr.Chatbot(
195
  elem_id="chatbot",
196
  label="Chat",
 
197
  avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=Qwen"),
198
  height=650
199
  )
 
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
 
 
313
  if is_spaces:
314
  print(f"🚀 Running on Hugging Face Spaces: {os.getenv('SPACE_ID')}")
315
  demo.launch(
316
+ theme=gr.themes.Soft(),
317
+ css=custom_css,
318
  show_error=True,
319
+ allowed_paths=[project_dir, temp_dir] if project_dir else None
320
  )
321
  else:
322
  print(f"🚀 Service is starting, please visit: http://localhost:7860")
323
  demo.launch(
324
+ theme=gr.themes.Soft(),
325
+ css=custom_css,
326
  server_name="0.0.0.0",
327
  server_port=7860,
328
  share=False,
329
  show_error=True,
330
+ allowed_paths=[project_dir, temp_dir] if project_dir else None
331
  )
requirements.txt CHANGED
@@ -1,10 +1,10 @@
1
- gradio>=6.0.0
2
- torch>=2.0.0
3
- torchvision>=0.15.0
4
- transformers>=4.37.0
5
- qwen-vl-utils
6
- accelerate
7
- sentencepiece
8
- protobuf
9
- pillow
10
  einops
 
1
+ gradio>=6.1.0
2
+ torch>=2.0.0
3
+ torchvision>=0.15.0
4
+ transformers>=4.37.0
5
+ qwen-vl-utils
6
+ accelerate
7
+ sentencepiece
8
+ protobuf
9
+ pillow
10
  einops