Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,39 @@ import io
|
|
| 5 |
import json
|
| 6 |
import requests
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 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 {
|
| 18 |
-
payload = {
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if resp.status_code == 200:
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
else:
|
| 24 |
-
return f"
|
| 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:
|