Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from fastmcp import Client
|
|
| 7 |
from fastmcp.client import StreamableHttpTransport
|
| 8 |
import asyncio
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
| 11 |
# Load environment variables (ensure .env is set up locally)
|
| 12 |
load_dotenv()
|
|
@@ -51,23 +52,23 @@ async def process_webcam_stream_async(image):
|
|
| 51 |
response = await MCP_CLIENT.call_tool(TOOL_NAME, payload)
|
| 52 |
|
| 53 |
if response.is_error:
|
| 54 |
-
# Access the first item in the content list for the error text
|
| 55 |
error_text = response.content[0].text if response.content and isinstance(response.content, list) else "Unknown error"
|
| 56 |
raise Exception(f"MCP Tool Error: {error_text}")
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
#response_dict = json.loads(raw_text)
|
| 62 |
|
| 63 |
-
#
|
|
|
|
| 64 |
|
| 65 |
-
description_out =
|
| 66 |
-
human_out =
|
| 67 |
-
|
| 68 |
-
environment_out =
|
| 69 |
|
| 70 |
-
#
|
|
|
|
| 71 |
|
| 72 |
return (
|
| 73 |
description_out,
|
|
@@ -78,6 +79,8 @@ async def process_webcam_stream_async(image):
|
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error calling remote MCP API: {e}")
|
|
|
|
|
|
|
| 81 |
return f"Error: {e}", "", "", ""
|
| 82 |
|
| 83 |
|
|
|
|
| 7 |
from fastmcp.client import StreamableHttpTransport
|
| 8 |
import asyncio
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
import ast # Import the Abstract Syntax Tree module
|
| 11 |
|
| 12 |
# Load environment variables (ensure .env is set up locally)
|
| 13 |
load_dotenv()
|
|
|
|
| 52 |
response = await MCP_CLIENT.call_tool(TOOL_NAME, payload)
|
| 53 |
|
| 54 |
if response.is_error:
|
|
|
|
| 55 |
error_text = response.content[0].text if response.content and isinstance(response.content, list) else "Unknown error"
|
| 56 |
raise Exception(f"MCP Tool Error: {error_text}")
|
| 57 |
|
| 58 |
+
# Use ast.literal_eval because your server returns Python-formatted strings (single quotes)
|
| 59 |
+
raw_text = response.content[0].text
|
| 60 |
+
response_dict = ast.literal_eval(raw_text)
|
|
|
|
| 61 |
|
| 62 |
+
# --- 👇 EXTRACTING EACH FIELD CORRECTLY 👇 ---
|
| 63 |
+
vlm_result = response_dict.get("result", {})
|
| 64 |
|
| 65 |
+
description_out = vlm_result.get("description", "")
|
| 66 |
+
human_out = vlm_result.get("human", "")
|
| 67 |
+
objects_list = vlm_result.get("objects", [])
|
| 68 |
+
environment_out = vlm_result.get("environment", "")
|
| 69 |
|
| 70 |
+
# Format the objects list into a single string for display
|
| 71 |
+
objects_str = ", ".join(objects_list) if isinstance(objects_list, list) else str(objects_list)
|
| 72 |
|
| 73 |
return (
|
| 74 |
description_out,
|
|
|
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
print(f"Error calling remote MCP API: {e}")
|
| 82 |
+
import traceback
|
| 83 |
+
traceback.print_exc() # Helps debug other errors
|
| 84 |
return f"Error: {e}", "", "", ""
|
| 85 |
|
| 86 |
|