OppaAI commited on
Commit
0ef482f
·
verified ·
1 Parent(s): 3de8d94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -46
app.py CHANGED
@@ -1,65 +1,70 @@
 
 
1
  import base64
2
  import requests
3
- import json
4
- import io
5
- from PIL import Image
6
- import tempfile
7
- import time
8
 
9
- # ----------------------------
10
- # OpenRouter API 設定
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
- def send_image_to_vlm(b64_image: str):
17
- # 1️⃣ base64 -> PIL Image
18
- img_bytes = base64.b64decode(b64_image)
19
- img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
20
 
21
- # 2️⃣ 存成臨時檔
22
- with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp_file:
23
- img.save(tmp_file.name)
 
24
 
25
- # 3️⃣ payload,使用 file 上傳
26
- payload = {
27
- "model": MODEL_NAME,
 
 
 
 
28
  "messages": [
29
  {
30
  "role": "user",
31
  "content": [
32
- {"type": "text", "text": "Describe the content of this image in detail."},
33
- {"type": "file", "file": tmp_file.name} # 這裡上傳檔案
34
  ]
35
  }
36
  ]
37
  }
38
 
39
- headers = {
40
- "Authorization": f"Bearer {OPENROUTER_KEY}",
41
- "Content-Type": "application/json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  }
43
 
44
- # 4️⃣ 呼叫 API
45
- resp = requests.post(API_URL, headers=headers, data=json.dumps(payload), timeout=120)
46
- if resp.status_code == 200:
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
- if __name__ == "__main__":
60
- # 這裡放一張你抓到的 base64 測試
61
- with open("test.jpg", "rb") as f:
62
- b64_test = base64.b64encode(f.read()).decode("utf-8")
63
 
64
- analysis = send_image_to_vlm(b64_test)
65
- print("VLM 分析結果:", analysis)
 
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)