OppaAI commited on
Commit
f6526ec
·
verified ·
1 Parent(s): 6c10eb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -5,23 +5,39 @@ 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 = "ROBOT_MCP_TOKEN" # API 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:
 
5
  import json
6
  import requests
7
 
8
+ OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
9
+ OPENROUTER_KEY = "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" # OpenRouter key
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
+ img_b64 = base64.b64encode(img_bytes).decode("utf-8")
17
 
18
+ headers = {"Authorization": f"Bearer {OPENROUTER_KEY}"}
19
+ payload = {
20
+ "model": "qwen/qwen2.5-vl-32b-instruct:free",
21
+ "messages": [
22
+ {
23
+ "role": "user",
24
+ "content": [
25
+ {"type": "input_text", "text": "Describe the image in detail."},
26
+ {"type": "input_image", "image_base64": img_b64}
27
+ ]
28
+ }
29
+ ],
30
+ "temperature": 0.2
31
+ }
32
+
33
+ resp = requests.post(OPENROUTER_URL, headers=headers, json=payload, timeout=60)
34
  if resp.status_code == 200:
35
+ try:
36
+ return resp.json()["choices"][0]["message"]["content"][0]["text"]
37
+ except Exception:
38
+ return "Error parsing VLM response"
39
  else:
40
+ return f"OpenRouter API error: {resp.status_code}"
41
 
42
  def process(payload: dict):
43
  try: