OppaAI commited on
Commit
4f14703
·
verified ·
1 Parent(s): f594945

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -1,16 +1,20 @@
1
  import gradio as gr
2
  import base64
3
- from PIL import Image
4
- import io
5
  import json
6
  import requests
7
 
8
- HF_VLM_API = "https://api-inference.huggingface.co/models/Qwen/Qwen2-VL-7B-Instruct"
9
- HF_TOKEN = "HF_CV_ROBOT_TOKEN" # HF Token
 
10
 
11
  def call_vlm_api(payload: dict):
 
 
 
12
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
13
  data = {
 
14
  "inputs": [
15
  {
16
  "image": {"b64": payload["image_b64"]},
@@ -18,16 +22,24 @@ def call_vlm_api(payload: dict):
18
  }
19
  ]
20
  }
21
- resp = requests.post(HF_VLM_API, headers=headers, json=data, timeout=60)
22
- if resp.status_code == 200:
23
- return resp.json()[0].get("generated_text", "")
24
- else:
25
- return f"VLM API error: {resp.status_code}, {resp.text}"
 
 
 
 
 
26
 
27
  def process(payload: dict):
 
 
 
 
28
  try:
29
  vlm_text = call_vlm_api(payload)
30
-
31
  reply = {
32
  "received": True,
33
  "robot_id": payload.get("robot_id", "unknown"),
@@ -37,10 +49,10 @@ def process(payload: dict):
37
  except Exception as e:
38
  return {"error": str(e)}
39
 
40
-
41
  demo = gr.Interface(
42
  fn=process,
43
- inputs=gr.JSON(label="Input Payload (Dict format)"),
44
  outputs=gr.JSON(label="Reply to Jetson"),
45
  api_name="predict"
46
  )
 
1
  import gradio as gr
2
  import base64
 
 
3
  import json
4
  import requests
5
 
6
+ HF_ROUTER_API = "https://router.huggingface.co/hf-inference"
7
+ HF_TOKEN = "HF_CV_ROBOT_TOKEN"
8
+ MODEL_NAME = "Qwen/Qwen2-VL-7B-Instruct"
9
 
10
  def call_vlm_api(payload: dict):
11
+ """
12
+ Call Hugging Face Router Inference API with Base64 image.
13
+ """
14
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
15
+
16
  data = {
17
+ "model": MODEL_NAME,
18
  "inputs": [
19
  {
20
  "image": {"b64": payload["image_b64"]},
 
22
  }
23
  ]
24
  }
25
+
26
+ try:
27
+ resp = requests.post(HF_ROUTER_API, headers=headers, json=data, timeout=60)
28
+ if resp.status_code == 200:
29
+ # 取第一個 generated_text
30
+ return resp.json()[0].get("generated_text", "")
31
+ else:
32
+ return f"VLM API error: {resp.status_code}, {resp.text}"
33
+ except Exception as e:
34
+ return f"Exception: {str(e)}"
35
 
36
  def process(payload: dict):
37
+ """
38
+ Process JSON payload from Jetson: Base64 image + robot_id
39
+ Return JSON with VLM analysis
40
+ """
41
  try:
42
  vlm_text = call_vlm_api(payload)
 
43
  reply = {
44
  "received": True,
45
  "robot_id": payload.get("robot_id", "unknown"),
 
49
  except Exception as e:
50
  return {"error": str(e)}
51
 
52
+ # Gradio MCP server
53
  demo = gr.Interface(
54
  fn=process,
55
+ inputs=gr.JSON(label="Input Payload from Jetson"),
56
  outputs=gr.JSON(label="Reply to Jetson"),
57
  api_name="predict"
58
  )