Spaces:
Sleeping
Sleeping
Create app/app.py
Browse files- app/app.py +73 -0
app/app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from agent import answer_question
|
| 4 |
+
from submit import submit_answers
|
| 5 |
+
from tools.file_loader import read_pdf, read_csv, read_txt
|
| 6 |
+
import os
|
| 7 |
+
import traceback
|
| 8 |
+
|
| 9 |
+
qa_data = []
|
| 10 |
+
answers = []
|
| 11 |
+
|
| 12 |
+
def run_agent():
|
| 13 |
+
global qa_data, answers
|
| 14 |
+
try:
|
| 15 |
+
status_lines = []
|
| 16 |
+
with open("data/metadata.jsonl", "r", encoding="utf-8") as f:
|
| 17 |
+
lines = f.readlines()[:20]
|
| 18 |
+
qa_data = [json.loads(line) for line in lines]
|
| 19 |
+
|
| 20 |
+
answers = []
|
| 21 |
+
|
| 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 = None
|
| 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 |
+
file_text = read_pdf(file_path)
|
| 33 |
+
elif file_name.endswith(".csv"):
|
| 34 |
+
file_text = read_csv(file_path)
|
| 35 |
+
elif file_name.endswith(".txt"):
|
| 36 |
+
file_text = read_txt(file_path)
|
| 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 "\n".join(status_lines)
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"❌ Error:\n{traceback.format_exc()}"
|
| 49 |
+
|
| 50 |
+
def handle_submit(username, code_link):
|
| 51 |
+
try:
|
| 52 |
+
result = submit_answers(username, code_link, answers)
|
| 53 |
+
return result
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"❌ Submission error:\n{traceback.format_exc()}"
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("# 🤖 GAIA Level 1 Agent (Local - Validation Set)")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
username = gr.Textbox(label="Your Hugging Face Username")
|
| 62 |
+
code_link = gr.Textbox(label="Your Space Code Link (/tree/main)")
|
| 63 |
+
|
| 64 |
+
status = gr.Textbox(label="Status", lines=15)
|
| 65 |
+
|
| 66 |
+
run_btn = gr.Button("🧠 Run Agent")
|
| 67 |
+
submit_btn = gr.Button("🚀 Submit Answers")
|
| 68 |
+
|
| 69 |
+
run_btn.click(fn=run_agent, outputs=status)
|
| 70 |
+
submit_btn.click(fn=handle_submit, inputs=[username, code_link], outputs=status)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch()
|