Miguel Figueiredo Marques commited on
Commit
be9d412
·
1 Parent(s): 1079dc5

change model

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -22,9 +22,9 @@ load_dotenv()
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
  # Main reasoning + tool-calling model.
25
- MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
26
- # Vision model for image questions (gpt-4o-mini supports vision natively).
27
- VISION_MODEL = os.getenv("OPENAI_VISION_MODEL", "gpt-4o-mini")
28
  # Seconds to pause between questions (OpenAI limits are generous; 1s is enough).
29
  PER_QUESTION_DELAY = float(os.getenv("PER_QUESTION_DELAY", "1"))
30
 
@@ -248,7 +248,7 @@ class BasicAgent:
248
  messages = [SystemMessage(content=SYSTEM_PROMPT), HumanMessage(content=question)]
249
  for attempt in range(3):
250
  try:
251
- result = self.graph.invoke({"messages": messages}, {"recursion_limit": 30})
252
  answer = extract_final_answer(result["messages"][-1].content)
253
  print(f"Agent returning: {answer[:120]}")
254
  return answer
@@ -260,13 +260,13 @@ class BasicAgent:
260
  time.sleep(wait)
261
  continue
262
  if ("tool_use_failed" in msg or "tool call validation" in msg) and attempt < 1:
263
- # Model emitted malformed tool call syntax; retry once
264
  print(f"Malformed tool call on attempt {attempt + 1}; retrying...")
265
  time.sleep(5)
266
  continue
267
- if "tool_use_failed" in msg or "tool call validation" in msg:
268
- # Retries exhausted — fall back to a direct no-tools LLM call
269
- print("Tool calls keep failing; falling back to direct LLM (no tools).")
 
270
  try:
271
  llm = ChatOpenAI(model=MODEL_NAME, temperature=0, max_tokens=1024)
272
  direct = llm.invoke(messages)
@@ -280,16 +280,20 @@ class BasicAgent:
280
 
281
  def _download_task_file(api_url: str, task_id: str, file_name: str) -> str | None:
282
  """Download a file attached to a task into a temp dir. Returns the local path or None."""
283
- try:
284
- r = requests.get(f"{api_url}/files/{task_id}", timeout=60)
285
- r.raise_for_status()
286
- local = os.path.join(tempfile.gettempdir(), file_name or f"{task_id}.bin")
287
- with open(local, "wb") as f:
288
- f.write(r.content)
289
- return local
290
- except Exception as e:
291
- print(f"Could not download file for task {task_id}: {e}")
292
- return None
 
 
 
 
293
 
294
 
295
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
  # Main reasoning + tool-calling model.
25
+ MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-5.4-mini")
26
+ # Vision model for image questions (supports vision natively).
27
+ VISION_MODEL = os.getenv("OPENAI_VISION_MODEL", "gpt-5.4-mini")
28
  # Seconds to pause between questions (OpenAI limits are generous; 1s is enough).
29
  PER_QUESTION_DELAY = float(os.getenv("PER_QUESTION_DELAY", "1"))
30
 
 
248
  messages = [SystemMessage(content=SYSTEM_PROMPT), HumanMessage(content=question)]
249
  for attempt in range(3):
250
  try:
251
+ result = self.graph.invoke({"messages": messages}, {"recursion_limit": 50})
252
  answer = extract_final_answer(result["messages"][-1].content)
253
  print(f"Agent returning: {answer[:120]}")
254
  return answer
 
260
  time.sleep(wait)
261
  continue
262
  if ("tool_use_failed" in msg or "tool call validation" in msg) and attempt < 1:
 
263
  print(f"Malformed tool call on attempt {attempt + 1}; retrying...")
264
  time.sleep(5)
265
  continue
266
+ # Recursion limit or repeated tool failures — fall back to direct LLM
267
+ if ("recursion" in msg.lower() or "tool_use_failed" in msg
268
+ or "tool call validation" in msg):
269
+ print(f"Graph failed ({msg[:80]}); falling back to direct LLM (no tools).")
270
  try:
271
  llm = ChatOpenAI(model=MODEL_NAME, temperature=0, max_tokens=1024)
272
  direct = llm.invoke(messages)
 
280
 
281
  def _download_task_file(api_url: str, task_id: str, file_name: str) -> str | None:
282
  """Download a file attached to a task into a temp dir. Returns the local path or None."""
283
+ local = os.path.join(tempfile.gettempdir(), file_name or f"{task_id}.bin")
284
+ for attempt in range(3):
285
+ try:
286
+ r = requests.get(f"{api_url}/files/{task_id}", timeout=60)
287
+ r.raise_for_status()
288
+ with open(local, "wb") as f:
289
+ f.write(r.content)
290
+ print(f"Downloaded {file_name} → {local} ({len(r.content)} bytes)")
291
+ return local
292
+ except Exception as e:
293
+ print(f"Download attempt {attempt + 1} failed for task {task_id}: {e}")
294
+ if attempt < 2:
295
+ time.sleep(3)
296
+ return None
297
 
298
 
299
  def run_and_submit_all(profile: gr.OAuthProfile | None):