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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -48
app.py CHANGED
@@ -1,67 +1,38 @@
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": [
28
- {
29
- "role": "user",
30
- "content": [
31
- {"type": "text", "text": "Describe this image in detail."},
32
- {"type": "file", "file": "image.jpg"}
33
- ]
34
- }
35
- ]
36
- }
37
-
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
  )
67
 
 
1
  import gradio as gr
2
  import json
3
  import base64
 
 
4
  import os
5
 
 
 
 
6
  def process(payload: dict):
7
  try:
8
  image_b64 = payload["image_b64"]
 
9
 
10
+ # Base64 → bytes
11
  img_bytes = base64.b64decode(image_b64)
12
 
13
+ # Save to local file
14
+ tmp_path = "tmp.jpg"
15
+ with open(tmp_path, "wb") as f:
16
+ f.write(img_bytes)
17
+
18
+ # Check file exists + return success
19
+ if os.path.exists(tmp_path):
20
+ size = os.path.getsize(tmp_path)
21
+ return {
22
+ "saved": True,
23
+ "path": tmp_path,
24
+ "file_size_bytes": size
25
+ }
26
+ else:
27
+ return {"saved": False, "error": "File not found after save."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  except Exception as e:
30
+ return {"saved": False, "error": str(e)}
31
 
32
  demo = gr.Interface(
33
  fn=process,
34
+ inputs=gr.JSON(label="Input Payload (Dict format)"),
35
+ outputs=gr.JSON(label="Reply"),
36
  api_name="predict"
37
  )
38