Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,8 @@ import base64
|
|
| 3 |
import time
|
| 4 |
import io
|
| 5 |
import gradio as gr
|
| 6 |
-
# Replace gradio_client with fastmcp Client and transport
|
| 7 |
from fastmcp import Client
|
| 8 |
from fastmcp.client import StreamableHttpTransport
|
| 9 |
-
# Import asyncio to manage async calls within the stream function
|
| 10 |
import asyncio
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
|
@@ -17,11 +15,13 @@ ROBOT_ID = os.environ.get("ROBOT_ID", "unknown")
|
|
| 17 |
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 18 |
if not HF_TOKEN:
|
| 19 |
print("Warning: HF_TOKEN not found. API calls may fail.")
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# The MCP URL of your remote server
|
| 22 |
MCP_SERVER_URL = "https://oppaai-robot-mcp-server.hf.space/gradio_api/mcp/"
|
| 23 |
SERVER_NAME = "Robot_MCP_Server"
|
| 24 |
-
# The exact tool name that matches the server function:
|
| 25 |
TOOL_NAME = "Robot_MCP_Server_robot_watch"
|
| 26 |
|
| 27 |
|
|
@@ -30,18 +30,21 @@ HTTP_TRANSPORT = StreamableHttpTransport(url=MCP_SERVER_URL)
|
|
| 30 |
MCP_CLIENT = Client(transport=HTTP_TRANSPORT, name=SERVER_NAME)
|
| 31 |
|
| 32 |
|
| 33 |
-
# This function needs to be an async function because client.call_tool is async
|
| 34 |
async def process_webcam_stream_async(image):
|
| 35 |
"""Send webcam image to HF MCP Server using MCP protocol and get result"""
|
| 36 |
if image is None:
|
| 37 |
return "", "", "", ""
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Convert Image to base64
|
| 40 |
buffered = io.BytesIO()
|
| 41 |
image.save(buffered, format="JPEG")
|
| 42 |
b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 43 |
|
| 44 |
-
# Prepare payload
|
| 45 |
payload = {
|
| 46 |
"hf_token_input": HF_TOKEN,
|
| 47 |
"robot_id_input": ROBOT_ID,
|
|
@@ -49,7 +52,6 @@ async def process_webcam_stream_async(image):
|
|
| 49 |
}
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
# Use the global client instance to call the tool asynchronously
|
| 53 |
async with MCP_CLIENT:
|
| 54 |
response = await MCP_CLIENT.call_tool(TOOL_NAME, payload)
|
| 55 |
|
|
@@ -57,7 +59,6 @@ async def process_webcam_stream_async(image):
|
|
| 57 |
error_text = response.content.text if response.content else "Unknown error"
|
| 58 |
raise Exception(f"MCP Tool Error: {error_text}")
|
| 59 |
|
| 60 |
-
# Parse the JSON string response from the server's output
|
| 61 |
import json
|
| 62 |
response_dict = json.loads(response.content.text)
|
| 63 |
|
|
@@ -84,7 +85,7 @@ async def process_webcam_stream_async(image):
|
|
| 84 |
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
gr.Markdown("## 🎥 Robot Vision Webcam Stream (using MCP Client)")
|
| 87 |
-
|
| 88 |
with gr.Row():
|
| 89 |
webcam_input = gr.Image(
|
| 90 |
label="Captured from Web-Cam",
|
|
@@ -97,9 +98,8 @@ with gr.Blocks() as demo:
|
|
| 97 |
objects_out = gr.Textbox(label="Objects")
|
| 98 |
environment_out = gr.Textbox(label="Environment")
|
| 99 |
|
| 100 |
-
# Gradio handles the local streaming loop and automatically wraps async functions
|
| 101 |
webcam_input.stream(
|
| 102 |
-
process_webcam_stream_async,
|
| 103 |
inputs=[webcam_input],
|
| 104 |
outputs=[description_out, human_out, objects_out, environment_out],
|
| 105 |
stream_every=0.5
|
|
|
|
| 3 |
import time
|
| 4 |
import io
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
from fastmcp import Client
|
| 7 |
from fastmcp.client import StreamableHttpTransport
|
|
|
|
| 8 |
import asyncio
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
|
|
|
| 15 |
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 16 |
if not HF_TOKEN:
|
| 17 |
print("Warning: HF_TOKEN not found. API calls may fail.")
|
| 18 |
+
# Set a placeholder string to avoid the 'None is not of type string' error
|
| 19 |
+
# The API call will fail later due to auth, but validation will pass.
|
| 20 |
+
HF_TOKEN = "missing_token_placeholder"
|
| 21 |
|
| 22 |
# The MCP URL of your remote server
|
| 23 |
MCP_SERVER_URL = "https://oppaai-robot-mcp-server.hf.space/gradio_api/mcp/"
|
| 24 |
SERVER_NAME = "Robot_MCP_Server"
|
|
|
|
| 25 |
TOOL_NAME = "Robot_MCP_Server_robot_watch"
|
| 26 |
|
| 27 |
|
|
|
|
| 30 |
MCP_CLIENT = Client(transport=HTTP_TRANSPORT, name=SERVER_NAME)
|
| 31 |
|
| 32 |
|
|
|
|
| 33 |
async def process_webcam_stream_async(image):
|
| 34 |
"""Send webcam image to HF MCP Server using MCP protocol and get result"""
|
| 35 |
if image is None:
|
| 36 |
return "", "", "", ""
|
| 37 |
|
| 38 |
+
# Check if a valid token is available before proceeding
|
| 39 |
+
if HF_TOKEN == "missing_token_placeholder":
|
| 40 |
+
return "Error: HF_TOKEN not set locally.", "", "", ""
|
| 41 |
+
|
| 42 |
# Convert Image to base64
|
| 43 |
buffered = io.BytesIO()
|
| 44 |
image.save(buffered, format="JPEG")
|
| 45 |
b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 46 |
|
| 47 |
+
# Prepare payload
|
| 48 |
payload = {
|
| 49 |
"hf_token_input": HF_TOKEN,
|
| 50 |
"robot_id_input": ROBOT_ID,
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
try:
|
|
|
|
| 55 |
async with MCP_CLIENT:
|
| 56 |
response = await MCP_CLIENT.call_tool(TOOL_NAME, payload)
|
| 57 |
|
|
|
|
| 59 |
error_text = response.content.text if response.content else "Unknown error"
|
| 60 |
raise Exception(f"MCP Tool Error: {error_text}")
|
| 61 |
|
|
|
|
| 62 |
import json
|
| 63 |
response_dict = json.loads(response.content.text)
|
| 64 |
|
|
|
|
| 85 |
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("## 🎥 Robot Vision Webcam Stream (using MCP Client)")
|
| 88 |
+
# ... (Gradio UI layout remains the same) ...
|
| 89 |
with gr.Row():
|
| 90 |
webcam_input = gr.Image(
|
| 91 |
label="Captured from Web-Cam",
|
|
|
|
| 98 |
objects_out = gr.Textbox(label="Objects")
|
| 99 |
environment_out = gr.Textbox(label="Environment")
|
| 100 |
|
|
|
|
| 101 |
webcam_input.stream(
|
| 102 |
+
process_webcam_stream_async,
|
| 103 |
inputs=[webcam_input],
|
| 104 |
outputs=[description_out, human_out, objects_out, environment_out],
|
| 105 |
stream_every=0.5
|