Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,70 @@
|
|
|
|
|
|
|
|
| 1 |
import base64
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
-
import io
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import tempfile
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# ----------------------------
|
| 12 |
-
OPENROUTER_KEY = "YOUR_OPENROUTER_API_KEY"
|
| 13 |
-
MODEL_NAME = "qwen/qwen3-vl-32b-instruct"
|
| 14 |
-
API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
img_bytes = base64.b64decode(b64_image)
|
| 19 |
-
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"messages": [
|
| 29 |
{
|
| 30 |
"role": "user",
|
| 31 |
"content": [
|
| 32 |
-
{"type": "text", "text": "Describe
|
| 33 |
-
{"type": "file", "file":
|
| 34 |
]
|
| 35 |
}
|
| 36 |
]
|
| 37 |
}
|
| 38 |
|
| 39 |
-
|
| 40 |
-
"
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
result = resp.json()
|
| 48 |
-
# 回傳生成的文字
|
| 49 |
-
try:
|
| 50 |
-
return result["choices"][0]["message"]["content"][0]["text"]
|
| 51 |
-
except:
|
| 52 |
-
return str(result)
|
| 53 |
-
else:
|
| 54 |
-
return f"VLM API error {resp.status_code}: {resp.text}"
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
b64_test = base64.b64encode(f.read()).decode("utf-8")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 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-VL-7B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
if not HF_TOKEN:
|
| 11 |
+
print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
|
|
|
|
|
|
|
| 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": [
|
| 26 |
{
|
| 27 |
"role": "user",
|
| 28 |
"content": [
|
| 29 |
+
{"type": "text", "text": "Describe this image in detail."},
|
| 30 |
+
{"type": "file", "file": image_b64} # <- 這裡用 file 直接放 base64
|
| 31 |
]
|
| 32 |
}
|
| 33 |
]
|
| 34 |
}
|
| 35 |
|
| 36 |
+
resp = requests.post(
|
| 37 |
+
"https://router.huggingface.co/v1/chat/completions",
|
| 38 |
+
headers=headers,
|
| 39 |
+
json=data,
|
| 40 |
+
timeout=60
|
| 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:
|
| 50 |
+
return {"error": f"Failed to parse VLM response: {e}, Response text: {resp.text}"}
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"received": True,
|
| 54 |
+
"robot_id": robot_id,
|
| 55 |
+
"vllm_analysis": vlm_text
|
| 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)"),
|
| 65 |
+
outputs=gr.JSON(label="Reply to Jetson"),
|
| 66 |
+
api_name="predict"
|
| 67 |
+
)
|
|
|
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch(mcp_server=True)
|