Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,42 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
demo = gr.Interface(
|
| 13 |
-
fn=
|
| 14 |
-
inputs=gr.
|
| 15 |
-
outputs=gr.JSON(label="Reply"),
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
+
# Add a docstring for your function; this is used to generate the MCP tool description
|
| 8 |
+
def process(json_input: dict) -> dict:
|
| 9 |
+
"""
|
| 10 |
+
Processes incoming JSON data containing a base64 encoded image and robot ID.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
json_input (dict): A dictionary with keys "image_b64" and "robot_id".
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
dict: A response dictionary indicating success, robot ID, and image size, or an error.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
data = json_input # Already a dict from Gradio API
|
| 20 |
+
|
| 21 |
+
img_bytes = base64.b64decode(data["image_b64"])
|
| 22 |
+
img = Image.open(io.BytesIO(img_bytes))
|
| 23 |
+
|
| 24 |
+
response = {
|
| 25 |
+
"received": True,
|
| 26 |
+
"robot_id": data.get("robot_id", "unknown"),
|
| 27 |
+
"image_size": img.size
|
| 28 |
+
}
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return {"error": str(e)}
|
| 33 |
|
| 34 |
demo = gr.Interface(
|
| 35 |
+
fn=process,
|
| 36 |
+
inputs=gr.JSON(label="Input JSON from Jetson"),
|
| 37 |
+
outputs=gr.JSON(label="Reply to Jetson"),
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Launch with mcp_server enabled
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch(mcp_server=True)
|