FD900 commited on
Commit
3d1b0c5
·
verified ·
1 Parent(s): f3387e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -62
app.py CHANGED
@@ -1,69 +1,37 @@
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()
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
  import gradio as gr
3
  import json
4
+ import os
5
  import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ from mistral_hf_wrapper import MistralInference
8
+ from agent import solve_task, load_tasks
 
 
9
 
10
+ API_URL = os.getenv("HF_MISTRAL_URL")
11
+ API_TOKEN = os.getenv("HF_TOKEN")
12
+ USERNAME = os.getenv("HF_USERNAME")
13
+ CODE_LINK = os.getenv("HF_CODE_LINK") # e.g., https://huggingface.co/spaces/yourname/yourspace/tree/main
14
 
15
+ def run_and_submit_all():
16
+ model = MistralInference(api_url=API_URL, api_token=API_TOKEN)
17
+ tasks = load_tasks()
18
+ answers = []
 
 
 
19
 
20
+ for task in tasks:
21
+ try:
22
+ answer = solve_task(task, model)
23
+ answers.append({"task_id": task["task_id"], "submitted_answer": answer.strip()})
24
+ except Exception as e:
25
+ answers.append({"task_id": task["task_id"], "submitted_answer": f"ERROR: {str(e)}"})
26
+
27
+ res = requests.post(
28
+ "https://agents-course-unit4-scoring.hf.space/submit",
29
+ headers={"Content-Type": "application/json"},
30
+ json={"username": USERNAME, "agent_code": CODE_LINK, "answers": answers},
31
+ )
32
+
33
+ if res.ok:
34
+ return json.dumps(res.json(), indent=2)
35
+ return f"Error submitting: {res.status_code} - {res.text}"
36
+
37
+ gr.Interface(run_and_submit_all, inputs=[], outputs="textbox", title="GAIA Benchmark Agent Submission").launch()