Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -141,39 +141,51 @@ Respond in STRICT JSON ONLY:
|
|
| 141 |
|
| 142 |
|
| 143 |
# ---------------------------------------------------
|
| 144 |
-
# Gradio UI Function
|
| 145 |
# ---------------------------------------------------
|
| 146 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
"""
|
| 148 |
-
|
| 149 |
-
|
| 150 |
"""
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
app = gr.Interface(
|
| 156 |
-
fn=
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
| 159 |
outputs=gr.Json(label="Tool Output"),
|
| 160 |
-
title="Robot MCP Server",
|
| 161 |
-
description="
|
| 162 |
api_name="predict"
|
| 163 |
)
|
| 164 |
|
| 165 |
-
# ---------------------------------------------------
|
| 166 |
-
# Explicit MCP API Definition
|
| 167 |
-
# ---------------------------------------------------
|
| 168 |
-
# We explicitly add the API using the Pydantic model for schema generation
|
| 169 |
-
app.api.post(
|
| 170 |
-
"/mcp/tool/robot_watch", # This defines the exact endpoint path for the tool
|
| 171 |
-
run_vlm_analysis, # Link it to the Pydantic-typed function
|
| 172 |
-
inputs=[RobotWatchPayload], # Use the Pydantic model as the explicit input schema
|
| 173 |
-
outputs=[dict] # The output type
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
if __name__ == "__main__":
|
| 178 |
-
#
|
| 179 |
-
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
# ---------------------------------------------------
|
| 144 |
+
# Gradio UI Function (Uses individual fields)
|
| 145 |
# ---------------------------------------------------
|
| 146 |
+
def gradio_ui_with_fields(
|
| 147 |
+
hf_token_input: str,
|
| 148 |
+
robot_id_input: str,
|
| 149 |
+
image_file: gr.File # Gradio component for file upload
|
| 150 |
+
):
|
| 151 |
"""
|
| 152 |
+
Handles input from individual Gradio components, converts to Pydantic model,
|
| 153 |
+
and calls the core logic.
|
| 154 |
"""
|
| 155 |
+
if not image_file or not image_file.path:
|
| 156 |
+
return {"error": "Image file not uploaded."}
|
| 157 |
+
|
| 158 |
+
# Read the file from the path Gradio provides and convert to base64
|
| 159 |
+
with open(image_file.path, "rb") as f:
|
| 160 |
+
image_b64_input = base64.b64encode(f.read()).decode()
|
| 161 |
+
|
| 162 |
+
# Create the Pydantic model instance manually
|
| 163 |
+
payload_instance = RobotWatchPayload(
|
| 164 |
+
hf_token=hf_token_input,
|
| 165 |
+
robot_id=robot_id_input,
|
| 166 |
+
image_b64=image_b64_input
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Call the core logic
|
| 170 |
+
result = run_vlm_analysis(payload_instance)
|
| 171 |
+
return result
|
| 172 |
|
| 173 |
|
| 174 |
app = gr.Interface(
|
| 175 |
+
fn=gradio_ui_with_fields, # Use the multi-input function for the UI
|
| 176 |
+
inputs=[
|
| 177 |
+
gr.Textbox(label="Hugging Face Token", lines=1),
|
| 178 |
+
gr.Textbox(label="Robot ID", lines=1, value="unknown"),
|
| 179 |
+
gr.File(label="Upload Image (test.jpg)")
|
| 180 |
+
],
|
| 181 |
outputs=gr.Json(label="Tool Output"),
|
| 182 |
+
title="Robot MCP Server (Field Inputs)",
|
| 183 |
+
description="Interface for the robot VLM analysis using individual fields.",
|
| 184 |
api_name="predict"
|
| 185 |
)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
if __name__ == "__main__":
|
| 188 |
+
# Note: When using this method, the automatic MCP schema might become invalid
|
| 189 |
+
# again because the *function signature* has changed dramatically.
|
| 190 |
+
# You might *still* need the `mcp==1.8.1` pin in requirements.txt to work.
|
| 191 |
+
app.launch(mcp_server=True)
|