Upload agents/semantic_agent.py with huggingface_hub
Browse files- agents/semantic_agent.py +32 -9
agents/semantic_agent.py
CHANGED
|
@@ -18,20 +18,43 @@ def _b64(img, mx=1024):
|
|
| 18 |
buf=io.BytesIO(); img.convert("RGB").save(buf,"JPEG",quality=90); return base64.b64encode(buf.getvalue()).decode()
|
| 19 |
|
| 20 |
def _vlm(img, sys_prompt, user_prompt):
|
|
|
|
| 21 |
try:
|
| 22 |
from openai import OpenAI
|
| 23 |
except ImportError: return None
|
| 24 |
token=os.environ.get("HF_TOKEN","")
|
| 25 |
if not token: return None
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def _parse(text):
|
| 37 |
if not text: return {}
|
|
|
|
| 18 |
buf=io.BytesIO(); img.convert("RGB").save(buf,"JPEG",quality=90); return base64.b64encode(buf.getvalue()).decode()
|
| 19 |
|
| 20 |
def _vlm(img, sys_prompt, user_prompt):
|
| 21 |
+
"""Call VLM with deterministic timeout and single retry."""
|
| 22 |
try:
|
| 23 |
from openai import OpenAI
|
| 24 |
except ImportError: return None
|
| 25 |
token=os.environ.get("HF_TOKEN","")
|
| 26 |
if not token: return None
|
| 27 |
+
|
| 28 |
+
client=OpenAI(
|
| 29 |
+
base_url="https://router.huggingface.co/v1",
|
| 30 |
+
api_key=token,
|
| 31 |
+
timeout=60.0, # Deterministic 60s timeout
|
| 32 |
+
)
|
| 33 |
+
b64=_b64(img)
|
| 34 |
+
messages=[
|
| 35 |
+
{"role":"system","content":sys_prompt},
|
| 36 |
+
{"role":"user","content":[
|
| 37 |
+
{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{b64}"}},
|
| 38 |
+
{"type":"text","text":user_prompt}
|
| 39 |
+
]}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# Try up to 2 times for deterministic behavior
|
| 43 |
+
for attempt in range(2):
|
| 44 |
+
try:
|
| 45 |
+
resp=client.chat.completions.create(
|
| 46 |
+
model="Qwen/Qwen2.5-VL-72B-Instruct",
|
| 47 |
+
messages=messages,
|
| 48 |
+
max_tokens=2000,
|
| 49 |
+
temperature=0.1,
|
| 50 |
+
)
|
| 51 |
+
return resp.choices[0].message.content
|
| 52 |
+
except Exception as e:
|
| 53 |
+
if attempt == 0:
|
| 54 |
+
import time; time.sleep(2) # Brief pause before retry
|
| 55 |
+
continue
|
| 56 |
+
return f"VLM_ERROR: {e}"
|
| 57 |
+
return "VLM_ERROR: exhausted retries"
|
| 58 |
|
| 59 |
def _parse(text):
|
| 60 |
if not text: return {}
|