OppaAI commited on
Commit
6916c39
·
verified ·
1 Parent(s): 058cbb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -25
app.py CHANGED
@@ -141,39 +141,51 @@ Respond in STRICT JSON ONLY:
141
 
142
 
143
  # ---------------------------------------------------
144
- # Gradio UI Function
145
  # ---------------------------------------------------
146
- def gradio_interface_fn(payload: RobotWatchPayload):
 
 
 
 
147
  """
148
- This function acts as the entry point for both the Gradio UI and the MCP Server endpoint.
149
- Using the Pydantic model ensures a valid JSON schema is exposed.
150
  """
151
- # When called via MCP, the input is already a RobotWatchPayload instance.
152
- return run_vlm_analysis(payload)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
 
155
  app = gr.Interface(
156
- fn=gradio_interface_fn, # Use the single entry point function
157
- # Corrected input component from gr.JSON() to gr.Json() as per Gradio documentation
158
- inputs=gr.Json(label="Input Payload (Pydantic Schema Applied)"),
 
 
 
159
  outputs=gr.Json(label="Tool Output"),
160
- title="Robot MCP Server",
161
- description="A MCP Server to describe image obtained from the CV of a robot/webcam.",
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
- # Launch Gradio with mcp_server=True which hooks up the above API
179
- app.launch(mcp_server=True)
 
 
 
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)