Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,29 +22,31 @@ def run_agent():
|
|
| 22 |
for idx, q in enumerate(qa_data, start=1):
|
| 23 |
task_id = q.get("task_id")
|
| 24 |
question = q.get("question")
|
| 25 |
-
file_text =
|
| 26 |
|
| 27 |
file_name = q.get("file_name", "")
|
| 28 |
if file_name:
|
| 29 |
file_path = os.path.join("data", file_name)
|
| 30 |
try:
|
| 31 |
if file_name.endswith(".pdf"):
|
| 32 |
-
|
|
|
|
| 33 |
elif file_name.endswith(".csv"):
|
| 34 |
-
|
|
|
|
| 35 |
elif file_name.endswith(".txt"):
|
| 36 |
-
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
status_lines.append(f"⚠️ Failed to read file {file_name}: {e}")
|
| 39 |
|
| 40 |
-
response = answer_question(question, file_context=file_text, do_search=False)
|
| 41 |
answers.append({"task_id": task_id, "submitted_answer": response})
|
| 42 |
status_lines.append(f"✅ Q{idx}: Answered")
|
| 43 |
|
| 44 |
-
status_lines.append(f"\n🎯 Done. {len(answers)} questions answered.")
|
| 45 |
return "🎯 Done.\n\n" + "\n".join(
|
| 46 |
-
|
| 47 |
-
)
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
return f"❌ Error:\n{traceback.format_exc()}"
|
|
|
|
| 22 |
for idx, q in enumerate(qa_data, start=1):
|
| 23 |
task_id = q.get("task_id")
|
| 24 |
question = q.get("question")
|
| 25 |
+
file_text = ""
|
| 26 |
|
| 27 |
file_name = q.get("file_name", "")
|
| 28 |
if file_name:
|
| 29 |
file_path = os.path.join("data", file_name)
|
| 30 |
try:
|
| 31 |
if file_name.endswith(".pdf"):
|
| 32 |
+
with open(file_path, "rb") as f:
|
| 33 |
+
file_text = read_pdf(f.read())
|
| 34 |
elif file_name.endswith(".csv"):
|
| 35 |
+
with open(file_path, "rb") as f:
|
| 36 |
+
file_text = read_csv(f.read())
|
| 37 |
elif file_name.endswith(".txt"):
|
| 38 |
+
with open(file_path, "rb") as f:
|
| 39 |
+
file_text = read_txt(f.read())
|
| 40 |
except Exception as e:
|
| 41 |
status_lines.append(f"⚠️ Failed to read file {file_name}: {e}")
|
| 42 |
|
| 43 |
+
response = answer_question(question, file_context=file_text or "", do_search=False)
|
| 44 |
answers.append({"task_id": task_id, "submitted_answer": response})
|
| 45 |
status_lines.append(f"✅ Q{idx}: Answered")
|
| 46 |
|
|
|
|
| 47 |
return "🎯 Done.\n\n" + "\n".join(
|
| 48 |
+
[f"✅ Q{idx+1} (ID {a['task_id']}): {a['submitted_answer']}" for idx, a in enumerate(answers)]
|
| 49 |
+
)
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
return f"❌ Error:\n{traceback.format_exc()}"
|