OppaAI commited on
Commit
d117752
·
verified ·
1 Parent(s): 71865dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -34
app.py CHANGED
@@ -1,45 +1,20 @@
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
- HF_UPLOAD_URL = "https://huggingface.co/api/uploads"
12
-
13
- def upload_to_hf(bytes_data):
14
- """Upload image bytes to HF and return image_url."""
15
- resp = requests.post(
16
- HF_UPLOAD_URL,
17
- headers={"Authorization": f"Bearer {HF_TOKEN}"},
18
- files={"file": ("temp.jpg", bytes_data, "image/jpeg")}
19
- )
20
-
21
- if resp.status_code != 200:
22
- raise RuntimeError(f"HF upload failed: {resp.text}")
23
-
24
- url = resp.json()["url"]
25
- return url
26
-
27
-
28
  def process(payload: dict):
29
  try:
30
  if not HF_TOKEN:
31
- return {"error": "Missing HF token."}
32
 
33
  robot_id = payload.get("robot_id", "unknown")
34
-
35
- # --- get image bytes
36
  image_b64 = payload["image_b64"]
37
- img_bytes = base64.b64decode(image_b64)
38
 
39
- # --- upload to HF (get public URL)
40
- image_url = upload_to_hf(img_bytes)
41
-
42
- # --- VLM request (image_url only)
43
  data = {
44
  "model": MODEL,
45
  "messages": [
@@ -47,7 +22,10 @@ def process(payload: dict):
47
  "role": "user",
48
  "content": [
49
  {"type": "text", "text": "Describe this image in detail."},
50
- {"type": "image_url", "image_url": {"url": image_url}}
 
 
 
51
  ]
52
  }
53
  ]
@@ -55,7 +33,10 @@ def process(payload: dict):
55
 
56
  resp = requests.post(
57
  "https://router.huggingface.co/v1/chat/completions",
58
- headers={"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"},
 
 
 
59
  data=json.dumps(data),
60
  timeout=60
61
  )
@@ -64,24 +45,24 @@ def process(payload: dict):
64
  return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
65
 
66
  try:
67
- vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
 
68
  except:
69
- return {"error": f"Bad VLM response: {resp.text}"}
70
 
71
  return {
72
  "received": True,
73
  "robot_id": robot_id,
74
- "vllm_analysis": vlm_text
75
  }
76
 
77
  except Exception as e:
78
  return {"error": str(e)}
79
 
80
-
81
  demo = gr.Interface(
82
  fn=process,
83
- inputs=gr.JSON(label="Input Payload (Dict format)"),
84
- outputs=gr.JSON(label="Reply to Jetson"),
85
  api_name="predict"
86
  )
87
 
 
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.5-VL-7B-Instruct"
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def process(payload: dict):
11
  try:
12
  if not HF_TOKEN:
13
+ return {"error": "Missing token"}
14
 
15
  robot_id = payload.get("robot_id", "unknown")
 
 
16
  image_b64 = payload["image_b64"]
 
17
 
 
 
 
 
18
  data = {
19
  "model": MODEL,
20
  "messages": [
 
22
  "role": "user",
23
  "content": [
24
  {"type": "text", "text": "Describe this image in detail."},
25
+ {
26
+ "type": "image_data",
27
+ "image_data": {"b64": image_b64}
28
+ }
29
  ]
30
  }
31
  ]
 
33
 
34
  resp = requests.post(
35
  "https://router.huggingface.co/v1/chat/completions",
36
+ headers={
37
+ "Authorization": f"Bearer {HF_TOKEN}",
38
+ "Content-Type": "application/json"
39
+ },
40
  data=json.dumps(data),
41
  timeout=60
42
  )
 
45
  return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
46
 
47
  try:
48
+ content = resp.json()["choices"][0]["message"]["content"]
49
+ first = content[0]["text"]
50
  except:
51
+ return {"error": f"Bad response: {resp.text}"}
52
 
53
  return {
54
  "received": True,
55
  "robot_id": robot_id,
56
+ "vllm_analysis": first
57
  }
58
 
59
  except Exception as e:
60
  return {"error": str(e)}
61
 
 
62
  demo = gr.Interface(
63
  fn=process,
64
+ inputs=gr.JSON(label="Input Payload"),
65
+ outputs=gr.JSON(label="Reply"),
66
  api_name="predict"
67
  )
68