File size: 1,689 Bytes
da279f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from api import get_all_questions, submit_answers, get_file
from agent import answer_question
from tools.file_loader import read_pdf, read_csv, read_txt

qa_data = []
answers = []

def run_agent():
    global qa_data, answers
    qa_data = get_all_questions()
    answers = []
    for q in qa_data:
        task_id = q['task_id']
        question = q['question']
        file_text = None
        if q.get("files"):
            for fname in q["files"]:
                raw = get_file(task_id, fname)
                if fname.endswith(".pdf"):
                    file_text = read_pdf(raw)
                elif fname.endswith(".csv"):
                    file_text = read_csv(raw)
                elif fname.endswith(".txt"):
                    file_text = read_txt(raw)
                break

        response = answer_question(question, file_context=file_text, do_search=True)
        answers.append({"task_id": task_id, "submitted_answer": response})
    return f"✅ Agent answered {len(answers)} questions. Ready to submit!"

def handle_submit(username, code_link):
    return submit_answers(username, code_link, answers)

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

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

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

    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()