Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from agent import load_tasks, solve_task
|
| 6 |
+
from smolagents import Task
|
| 7 |
+
|
| 8 |
+
# Load metadata
|
| 9 |
+
METADATA_PATH = "metadata.jsonl"
|
| 10 |
+
GAIA_API_URL = "https://huggingface.co/spaces/gaia-benchmark/api/submit"
|
| 11 |
+
|
| 12 |
+
# Optional auth for GAIA
|
| 13 |
+
HF_USER = os.getenv("HF_USERNAME")
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
+
|
| 16 |
+
def run_and_submit_all(submit: bool = False):
|
| 17 |
+
tasks = load_tasks(METADATA_PATH)
|
| 18 |
+
results = []
|
| 19 |
+
for task in tasks:
|
| 20 |
+
answer = solve_task(task)
|
| 21 |
+
results.append({
|
| 22 |
+
"question_id": task.task_id,
|
| 23 |
+
"answer": answer,
|
| 24 |
+
})
|
| 25 |
+
|
| 26 |
+
if submit:
|
| 27 |
+
if not HF_USER or not HF_TOKEN:
|
| 28 |
+
raise ValueError("Missing HF_USERNAME or HF_TOKEN in env vars.")
|
| 29 |
+
response = requests.post(
|
| 30 |
+
GAIA_API_URL,
|
| 31 |
+
headers={
|
| 32 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 33 |
+
"Content-Type": "application/json",
|
| 34 |
+
},
|
| 35 |
+
json={
|
| 36 |
+
"username": HF_USER,
|
| 37 |
+
"level": "level1",
|
| 38 |
+
"answers": results,
|
| 39 |
+
},
|
| 40 |
+
)
|
| 41 |
+
return f"Submitted! Response: {response.text}"
|
| 42 |
+
|
| 43 |
+
return json.dumps(results, indent=2)
|
| 44 |
+
|
| 45 |
+
def single_task_run(question_id: str, question_input: str):
|
| 46 |
+
task = Task(task_id=question_id, input=question_input)
|
| 47 |
+
output = solve_task(task)
|
| 48 |
+
return output
|
| 49 |
+
|
| 50 |
+
# Gradio UI
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("# GAIA Benchmark Agent")
|
| 53 |
+
|
| 54 |
+
with gr.Tab("Run Single Task"):
|
| 55 |
+
with gr.Row():
|
| 56 |
+
qid = gr.Textbox(label="Question ID", placeholder="e.g., q123")
|
| 57 |
+
qtext = gr.Textbox(label="Question Text", lines=4)
|
| 58 |
+
out = gr.Textbox(label="Generated Answer")
|
| 59 |
+
btn = gr.Button("Run Agent")
|
| 60 |
+
btn.click(fn=single_task_run, inputs=[qid, qtext], outputs=out)
|
| 61 |
+
|
| 62 |
+
with gr.Tab(" Run All + Submit"):
|
| 63 |
+
submit_toggle = gr.Checkbox(label="Submit to GAIA?", value=False)
|
| 64 |
+
submit_btn = gr.Button("Run All Tasks")
|
| 65 |
+
batch_out = gr.Textbox(label="Batch Output", lines=20)
|
| 66 |
+
submit_btn.click(fn=run_and_submit_all, inputs=[submit_toggle], outputs=batch_out)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|