Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,36 @@
|
|
| 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"
|
| 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:
|
| 20 |
if not HF_TOKEN:
|
| 21 |
-
return {"error": "Hugging Face token
|
| 22 |
|
| 23 |
robot_id = payload.get("robot_id", "unknown")
|
| 24 |
image_b64 = payload["image_b64"]
|
| 25 |
|
| 26 |
-
#
|
| 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":
|
|
|
|
| 55 |
]
|
| 56 |
}
|
| 57 |
]
|
|
@@ -60,17 +39,17 @@ def process(payload: dict):
|
|
| 60 |
resp = requests.post(
|
| 61 |
"https://router.huggingface.co/v1/chat/completions",
|
| 62 |
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 63 |
-
|
| 64 |
timeout=60
|
| 65 |
)
|
| 66 |
|
| 67 |
if resp.status_code != 200:
|
| 68 |
-
return {"error": f"VLM
|
| 69 |
|
| 70 |
try:
|
| 71 |
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 72 |
-
except
|
| 73 |
-
return {"error": f"Failed to parse VLM response: {e}, Response
|
| 74 |
|
| 75 |
return {
|
| 76 |
"received": True,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import base64
|
|
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
| 7 |
# HF token & model
|
| 8 |
HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
|
| 9 |
+
MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 10 |
|
| 11 |
if not HF_TOKEN:
|
| 12 |
print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
|
| 13 |
|
| 14 |
# -------------------------------
|
| 15 |
+
# Main Processing Function
|
| 16 |
# -------------------------------
|
| 17 |
def process(payload: dict):
|
| 18 |
try:
|
| 19 |
if not HF_TOKEN:
|
| 20 |
+
return {"error": "Hugging Face token missing. Check environment variable."}
|
| 21 |
|
| 22 |
robot_id = payload.get("robot_id", "unknown")
|
| 23 |
image_b64 = payload["image_b64"]
|
| 24 |
|
| 25 |
+
# 正確格式:直接把 base64 放入 image_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
]
|
|
|
|
| 39 |
resp = requests.post(
|
| 40 |
"https://router.huggingface.co/v1/chat/completions",
|
| 41 |
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 42 |
+
json=data,
|
| 43 |
timeout=60
|
| 44 |
)
|
| 45 |
|
| 46 |
if resp.status_code != 200:
|
| 47 |
+
return {"error": f"HF VLM error: {resp.status_code}, {resp.text}"}
|
| 48 |
|
| 49 |
try:
|
| 50 |
vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return {"error": f"Failed to parse VLM response: {e}, Response={resp.text}"}
|
| 53 |
|
| 54 |
return {
|
| 55 |
"received": True,
|