File size: 2,489 Bytes
52315f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import json
from agent import answer_question
from submit import submit_answers
from tools.file_loader import read_pdf, read_csv, read_txt
import os
import traceback

qa_data = []
answers = []

def run_agent():
    global qa_data, answers
    try:
        status_lines = []
        with open("data/metadata.jsonl", "r", encoding="utf-8") as f:
            lines = f.readlines()[:20]
            qa_data = [json.loads(line) for line in lines]

        answers = []

        for idx, q in enumerate(qa_data, start=1):
            task_id = q.get("task_id")
            question = q.get("question")
            file_text = None

            file_name = q.get("file_name", "")
            if file_name:
                file_path = os.path.join("data", file_name)
                try:
                    if file_name.endswith(".pdf"):
                        file_text = read_pdf(file_path)
                    elif file_name.endswith(".csv"):
                        file_text = read_csv(file_path)
                    elif file_name.endswith(".txt"):
                        file_text = read_txt(file_path)
                except Exception as e:
                    status_lines.append(f"⚠️ Failed to read file {file_name}: {e}")

            response = answer_question(question, file_context=file_text, do_search=False)
            answers.append({"task_id": task_id, "submitted_answer": response})
            status_lines.append(f"✅ Q{idx}: Answered")

        status_lines.append(f"\n🎯 Done. {len(answers)} questions answered.")
        return "\n".join(status_lines)

    except Exception as e:
        return f"❌ Error:\n{traceback.format_exc()}"

def handle_submit(username, code_link):
    try:
        result = submit_answers(username, code_link, answers)
        return result
    except Exception as e:
        return f"❌ Submission error:\n{traceback.format_exc()}"

with gr.Blocks() as demo:
    gr.Markdown("# 🤖 GAIA Level 1 Agent (Local - Validation Set)")

    with gr.Row():
        username = gr.Textbox(label="Your Hugging Face Username")
        code_link = gr.Textbox(label="Your Space Code Link (/tree/main)")

    status = gr.Textbox(label="Status", lines=15)

    run_btn = gr.Button("🧠 Run Agent")
    submit_btn = gr.Button("🚀 Submit Answers")

    run_btn.click(fn=run_agent, outputs=status)
    submit_btn.click(fn=handle_submit, inputs=[username, code_link], outputs=status)

if __name__ == "__main__":
    demo.launch()