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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
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" # 確認這個模型在 HF 支援列表裡
11
 
12
  if not HF_TOKEN:
13
  print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
14
 
15
  # -------------------------------
16
- # 主處理函數 (Main Processing Function)
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
- # Base64 -> bytes
 
 
27
  img_bytes = base64.b64decode(image_b64)
 
 
 
28
 
29
- # 將 Base64 bytes 當作 file-like 上傳
30
- files = {
31
- "file": ("image.jpg", BytesIO(img_bytes), "image/jpeg")
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=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
- vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
62
- except (KeyError, IndexError, json.JSONDecodeError) as e:
 
 
 
 
 
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"An unexpected error occurred: {e}")
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
  # -------------------------------