OppaAI commited on
Commit
f594945
·
verified ·
1 Parent(s): 20f710b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -8,38 +8,36 @@ import requests
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(img: Image):
12
- # encode image to bytes
13
- buf = io.BytesIO()
14
- img.save(buf, format="JPEG")
15
- img_bytes = buf.getvalue()
16
-
17
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
18
- payload = {"inputs": [{"image": img_bytes, "text": "Describe the image in detail."}]}
19
-
20
- resp = requests.post(HF_VLM_API, headers=headers, json=payload, timeout=60)
 
 
 
 
 
 
21
  if resp.status_code == 200:
22
  return resp.json()[0].get("generated_text", "")
23
  else:
24
- return f"VLM API error: {resp.status_code}"
25
 
26
  def process(payload: dict):
27
  try:
28
- img_bytes = base64.b64decode(payload["image_b64"])
29
- img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
30
-
31
- vlm_text = call_vlm_api(img)
32
 
33
  reply = {
34
  "received": True,
35
  "robot_id": payload.get("robot_id", "unknown"),
36
- "size": img.size,
37
  "vllm_analysis": vlm_text
38
  }
39
  return reply
40
  except Exception as e:
41
  return {"error": str(e)}
42
 
 
43
  demo = gr.Interface(
44
  fn=process,
45
  inputs=gr.JSON(label="Input Payload (Dict format)"),
 
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"]},
17
+ "text": "Describe the image in detail."
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"),
 
34
  "vllm_analysis": vlm_text
35
  }
36
  return reply
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)"),