Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,20 @@ import base64
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 8 |
-
MODEL = "Qwen/Qwen2-VL-7B-Instruct"
|
| 9 |
|
| 10 |
if not HF_TOKEN:
|
| 11 |
-
print("ERROR: HF_CV_ROBOT_TOKEN
|
| 12 |
|
| 13 |
def process(payload: dict):
|
| 14 |
try:
|
| 15 |
-
if not HF_TOKEN:
|
| 16 |
-
return {"error": "Hugging Face token is missing. Please check Space secrets."}
|
| 17 |
-
|
| 18 |
robot_id = payload.get("robot_id", "unknown")
|
| 19 |
image_b64 = payload["image_b64"]
|
| 20 |
|
| 21 |
-
# Router API payload using "type": "file" for base64
|
| 22 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
| 23 |
data = {
|
| 24 |
"model": MODEL,
|
| 25 |
"messages": [
|
|
@@ -27,7 +25,7 @@ def process(payload: dict):
|
|
| 27 |
"role": "user",
|
| 28 |
"content": [
|
| 29 |
{"type": "text", "text": "Describe this image in detail."},
|
| 30 |
-
{"type": "file", "file": image_b64
|
| 31 |
]
|
| 32 |
}
|
| 33 |
]
|
|
@@ -41,9 +39,9 @@ def process(payload: dict):
|
|
| 41 |
)
|
| 42 |
|
| 43 |
if resp.status_code != 200:
|
| 44 |
-
print(f"VLM API error: {resp.status_code}, {resp.text}")
|
| 45 |
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
|
| 46 |
|
|
|
|
| 47 |
try:
|
| 48 |
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 49 |
except (KeyError, IndexError, json.JSONDecodeError) as e:
|
|
@@ -56,9 +54,9 @@ def process(payload: dict):
|
|
| 56 |
}
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"An unexpected error occurred: {e}")
|
| 60 |
return {"error": str(e)}
|
| 61 |
|
|
|
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=process,
|
| 64 |
inputs=gr.JSON(label="Input Payload (Dict format)"),
|
|
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN") # 在 Space Secrets 裡設定
|
| 8 |
+
MODEL = "Qwen/Qwen2-VL-7B-Instruct" # 你要用的 VLM model
|
| 9 |
|
| 10 |
if not HF_TOKEN:
|
| 11 |
+
print("ERROR: HF_CV_ROBOT_TOKEN is not set!")
|
| 12 |
|
| 13 |
def process(payload: dict):
|
| 14 |
try:
|
|
|
|
|
|
|
|
|
|
| 15 |
robot_id = payload.get("robot_id", "unknown")
|
| 16 |
image_b64 = payload["image_b64"]
|
| 17 |
|
|
|
|
| 18 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 19 |
+
|
| 20 |
+
# 這裡用 type='file' + Base64,Router API 支援直接解析
|
| 21 |
data = {
|
| 22 |
"model": MODEL,
|
| 23 |
"messages": [
|
|
|
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
{"type": "text", "text": "Describe this image in detail."},
|
| 28 |
+
{"type": "file", "file": {"b64": image_b64, "name": "image.jpg"}}
|
| 29 |
]
|
| 30 |
}
|
| 31 |
]
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
if resp.status_code != 200:
|
|
|
|
| 42 |
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
|
| 43 |
|
| 44 |
+
# 解析回傳文字描述
|
| 45 |
try:
|
| 46 |
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 47 |
except (KeyError, IndexError, json.JSONDecodeError) as e:
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
except Exception as e:
|
|
|
|
| 57 |
return {"error": str(e)}
|
| 58 |
|
| 59 |
+
# Gradio MCP Server
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=process,
|
| 62 |
inputs=gr.JSON(label="Input Payload (Dict format)"),
|