Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import base64
|
| 3 |
import json
|
|
|
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
HF_ROUTER_API = "https://router.huggingface.co/hf-inference"
|
| 8 |
HF_TOKEN = os.getenv("HF_CV_ROBOT_TOKEN")
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def call_vlm_api(payload: dict):
|
| 12 |
-
"""
|
| 13 |
-
Call Hugging Face Router Inference API with Base64 image.
|
| 14 |
-
"""
|
| 15 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 16 |
-
|
| 17 |
-
data = {
|
| 18 |
-
"model": MODEL_NAME,
|
| 19 |
-
"inputs": [
|
| 20 |
-
{
|
| 21 |
-
"image": {"b64": payload["image_b64"]},
|
| 22 |
-
"text": "Describe the image in detail."
|
| 23 |
-
}
|
| 24 |
-
]
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
try:
|
| 28 |
-
resp = requests.post(HF_ROUTER_API, headers=headers, json=data, timeout=60)
|
| 29 |
-
if resp.status_code == 200:
|
| 30 |
-
# 取第一個 generated_text
|
| 31 |
-
return resp.json()[0].get("generated_text", "")
|
| 32 |
-
else:
|
| 33 |
-
return f"VLM API error: {resp.status_code}, {resp.text}"
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"Exception: {str(e)}"
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
def process(payload: dict):
|
| 38 |
-
"""
|
| 39 |
-
Process JSON payload from Jetson: Base64 image + robot_id
|
| 40 |
-
Return JSON with VLM analysis
|
| 41 |
-
"""
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"received": True,
|
| 46 |
-
"robot_id":
|
| 47 |
"vllm_analysis": vlm_text
|
| 48 |
}
|
| 49 |
-
|
| 50 |
except Exception as e:
|
| 51 |
return {"error": str(e)}
|
| 52 |
|
| 53 |
-
#
|
|
|
|
|
|
|
| 54 |
demo = gr.Interface(
|
| 55 |
fn=process,
|
| 56 |
-
inputs=gr.JSON(label="Input Payload
|
| 57 |
outputs=gr.JSON(label="Reply to Jetson"),
|
| 58 |
api_name="predict"
|
| 59 |
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import json
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
import requests
|
| 7 |
import os
|
| 8 |
|
|
|
|
| 9 |
HF_TOKEN = os.getenv("HF_CV_ROBOT_TOKEN")
|
| 10 |
+
MODEL = "Qwen/Qwen3-VL-32B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# 主處理函數
|
| 14 |
+
# -------------------------------
|
| 15 |
def process(payload: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
+
robot_id = payload.get("robot_id", "unknown")
|
| 18 |
+
image_b64 = payload["image_b64"]
|
| 19 |
+
|
| 20 |
+
# Base64 解碼成圖片,用 PIL 開啟
|
| 21 |
+
img_bytes = base64.b64decode(image_b64)
|
| 22 |
+
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 23 |
+
|
| 24 |
+
# Router API payload
|
| 25 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 26 |
+
data = {
|
| 27 |
+
"model": MODEL,
|
| 28 |
+
"messages": [
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": [
|
| 32 |
+
{"type": "text", "text": "Describe this image in detail."},
|
| 33 |
+
{"type": "image_data", "image_data": {"b64": image_b64}}
|
| 34 |
+
]
|
| 35 |
+
}
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
resp = requests.post(
|
| 40 |
+
"https://router.huggingface.co/v1/chat/completions",
|
| 41 |
+
headers=headers,
|
| 42 |
+
json=data,
|
| 43 |
+
timeout=60
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if resp.status_code != 200:
|
| 47 |
+
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
|
| 48 |
+
|
| 49 |
+
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 50 |
+
|
| 51 |
+
return {
|
| 52 |
"received": True,
|
| 53 |
+
"robot_id": robot_id,
|
| 54 |
"vllm_analysis": vlm_text
|
| 55 |
}
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
return {"error": str(e)}
|
| 59 |
|
| 60 |
+
# -------------------------------
|
| 61 |
+
# Gradio MCP Server
|
| 62 |
+
# -------------------------------
|
| 63 |
demo = gr.Interface(
|
| 64 |
fn=process,
|
| 65 |
+
inputs=gr.JSON(label="Input Payload (Dict format)"),
|
| 66 |
outputs=gr.JSON(label="Reply to Jetson"),
|
| 67 |
api_name="predict"
|
| 68 |
)
|