OppaAI commited on
Commit
5a76b6b
·
verified ·
1 Parent(s): baa64dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -1,28 +1,27 @@
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": [
@@ -30,7 +29,7 @@ def process(payload: dict):
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,33 +38,29 @@ def process(payload: dict):
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,
56
  "robot_id": robot_id,
57
- "vllm_analysis": vlm_text
58
  }
59
 
60
  except Exception as e:
61
  return {"error": str(e)}
62
 
63
- # -------------------------------
64
- # Gradio MCP Server
65
- # -------------------------------
66
  demo = gr.Interface(
67
  fn=process,
68
- inputs=gr.JSON(label="Input Payload (Dict format)"),
69
  outputs=gr.JSON(label="Reply to Jetson"),
70
  api_name="predict"
71
  )
 
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 = os.environ.get("HF_CV_ROBOT_TOKEN")
9
  MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
10
 
 
 
 
 
 
 
11
  def process(payload: dict):
12
  try:
 
 
 
 
13
  image_b64 = payload["image_b64"]
14
+ robot_id = payload.get("robot_id", "unknown")
15
+
16
+ # Base64 → Bytes
17
+ img_bytes = base64.b64decode(image_b64)
18
+
19
+ # multipart file
20
+ files = {
21
+ "file": ("image.jpg", BytesIO(img_bytes), "image/jpeg")
22
+ }
23
 
24
+ # Router 要求的 payload 格式(不含 image_data
25
  data = {
26
  "model": MODEL,
27
  "messages": [
 
29
  "role": "user",
30
  "content": [
31
  {"type": "text", "text": "Describe this image in detail."},
32
+ {"type": "file", "file": "image.jpg"}
33
  ]
34
  }
35
  ]
 
38
  resp = requests.post(
39
  "https://router.huggingface.co/v1/chat/completions",
40
  headers={"Authorization": f"Bearer {HF_TOKEN}"},
41
+ data={"payload": json.dumps(data)},
42
+ files=files,
43
  timeout=60
44
  )
45
 
46
  if resp.status_code != 200:
47
+ return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
48
 
49
+ out = resp.json()
50
+ txt = out["choices"][0]["message"]["content"][0]["text"]
 
 
51
 
52
  return {
53
  "received": True,
54
  "robot_id": robot_id,
55
+ "vllm_analysis": txt
56
  }
57
 
58
  except Exception as e:
59
  return {"error": str(e)}
60
 
 
 
 
61
  demo = gr.Interface(
62
  fn=process,
63
+ inputs=gr.JSON(label="Input Payload (Dict)"),
64
  outputs=gr.JSON(label="Reply to Jetson"),
65
  api_name="predict"
66
  )