Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,13 @@ 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" #
|
| 11 |
|
| 12 |
if not HF_TOKEN:
|
| 13 |
print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
-
# 主處理函數
|
| 17 |
# -------------------------------
|
| 18 |
def process(payload: dict):
|
| 19 |
try:
|
|
@@ -23,15 +23,17 @@ def process(payload: dict):
|
|
| 23 |
robot_id = payload.get("robot_id", "unknown")
|
| 24 |
image_b64 = payload["image_b64"]
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
| 27 |
img_bytes = base64.b64decode(image_b64)
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
# JSON payload 只放文字訊息
|
| 35 |
data = {
|
| 36 |
"model": MODEL,
|
| 37 |
"messages": [
|
|
@@ -44,32 +46,43 @@ def process(payload: dict):
|
|
| 44 |
]
|
| 45 |
}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
resp = requests.post(
|
| 48 |
"https://router.huggingface.co/v1/chat/completions",
|
| 49 |
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 50 |
data={"payload": json.dumps(data)},
|
| 51 |
-
files=
|
| 52 |
timeout=60
|
| 53 |
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
if resp.status_code != 200:
|
| 56 |
print(f"VLM API error: {resp.status_code}, {resp.text}")
|
| 57 |
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
|
| 58 |
|
| 59 |
-
#
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return {"error": f"Failed to parse VLM response: {e}, Response text: {resp.text}"}
|
| 64 |
|
| 65 |
return {
|
| 66 |
"received": True,
|
| 67 |
"robot_id": robot_id,
|
| 68 |
-
"vllm_analysis": vlm_text
|
| 69 |
}
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
-
print(f"
|
| 73 |
return {"error": str(e)}
|
| 74 |
|
| 75 |
# -------------------------------
|
|
|
|
| 7 |
|
| 8 |
# HF token & model
|
| 9 |
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 10 |
+
MODEL = "Qwen/Qwen2.5-VL-7B-Instruct" # 確認此模型有支援 VLM (目前有)
|
| 11 |
|
| 12 |
if not HF_TOKEN:
|
| 13 |
print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
+
# 主處理函數
|
| 17 |
# -------------------------------
|
| 18 |
def process(payload: dict):
|
| 19 |
try:
|
|
|
|
| 23 |
robot_id = payload.get("robot_id", "unknown")
|
| 24 |
image_b64 = payload["image_b64"]
|
| 25 |
|
| 26 |
+
# ------------------------------------------------
|
| 27 |
+
# ⭐ 1) Base64 → 圖檔並存成 temp.jpg
|
| 28 |
+
# ------------------------------------------------
|
| 29 |
img_bytes = base64.b64decode(image_b64)
|
| 30 |
+
temp_path = "temp.jpg"
|
| 31 |
+
with open(temp_path, "wb") as f:
|
| 32 |
+
f.write(img_bytes)
|
| 33 |
|
| 34 |
+
# ------------------------------------------------
|
| 35 |
+
# ⭐ 2) JSON 部分(只放文字)
|
| 36 |
+
# ------------------------------------------------
|
|
|
|
|
|
|
|
|
|
| 37 |
data = {
|
| 38 |
"model": MODEL,
|
| 39 |
"messages": [
|
|
|
|
| 46 |
]
|
| 47 |
}
|
| 48 |
|
| 49 |
+
# ------------------------------------------------
|
| 50 |
+
# ⭐ 3) 用 multipart/form-data 傳送 image + JSON payload
|
| 51 |
+
# ------------------------------------------------
|
| 52 |
resp = requests.post(
|
| 53 |
"https://router.huggingface.co/v1/chat/completions",
|
| 54 |
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 55 |
data={"payload": json.dumps(data)},
|
| 56 |
+
files={"file": ("image.jpg", open(temp_path, "rb"), "image/jpeg")},
|
| 57 |
timeout=60
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# ------------------------------------------------
|
| 61 |
+
# ⭐ 4) 處理回應
|
| 62 |
+
# ------------------------------------------------
|
| 63 |
if resp.status_code != 200:
|
| 64 |
print(f"VLM API error: {resp.status_code}, {resp.text}")
|
| 65 |
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
|
| 66 |
|
| 67 |
+
# 正常解析內容
|
| 68 |
try:
|
| 69 |
+
content = resp.json()["choices"][0]["message"]["content"]
|
| 70 |
+
# content 是 array,找出 text
|
| 71 |
+
vlm_text = ""
|
| 72 |
+
for part in content:
|
| 73 |
+
if part.get("type") == "text":
|
| 74 |
+
vlm_text += part["text"]
|
| 75 |
+
except Exception as e:
|
| 76 |
return {"error": f"Failed to parse VLM response: {e}, Response text: {resp.text}"}
|
| 77 |
|
| 78 |
return {
|
| 79 |
"received": True,
|
| 80 |
"robot_id": robot_id,
|
| 81 |
+
"vllm_analysis": vlm_text.strip()
|
| 82 |
}
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
print(f"Unexpected error: {e}")
|
| 86 |
return {"error": str(e)}
|
| 87 |
|
| 88 |
# -------------------------------
|