hjtz commited on
Commit
714bc0e
·
verified ·
1 Parent(s): a4e8aed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -35
app.py CHANGED
@@ -1,84 +1,93 @@
1
  import gradio as gr
2
  import torch
3
  from PIL import Image
4
- # 根据你的模型,这里可能需要引入 transformers 中的 AutoModel, AutoProcessor 等
5
  # from transformers import AutoModel, AutoProcessor
6
 
7
  # ==========================================
8
- # 1. 加载模型权重 (全局加载,避免每次点击重复加载)
9
  # ==========================================
10
  MODEL_ID = "yangxiaoda/SpatialLogic-ckpt-tag"
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
- print(f"正在加载模型至 {device}...")
14
- # 【注意】这里需要替换为你模型真实的加载代码
15
- # 因为我无法看到该仓库内部的具体结构,通常代码如下:
16
  # processor = AutoProcessor.from_pretrained(MODEL_ID)
17
  # model = AutoModel.from_pretrained(MODEL_ID).to(device)
18
- print("模型加载完成!")
19
 
20
 
21
  # ==========================================
22
- # 2. 定义核心推理函数
23
  # ==========================================
24
  def compare_images(img1, img2, action_desc):
25
  """
26
- img1: PIL.Image 格式
27
- img2: PIL.Image 格式
28
- action_desc: 字符串格式的动作描述
29
  """
 
30
  if img1 is None or img2 is None:
31
- return "请确保上传了两张图片!"
32
  if not action_desc.strip():
33
- return "请输入动作描述!"
34
 
35
  try:
36
- # 【注意】这里需要替换为你模型真实的推理代码
37
- # 伪代码示例:
38
  # inputs = processor(text=action_desc, images=[img1, img2], return_tensors="pt").to(device)
39
  # outputs = model(**inputs)
40
- # score1, score2 = outputs.logits # 获取模型对两张图的打分
41
 
42
- # 假设这里是你算出的得分(为了演示,这里用随机逻辑代替)
43
- score_img1 = 0.8 # 替换为真实的 img1 得分
44
- score_img2 = 0.4 # 替换为真实的 img2 得分
45
 
46
- # 判断逻辑
47
  if score_img1 > score_img2:
48
  result = "img1"
49
- explanation = "模型认为 **图片 1 (img1)** 更符合该动作描述。"
50
  elif score_img2 > score_img1:
51
  result = "img2"
52
- explanation = "模型认为 **图片 2 (img2)** 更符合该动作描述。"
53
  else:
54
- result = "平局"
55
- explanation = "模型认为两张图完成度一致。"
56
 
57
- return f"**输出结果:** {result}\n\n**详细说明:** {explanation}"
 
58
 
59
  except Exception as e:
60
- return f"推理过程中出错: {str(e)}"
61
 
62
  # ==========================================
63
- # 3. 构建 Gradio 交互界面
64
  # ==========================================
65
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
66
- gr.Markdown("# 🧠 SpatialLogic 空间逻辑推理演示")
67
- gr.Markdown("上传两张图片并输入动作描述,模型将判断哪一张图片更接近任务完成状态。")
 
 
68
 
69
  with gr.Row():
70
  with gr.Column():
71
- img1_input = gr.Image(type="pil", label="图片 1 (img1)")
 
72
  with gr.Column():
73
- img2_input = gr.Image(type="pil", label="图片 2 (img2)")
74
 
75
- text_input = gr.Textbox(label="动作描述 (Action Description)", placeholder="例如:把苹果放在桌子上")
 
 
 
 
76
 
77
- submit_btn = gr.Button("🤖 开始判断", variant="primary")
 
78
 
79
- output_text = gr.Markdown(label="判断结果")
 
80
 
81
- # 绑定函数与组件
82
  submit_btn.click(
83
  fn=compare_images,
84
  inputs=[img1_input, img2_input, text_input],
@@ -86,5 +95,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
  )
87
 
88
  if __name__ == "__main__":
89
- # 启动应用
90
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
  from PIL import Image
 
4
  # from transformers import AutoModel, AutoProcessor
5
 
6
  # ==========================================
7
+ # 1. Load Model Weights
8
  # ==========================================
9
  MODEL_ID = "yangxiaoda/SpatialLogic-ckpt-tag"
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
+ print(f"Loading model to {device}...")
13
+ # [REPLACE THIS] with your actual model loading code
 
14
  # processor = AutoProcessor.from_pretrained(MODEL_ID)
15
  # model = AutoModel.from_pretrained(MODEL_ID).to(device)
16
+ print("Model loaded successfully!")
17
 
18
 
19
  # ==========================================
20
+ # 2. Define Core Inference Function
21
  # ==========================================
22
  def compare_images(img1, img2, action_desc):
23
  """
24
+ img1: PIL.Image
25
+ img2: PIL.Image
26
+ action_desc: string
27
  """
28
+ # 1. Input Validation
29
  if img1 is None or img2 is None:
30
+ return "⚠️ Please ensure both images (img1 and img2) are uploaded!"
31
  if not action_desc.strip():
32
+ return "⚠️ Please enter an action description!"
33
 
34
  try:
35
+ # [REPLACE THIS] with your actual inference code
36
+ # Example pseudo-code:
37
  # inputs = processor(text=action_desc, images=[img1, img2], return_tensors="pt").to(device)
38
  # outputs = model(**inputs)
39
+ # score1, score2 = outputs.logits
40
 
41
+ # Dummy scores for UI testing (Replace with real logic)
42
+ score_img1 = 0.8
43
+ score_img2 = 0.4
44
 
45
+ # 2. Logic Judgment
46
  if score_img1 > score_img2:
47
  result = "img1"
48
+ explanation = "The model evaluates that **Image 1 (img1)** is closer to the completed state of the described task."
49
  elif score_img2 > score_img1:
50
  result = "img2"
51
+ explanation = "The model evaluates that **Image 2 (img2)** is closer to the completed state of the described task."
52
  else:
53
+ result = "Tie"
54
+ explanation = "The model evaluates both images equally regarding the task completion."
55
 
56
+ # 3. Format Output
57
+ return f"### **Output:** {result}\n\n**Explanation:** {explanation}"
58
 
59
  except Exception as e:
60
+ return f"❌ An error occurred during inference: {str(e)}"
61
 
62
  # ==========================================
63
+ # 3. Build Gradio Interface
64
  # ==========================================
65
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
66
+ # Changed Title to the exact text you requested
67
+ gr.Markdown("# 🧠 SpatialLogic Reasoning Demo")
68
+ # Changed sub-title to English
69
+ gr.Markdown("Upload two images and input an action description. The model will predict which image is closer to the completed state of the task.")
70
 
71
  with gr.Row():
72
  with gr.Column():
73
+ # Changed labels to English
74
+ img1_input = gr.Image(type="pil", label="Image 1 (img1)")
75
  with gr.Column():
76
+ img2_input = gr.Image(type="pil", label="Image 2 (img2)")
77
 
78
+ # Changed label and placeholder to English
79
+ text_input = gr.Textbox(
80
+ label="Action Description",
81
+ placeholder="e.g., Put the apple on the table"
82
+ )
83
 
84
+ # Changed button text to English
85
+ submit_btn = gr.Button("🤖 Evaluate", variant="primary")
86
 
87
+ # Output area
88
+ output_text = gr.Markdown(label="Result")
89
 
90
+ # Bind function to components
91
  submit_btn.click(
92
  fn=compare_images,
93
  inputs=[img1_input, img2_input, text_input],
 
95
  )
96
 
97
  if __name__ == "__main__":
 
98
  demo.launch()