anky2002 commited on
Commit
6845e7a
·
verified ·
1 Parent(s): 8a218d4

Upload agents/semantic_agent.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. agents/semantic_agent.py +11 -7
agents/semantic_agent.py CHANGED
@@ -18,7 +18,7 @@ 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
- """Call VLM with deterministic timeout and single retry."""
22
  try:
23
  from openai import OpenAI
24
  except ImportError: return None
@@ -28,7 +28,7 @@ def _vlm(img, sys_prompt, user_prompt):
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=[
@@ -39,8 +39,9 @@ def _vlm(img, sys_prompt, 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",
@@ -50,10 +51,13 @@ def _vlm(img, sys_prompt, user_prompt):
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):
 
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 generous timeout and retry for cold-start."""
22
  try:
23
  from openai import OpenAI
24
  except ImportError: return None
 
28
  client=OpenAI(
29
  base_url="https://router.huggingface.co/v1",
30
  api_key=token,
31
+ timeout=90.0, # 90s 72B model needs time for cold start
32
  )
33
  b64=_b64(img)
34
  messages=[
 
39
  ]}
40
  ]
41
 
42
+ # Try up to 3 times with exponential backoff (cold start can take 30s+)
43
+ last_error = None
44
+ for attempt in range(3):
45
  try:
46
  resp=client.chat.completions.create(
47
  model="Qwen/Qwen2.5-VL-72B-Instruct",
 
51
  )
52
  return resp.choices[0].message.content
53
  except Exception as e:
54
+ last_error = e
55
+ if attempt < 2:
56
+ import time
57
+ wait = 3 * (attempt + 1) # 3s, 6s backoff
58
+ time.sleep(wait)
59
  continue
60
+ return f"VLM_ERROR: {last_error}"
61
  return "VLM_ERROR: exhausted retries"
62
 
63
  def _parse(text):