kenqia commited on
Commit
09b4571
·
verified ·
1 Parent(s): 532e8f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -16
app.py CHANGED
@@ -321,13 +321,33 @@ class BasicAgent:
321
  return {
322
  "messages": [AIMessage(content=final_answer)]
323
  }
324
-
325
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  def answer_question(self, question: str, task_id: str | None = None) -> str:
327
  file_info = None
 
328
 
329
  if task_id:
330
  info_str = download_task_file.invoke({"task_id": task_id})
 
331
  try:
332
  file_info = json.loads(info_str)
333
  except Exception:
@@ -337,34 +357,45 @@ class BasicAgent:
337
  suffix = file_info.get("suffix", "").lower()
338
  file_path = file_info["file_path"]
339
 
340
- if suffix in [".png", ".jpg", ".jpeg", ".webp"]:
341
- return answer_image_question.invoke({
 
 
 
 
 
 
 
 
 
 
 
 
 
342
  "file_path": file_path,
343
  "question": question
344
  })
 
345
 
346
- if suffix in [".mp3", ".wav", ".m4a", ".aac", ".flac", ".ogg", ".opus", ".webm"]:
347
- return answer_audio_question.invoke({
348
  "file_path": file_path,
349
  "question": question
350
  })
 
351
 
352
- if suffix == ".py":
353
- return answer_python_question.invoke({
354
  "file_path": file_path
355
  })
 
356
 
357
- if suffix in [".xlsx", ".xls", ".csv"]:
358
  raw = answer_excel_question.invoke({
359
  "file_path": file_path,
360
  "question": question
361
  })
362
- result = self.react_graph.invoke({
363
- "messages": [
364
- HumanMessage(content=f"Question:\n{question}\n\nTool result:\n{raw}\n\nReturn only the final answer.")
365
- ]
366
- })
367
- return result["messages"][-1].content
368
 
369
  user_content = question
370
  if task_id:
@@ -374,7 +405,6 @@ class BasicAgent:
374
  return result["messages"][-1].content
375
 
376
 
377
-
378
  def run_and_submit_all( profile: gr.OAuthProfile | None):
379
  """
380
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
321
  return {
322
  "messages": [AIMessage(content=final_answer)]
323
  }
324
+ def format_final_answer(self, question: str, raw_answer: str) -> str:
325
+ response = self.model.invoke([
326
+ SystemMessage(content="""
327
+ You are an exact-match answer formatter.
328
+ Return only the final answer.
329
+ No explanation.
330
+ No markdown.
331
+ No citations.
332
+ No extra punctuation.
333
+ Follow the requested format exactly.
334
+ If the question asks for a number, return only the number.
335
+ If the question asks for USD with two decimals, return only the number with two decimals.
336
+ If the question asks comma-separated values, use ", " between items.
337
+ If the question asks for algebraic notation, return only the move.
338
+ If the question says without abbreviations, expand abbreviations.
339
+ """),
340
+ HumanMessage(content=f"Question:\n{question}\n\nRaw answer:\n{raw_answer}")
341
+ ])
342
+ return response.content.strip()
343
+
344
  def answer_question(self, question: str, task_id: str | None = None) -> str:
345
  file_info = None
346
+ q = question.lower()
347
 
348
  if task_id:
349
  info_str = download_task_file.invoke({"task_id": task_id})
350
+ print(f"[file_info] {info_str}", flush=True)
351
  try:
352
  file_info = json.loads(info_str)
353
  except Exception:
 
357
  suffix = file_info.get("suffix", "").lower()
358
  file_path = file_info["file_path"]
359
 
360
+ is_image_q = any(x in q for x in [
361
+ "image", "picture", "screenshot", "chess position", "visual", "diagram", "shown in"
362
+ ])
363
+ is_audio_q = any(x in q for x in [
364
+ "audio", "recording", "mp3", "wav", "says", "say in response", "lecture"
365
+ ])
366
+ is_python_q = any(x in q for x in [
367
+ "python code", "attached python", "numeric output from the attached python"
368
+ ])
369
+ is_excel_q = any(x in q for x in [
370
+ "excel", "spreadsheet", "csv", "sales", "table contains"
371
+ ])
372
+
373
+ if suffix in [".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"] or is_image_q:
374
+ raw = answer_image_question.invoke({
375
  "file_path": file_path,
376
  "question": question
377
  })
378
+ return self.format_final_answer(question, raw)
379
 
380
+ if suffix in [".mp3", ".wav", ".m4a", ".aac", ".flac", ".ogg", ".opus", ".webm"] or is_audio_q:
381
+ raw = answer_audio_question.invoke({
382
  "file_path": file_path,
383
  "question": question
384
  })
385
+ return self.format_final_answer(question, raw)
386
 
387
+ if suffix == ".py" or is_python_q:
388
+ raw = answer_python_question.invoke({
389
  "file_path": file_path
390
  })
391
+ return self.format_final_answer(question, raw)
392
 
393
+ if suffix in [".xlsx", ".xls", ".csv"] or is_excel_q:
394
  raw = answer_excel_question.invoke({
395
  "file_path": file_path,
396
  "question": question
397
  })
398
+ return self.format_final_answer(question, raw)
 
 
 
 
 
399
 
400
  user_content = question
401
  if task_id:
 
405
  return result["messages"][-1].content
406
 
407
 
 
408
  def run_and_submit_all( profile: gr.OAuthProfile | None):
409
  """
410
  Fetches all questions, runs the BasicAgent on them, submits all answers,