Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import base64
|
|
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 7 |
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 8 |
-
MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def process(payload: dict):
|
| 11 |
try:
|
| 12 |
if not HF_TOKEN:
|
| 13 |
-
return {"error": "
|
| 14 |
|
| 15 |
robot_id = payload.get("robot_id", "unknown")
|
| 16 |
image_b64 = payload["image_b64"]
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
data = {
|
| 22 |
"model": MODEL,
|
| 23 |
"messages": [
|
| 24 |
{
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
-
{"type": "text", "text":
|
| 28 |
]
|
| 29 |
}
|
| 30 |
]
|
|
@@ -32,21 +59,18 @@ def process(payload: dict):
|
|
| 32 |
|
| 33 |
resp = requests.post(
|
| 34 |
"https://router.huggingface.co/v1/chat/completions",
|
| 35 |
-
headers={
|
| 36 |
-
|
| 37 |
-
"Content-Type": "application/json"
|
| 38 |
-
},
|
| 39 |
-
data=json.dumps(data),
|
| 40 |
timeout=60
|
| 41 |
)
|
| 42 |
|
| 43 |
if resp.status_code != 200:
|
| 44 |
-
return {"error": f"
|
| 45 |
|
| 46 |
try:
|
| 47 |
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 48 |
-
except
|
| 49 |
-
return {"error": f"
|
| 50 |
|
| 51 |
return {
|
| 52 |
"received": True,
|
|
@@ -57,11 +81,13 @@ def process(payload: dict):
|
|
| 57 |
except Exception as e:
|
| 58 |
return {"error": str(e)}
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
demo = gr.Interface(
|
| 62 |
fn=process,
|
| 63 |
-
inputs=gr.JSON(label="Input Payload"),
|
| 64 |
-
outputs=gr.JSON(label="Reply"),
|
| 65 |
api_name="predict"
|
| 66 |
)
|
| 67 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import base64
|
| 4 |
+
from io import BytesIO
|
| 5 |
import requests
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# HF token & model
|
| 9 |
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 10 |
+
MODEL = "Qwen/Qwen2.5-VL-7B-Instruct" # HF 支援列表裡的模型
|
| 11 |
|
| 12 |
+
if not HF_TOKEN:
|
| 13 |
+
print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
|
| 14 |
+
|
| 15 |
+
# -------------------------------
|
| 16 |
+
# 主處理函數 (Main Processing Function)
|
| 17 |
+
# -------------------------------
|
| 18 |
def process(payload: dict):
|
| 19 |
try:
|
| 20 |
if not HF_TOKEN:
|
| 21 |
+
return {"error": "Hugging Face token is missing. Please check Space secrets."}
|
| 22 |
|
| 23 |
robot_id = payload.get("robot_id", "unknown")
|
| 24 |
image_b64 = payload["image_b64"]
|
| 25 |
|
| 26 |
+
# Base64 -> bytes -> 保存為 tmp.jpg
|
| 27 |
+
tmp_path = "tmp.jpg"
|
| 28 |
+
with open(tmp_path, "wb") as f:
|
| 29 |
+
f.write(base64.b64decode(image_b64))
|
| 30 |
+
|
| 31 |
+
# 上傳 image file 到 HF Router
|
| 32 |
+
files = {"file": open(tmp_path, "rb")}
|
| 33 |
+
upload_resp = requests.post(
|
| 34 |
+
"https://huggingface.co/api/uploads",
|
| 35 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 36 |
+
files=files
|
| 37 |
+
)
|
| 38 |
+
files["file"].close()
|
| 39 |
+
os.remove(tmp_path)
|
| 40 |
+
|
| 41 |
+
if upload_resp.status_code != 200:
|
| 42 |
+
return {"error": f"HF upload failed: {upload_resp.status_code}, {upload_resp.text}"}
|
| 43 |
+
|
| 44 |
+
file_info = upload_resp.json()
|
| 45 |
+
file_url = file_info.get("href") # 取得 HF hosted file URL
|
| 46 |
|
| 47 |
+
# JSON payload 放文字訊息 + image file reference
|
| 48 |
data = {
|
| 49 |
"model": MODEL,
|
| 50 |
"messages": [
|
| 51 |
{
|
| 52 |
"role": "user",
|
| 53 |
"content": [
|
| 54 |
+
{"type": "text", "text": f" Describe this image in detail."}
|
| 55 |
]
|
| 56 |
}
|
| 57 |
]
|
|
|
|
| 59 |
|
| 60 |
resp = requests.post(
|
| 61 |
"https://router.huggingface.co/v1/chat/completions",
|
| 62 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 63 |
+
data={"payload": json.dumps(data)},
|
|
|
|
|
|
|
|
|
|
| 64 |
timeout=60
|
| 65 |
)
|
| 66 |
|
| 67 |
if resp.status_code != 200:
|
| 68 |
+
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
|
| 69 |
|
| 70 |
try:
|
| 71 |
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 72 |
+
except (KeyError, IndexError, json.JSONDecodeError) as e:
|
| 73 |
+
return {"error": f"Failed to parse VLM response: {e}, Response text: {resp.text}"}
|
| 74 |
|
| 75 |
return {
|
| 76 |
"received": True,
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
return {"error": str(e)}
|
| 83 |
|
| 84 |
+
# -------------------------------
|
| 85 |
+
# Gradio MCP Server
|
| 86 |
+
# -------------------------------
|
| 87 |
demo = gr.Interface(
|
| 88 |
fn=process,
|
| 89 |
+
inputs=gr.JSON(label="Input Payload (Dict format)"),
|
| 90 |
+
outputs=gr.JSON(label="Reply to Jetson"),
|
| 91 |
api_name="predict"
|
| 92 |
)
|
| 93 |
|