Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,38 +2,72 @@ import gradio as gr
|
|
| 2 |
from api import get_all_questions, submit_answers, get_file
|
| 3 |
from agent import answer_question
|
| 4 |
from tools.file_loader import read_pdf, read_csv, read_txt
|
|
|
|
| 5 |
|
| 6 |
qa_data = []
|
| 7 |
answers = []
|
| 8 |
|
| 9 |
def run_agent():
|
| 10 |
global qa_data, answers
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def handle_submit(username, code_link):
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
-
gr.Markdown("#
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
username = gr.Textbox(label="Hugging Face Username")
|
|
@@ -41,11 +75,11 @@ with gr.Blocks() as demo:
|
|
| 41 |
|
| 42 |
status = gr.Textbox(label="Status", lines=4)
|
| 43 |
|
| 44 |
-
run_btn = gr.Button("
|
| 45 |
-
submit_btn = gr.Button("
|
| 46 |
|
| 47 |
run_btn.click(fn=run_agent, outputs=status)
|
| 48 |
submit_btn.click(fn=handle_submit, inputs=[username, code_link], outputs=status)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 2 |
from api import get_all_questions, submit_answers, get_file
|
| 3 |
from agent import answer_question
|
| 4 |
from tools.file_loader import read_pdf, read_csv, read_txt
|
| 5 |
+
import traceback
|
| 6 |
|
| 7 |
qa_data = []
|
| 8 |
answers = []
|
| 9 |
|
| 10 |
def run_agent():
|
| 11 |
global qa_data, answers
|
| 12 |
+
try:
|
| 13 |
+
print("[INFO] Fetching questions from GAIA API...")
|
| 14 |
+
qa_data = get_all_questions()
|
| 15 |
+
answers = []
|
| 16 |
+
|
| 17 |
+
print(f"[INFO] Total questions retrieved: {len(qa_data)}")
|
| 18 |
+
|
| 19 |
+
for q in qa_data:
|
| 20 |
+
task_id = q.get('task_id')
|
| 21 |
+
question = q.get('question')
|
| 22 |
+
file_text = None
|
| 23 |
+
|
| 24 |
+
if q.get("files"):
|
| 25 |
+
for fname in q["files"]:
|
| 26 |
+
try:
|
| 27 |
+
raw = get_file(task_id, fname)
|
| 28 |
+
print(f"[INFO] Loaded file: {fname}")
|
| 29 |
+
|
| 30 |
+
if fname.endswith(".pdf"):
|
| 31 |
+
file_text = read_pdf(raw)
|
| 32 |
+
elif fname.endswith(".csv"):
|
| 33 |
+
file_text = read_csv(raw)
|
| 34 |
+
elif fname.endswith(".txt"):
|
| 35 |
+
file_text = read_txt(raw)
|
| 36 |
+
else:
|
| 37 |
+
print(f"[WARN] Unsupported file type: {fname}")
|
| 38 |
+
except Exception as fe:
|
| 39 |
+
print(f"[ERROR] Failed to load file {fname}: {str(fe)}")
|
| 40 |
+
break # Load only the first file
|
| 41 |
+
|
| 42 |
+
print(f"[INFO] Answering question: {question}")
|
| 43 |
+
response = answer_question(question, file_context=file_text, do_search=True)
|
| 44 |
+
print(f"[INFO] Answer: {response}")
|
| 45 |
+
|
| 46 |
+
answers.append({
|
| 47 |
+
"task_id": task_id,
|
| 48 |
+
"submitted_answer": response
|
| 49 |
+
})
|
| 50 |
+
|
| 51 |
+
return f"✅ Agent answered {len(answers)} questions. Ready to submit!"
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
error_log = traceback.format_exc()
|
| 55 |
+
print("[ERROR]", error_log)
|
| 56 |
+
return f"❌ Error:\n{error_log}"
|
| 57 |
|
| 58 |
def handle_submit(username, code_link):
|
| 59 |
+
try:
|
| 60 |
+
print(f"[INFO] Submitting answers for user: {username}")
|
| 61 |
+
result = submit_answers(username, code_link, answers)
|
| 62 |
+
print(f"[INFO] Submission response: {result}")
|
| 63 |
+
return result
|
| 64 |
+
except Exception as e:
|
| 65 |
+
error_log = traceback.format_exc()
|
| 66 |
+
print("[ERROR]", error_log)
|
| 67 |
+
return f" Submission failed:\n{error_log}"
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("# GAIA Level 1 Agent")
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
username = gr.Textbox(label="Hugging Face Username")
|
|
|
|
| 75 |
|
| 76 |
status = gr.Textbox(label="Status", lines=4)
|
| 77 |
|
| 78 |
+
run_btn = gr.Button("Run Agent")
|
| 79 |
+
submit_btn = gr.Button(" Submit Answers")
|
| 80 |
|
| 81 |
run_btn.click(fn=run_agent, outputs=status)
|
| 82 |
submit_btn.click(fn=handle_submit, inputs=[username, code_link], outputs=status)
|
| 83 |
|
| 84 |
+
# Make sure this launches in Spaces
|
| 85 |
+
demo.launch()
|