gabejavitt commited on
Commit
e8bd4b1
Β·
verified Β·
1 Parent(s): 1c6d6d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -1111,7 +1111,7 @@ REMEMBER: One tool per turn. No reasoning without tools. Exact answer format.
1111
  self.graph = graph_builder.compile()
1112
  print("βœ… Graph compiled successfully.")
1113
 
1114
- def __call__(self, question: str) -> str:
1115
  """Execute agent on a question."""
1116
  print(f"\n{'='*70}")
1117
  print(f"🎯 NEW QUESTION")
@@ -1122,8 +1122,9 @@ REMEMBER: One tool per turn. No reasoning without tools. Exact answer format.
1122
  graph_input = {
1123
  "messages": [
1124
  SystemMessage(content=self.system_prompt),
1125
- HumanMessage(content=question)
1126
  ],
 
1127
  "turn": 0,
1128
  "has_plan": False,
1129
  "consecutive_errors": 0,
@@ -1321,25 +1322,33 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
1321
  for item in questions_data:
1322
  task_id = item.get("task_id")
1323
  question_text = item.get("question")
1324
- # Download attached files if they exist
1325
- # Handle file attachments
1326
- file_info = ""
1327
- if item.get("file_path"):
1328
- file_path = item["file_path"]
1329
- file_info = f"\n\n[Note: Question references file: {file_path}]"
 
 
 
 
 
1330
 
1331
- # Check if file exists
1332
- if not os.path.exists(file_path):
1333
- print(f"⚠️ Warning: File {file_path} not found!")
1334
- file_info += f"\n[WARNING: File not found in current directory]"
 
 
 
1335
 
1336
  if not task_id or question_text is None:
1337
  print(f"Skipping item with missing task_id or question: {item}")
1338
  continue
1339
  try:
1340
  question_with_context = question_text + file_info
1341
- submitted_answer = agent(question_with_context)
1342
- #submitted_answer = agent(question_text)
1343
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
1344
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
1345
  except Exception as e:
 
1111
  self.graph = graph_builder.compile()
1112
  print("βœ… Graph compiled successfully.")
1113
 
1114
+ def __call__(self, question: str, file_path: str = None) -> str:
1115
  """Execute agent on a question."""
1116
  print(f"\n{'='*70}")
1117
  print(f"🎯 NEW QUESTION")
 
1122
  graph_input = {
1123
  "messages": [
1124
  SystemMessage(content=self.system_prompt),
1125
+ HumanMessage(content=question + (f"\n\n[FILE ATTACHED: {file_path}]" if file_path else ""))
1126
  ],
1127
+ "file_path": file_path, # Add this to the graph state,
1128
  "turn": 0,
1129
  "has_plan": False,
1130
  "consecutive_errors": 0,
 
1322
  for item in questions_data:
1323
  task_id = item.get("task_id")
1324
  question_text = item.get("question")
1325
+ file_path_from_api = item["file_path"]
1326
+ file_download_url = f"{DEFAULT_API_URL}/files/{task_id}"
1327
+ file_info = []
1328
+ # Extract the original file name to preserve the extension (e.g., .pdf, .jpg)
1329
+ original_filename = file_path_from_api.split('/')[-1]
1330
+
1331
+ # 🌟 NEW: Save the file to the /tmp directory
1332
+ local_file_path = os.path.join("/tmp", original_filename)
1333
+ try:
1334
+ file_response = requests.get(file_download_url, timeout=15)
1335
+ file_response.raise_for_status()
1336
 
1337
+ # 🌟 CRITICAL: Write the raw bytes content to the local file path
1338
+ with open(local_file_path, 'wb') as f:
1339
+ f.write(file_response.content)
1340
+ print(f"βœ… Downloaded file to: {local_file_path}")
1341
+ file_info = local_file_path
1342
+ except requests.exceptions.RequestException as e:
1343
+ error_message = f"[FILE DOWNLOAD ERROR: Could not fetch file from {file_download_url}: {e}]"
1344
 
1345
  if not task_id or question_text is None:
1346
  print(f"Skipping item with missing task_id or question: {item}")
1347
  continue
1348
  try:
1349
  question_with_context = question_text + file_info
1350
+ #submitted_answer = agent(question_with_context)
1351
+ submitted_answer = agent(question_text, local_file_path if item.get("file_path") else None)
1352
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
1353
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
1354
  except Exception as e: