Spaces:
Sleeping
Sleeping
| 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 "π― Done.\n\n" + "\n".join( | |
| [f"β Q{idx+1} (ID {a['task_id']}): {a['submitted_answer']}" for idx, a in enumerate(answers)] | |
| ) | |
| 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() |