OppaAI commited on
Commit
48607b7
·
verified ·
1 Parent(s): dd3451f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -6
app.py CHANGED
@@ -2,24 +2,88 @@ 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
- # 直接寫到 space sandbox
11
  tmp_path = "/tmp/tmp.jpg"
12
-
13
  with open(tmp_path, "wb") as f:
14
  f.write(base64.b64decode(image_b64))
15
-
16
- # 再讀取確認寫入成功
17
  file_size = os.path.getsize(tmp_path)
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  return {
20
  "saved": True,
21
  "file_path": tmp_path,
22
- "file_size_bytes": file_size
 
 
 
23
  }
24
 
25
  except Exception as e:
@@ -28,7 +92,7 @@ def process(payload: dict):
28
  demo = gr.Interface(
29
  fn=process,
30
  inputs=gr.JSON(label="Input Payload (Dict format)"),
31
- outputs=gr.JSON(label="Reply"),
32
  api_name="predict"
33
  )
34
 
 
2
  import json
3
  import base64
4
  import os
5
+ import requests
6
+ from huggingface_hub import HfApi, HfFolder
7
+
8
+ HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
9
+ MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
10
+ REPO_ID = "your-username/tmp-vlm-images" # Replace with your HF repo
11
+
12
+ if not HF_TOKEN:
13
+ print("ERROR: HF_CV_ROBOT_TOKEN environment variable not set.")
14
+
15
+ api = HfApi()
16
+ HfFolder.save_token(HF_TOKEN)
17
+
18
+ def upload_to_hf(filepath: str):
19
+ filename = os.path.basename(filepath)
20
+ # Upload to HF repo
21
+ api.upload_file(
22
+ path_or_fileobj=filepath,
23
+ path_in_repo=filename,
24
+ repo_id=REPO_ID,
25
+ repo_type="dataset",
26
+ token=HF_TOKEN,
27
+ overwrite=True
28
+ )
29
+ # Construct raw URL
30
+ url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{filename}"
31
+ return url
32
 
33
  def process(payload: dict):
34
  try:
35
+ if not HF_TOKEN:
36
+ return {"error": "Hugging Face token missing."}
37
+
38
+ robot_id = payload.get("robot_id", "unknown")
39
  image_b64 = payload["image_b64"]
40
 
41
+ # 1️⃣ Save the image locally
42
  tmp_path = "/tmp/tmp.jpg"
 
43
  with open(tmp_path, "wb") as f:
44
  f.write(base64.b64decode(image_b64))
 
 
45
  file_size = os.path.getsize(tmp_path)
46
 
47
+ # 2️⃣ Upload to HF to get URL
48
+ image_url = upload_to_hf(tmp_path)
49
+
50
+ # 3️⃣ Call VLM with image URL
51
+ data = {
52
+ "model": MODEL,
53
+ "messages": [
54
+ {
55
+ "role": "user",
56
+ "content": [
57
+ {"type": "text", "text": "Describe this image in detail."},
58
+ {"type": "image_url", "image_url": image_url}
59
+ ]
60
+ }
61
+ ]
62
+ }
63
+
64
+ resp = requests.post(
65
+ "https://router.huggingface.co/v1/chat/completions",
66
+ headers={"Authorization": f"Bearer {HF_TOKEN}"},
67
+ json=data,
68
+ timeout=60
69
+ )
70
+
71
+ if resp.status_code != 200:
72
+ vlm_text = f"HF VLM error: {resp.status_code}, {resp.text}"
73
+ else:
74
+ try:
75
+ vlm_text = resp.json()["choices"][0]["message"]["content"][0]["text"]
76
+ except Exception as e:
77
+ vlm_text = f"Failed to parse VLM response: {e}, Response={resp.text}"
78
+
79
+ # 4️⃣ Return combined info
80
  return {
81
  "saved": True,
82
  "file_path": tmp_path,
83
+ "file_size_bytes": file_size,
84
+ "robot_id": robot_id,
85
+ "vlm_description": vlm_text,
86
+ "image_url": image_url
87
  }
88
 
89
  except Exception as e:
 
92
  demo = gr.Interface(
93
  fn=process,
94
  inputs=gr.JSON(label="Input Payload (Dict format)"),
95
+ outputs=gr.JSON(label="Reply to Jetson"),
96
  api_name="predict"
97
  )
98