Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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"
|
| 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
|
| 44 |
-
score_img2 = 0.4
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if score_img1 > score_img2:
|
| 48 |
result = "img1"
|
| 49 |
-
explanation = "
|
| 50 |
elif score_img2 > score_img1:
|
| 51 |
result = "img2"
|
| 52 |
-
explanation = "
|
| 53 |
else:
|
| 54 |
-
result = "
|
| 55 |
-
explanation = "
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
-
return f"
|
| 61 |
|
| 62 |
# ==========================================
|
| 63 |
-
# 3.
|
| 64 |
# ==========================================
|
| 65 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 66 |
-
|
| 67 |
-
gr.Markdown("
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column():
|
| 71 |
-
|
|
|
|
| 72 |
with gr.Column():
|
| 73 |
-
img2_input = gr.Image(type="pil", label="
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
| 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()
|