OppaAI commited on
Commit
23f2922
·
verified ·
1 Parent(s): 5e39a23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -49
app.py CHANGED
@@ -5,93 +5,77 @@ 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" # 確認此模型有支援 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:
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
- # ------------------------------------------------
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": [
40
- {
41
- "role": "user",
42
- "content": [
43
- {"type": "text", "text": "Describe this image in detail."}
44
- ]
45
- }
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
- # -------------------------------
89
- # Gradio MCP Server
90
- # -------------------------------
91
  demo = gr.Interface(
92
  fn=process,
93
- inputs=gr.JSON(label="Input Payload (Dict format)"),
94
- outputs=gr.JSON(label="Reply to Jetson"),
95
  api_name="predict"
96
  )
97
 
 
5
  import requests
6
  import os
7
 
 
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
  def process(payload: dict):
15
  try:
16
  if not HF_TOKEN:
17
+ return {"error": "Missing HF token"}
18
+
19
  robot_id = payload.get("robot_id", "unknown")
20
  image_b64 = payload["image_b64"]
21
 
22
+ # Save Base64 → temp file
 
 
23
  img_bytes = base64.b64decode(image_b64)
24
  temp_path = "temp.jpg"
25
  with open(temp_path, "wb") as f:
26
  f.write(img_bytes)
27
 
28
+ # HF Router expects:
29
+ # data = model + messages (string)
30
+ # files = file
31
+ messages_json = json.dumps([
32
+ {
33
+ "role": "user",
34
+ "content": [
35
+ {"type": "text", "text": "Describe this image in detail."}
36
+ ]
37
+ }
38
+ ])
 
 
 
39
 
 
 
 
40
  resp = requests.post(
41
  "https://router.huggingface.co/v1/chat/completions",
42
  headers={"Authorization": f"Bearer {HF_TOKEN}"},
43
+ data={
44
+ "model": MODEL, # <- 放在這裡才對!
45
+ "messages": messages_json # <- 必須是字串
46
+ },
47
+ files={
48
+ "file": ("image.jpg", open(temp_path, "rb"), "image/jpeg")
49
+ },
50
  timeout=60
51
  )
52
 
 
 
 
53
  if resp.status_code != 200:
54
  print(f"VLM API error: {resp.status_code}, {resp.text}")
55
  return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
56
 
57
+ # Parse
58
+ out = resp.json()
59
+ parts = out["choices"][0]["message"]["content"]
60
+
61
+ text = ""
62
+ for p in parts:
63
+ if p["type"] == "text":
64
+ text += p["text"]
 
 
65
 
66
  return {
67
  "received": True,
68
  "robot_id": robot_id,
69
+ "vllm_analysis": text
70
  }
71
 
72
  except Exception as e:
 
73
  return {"error": str(e)}
74
 
 
 
 
75
  demo = gr.Interface(
76
  fn=process,
77
+ inputs=gr.JSON(label="Input Payload"),
78
+ outputs=gr.JSON(label="Reply"),
79
  api_name="predict"
80
  )
81