framsouza commited on
Commit
e89ec77
·
verified ·
1 Parent(s): 950d883

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -89
app.py CHANGED
@@ -1,78 +1,56 @@
1
- # app.py
2
-
3
  import os
4
  import time
5
  import requests
6
  import pandas as pd
7
  import gradio as gr
8
 
9
- from smolagents import (
10
- CodeAgent,
11
- DuckDuckGoSearchTool,
12
- PythonInterpreterTool,
13
- InferenceClientModel
14
- )
15
-
16
- # --- Configuration ---
17
  API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
18
- SPACE_ID = os.getenv("SPACE_ID") # e.g. "your-username/your-space"
19
- HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") # Hugging Face token
20
- # No need for HF_USERNAME—Gradio OAuthProfile provides it
21
 
22
  if not all([SPACE_ID, HF_TOKEN]):
23
  raise RuntimeError(
24
- "Please set the following environment variables in your Space settings:\n"
25
- " • SPACE_ID\n"
26
- " • HUGGINGFACEHUB_API_TOKEN"
27
  )
28
 
29
- WELCOME_TEXT = """
30
- ## Welcome to the GAIA Benchmark Runner 🎉
31
 
32
- This challenge is your final hands-on project:
33
- - Build an agent and evaluate it on a subset of the GAIA benchmark.
34
- - You need **≥30%** to earn your Certificate of Completion. 🏅
35
- - Submit your score and see how you stack up on the Student Leaderboard!
36
  """
37
 
38
- # --- Agent Definition ---
39
  class GAIAAgent:
40
- def __init__(self, model_id="meta-llama/Llama-3-70B-Instruct"):
41
- # Initialize HF Inference client
42
- self.model = InferenceClientModel(
43
- model_id=model_id,
44
- token=HF_TOKEN,
45
- provider="hf-inference",
46
- timeout=120,
47
- temperature=0.2
48
- )
49
- # Attach search + code execution tools
50
- tools = [
51
- DuckDuckGoSearchTool(),
52
- PythonInterpreterTool()
53
- ]
54
- self.agent = CodeAgent(
55
- tools=tools,
56
- model=self.model,
57
- executor_type="local"
58
- )
59
-
60
- def answer(self, question: str, task_file: str = None) -> str:
61
- prompt = question
62
- if task_file:
63
- try:
64
- with open(task_file, "r") as f:
65
- content = f.read()
66
- prompt += f"\n\nAttached file:\n```\n{content}\n```"
67
- except:
68
- pass
69
- return self.agent.run(prompt)
70
-
71
- # --- Runner & Submission ---
72
  def run_and_submit_all(profile: gr.OAuthProfile | None):
73
  if profile is None:
74
  return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
75
-
76
  username = profile.username
77
 
78
  # 1) Fetch GAIA questions
@@ -80,36 +58,27 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
80
  q_resp.raise_for_status()
81
  questions = q_resp.json() or []
82
  if not questions:
83
- return "❌ No questions returned; check your API_URL.", pd.DataFrame()
84
 
85
- # 2) Initialize your agent
86
- agent = GAIAAgent()
87
 
88
- # 3) Run agent on each question
89
- results, payload = [], []
 
90
  for item in questions:
91
- task_id = item.get("task_id")
92
- question = item.get("question", "")
93
- file_path = item.get("task_file_path") # optional
94
-
95
  try:
96
- answer = agent.answer(question, file_path)
97
  except Exception as e:
98
- answer = f"ERROR: {e}"
99
-
100
- results.append({
101
- "Task ID": task_id,
102
- "Question": question,
103
- "Answer": answer
104
- })
105
- payload.append({
106
- "task_id": task_id,
107
- "submitted_answer": answer
108
- })
109
-
110
- time.sleep(0.5) # throttle requests
111
 
112
- # 4) Submit all answers
113
  submission = {
114
  "username": username,
115
  "agent_code": f"https://huggingface.co/spaces/{SPACE_ID}/tree/main",
@@ -119,7 +88,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
119
  s_resp.raise_for_status()
120
  data = s_resp.json()
121
 
122
- # 5) Build status message
123
  status = (
124
  f"✅ **Submission Successful!**\n\n"
125
  f"**User:** {data.get('username')}\n"
@@ -127,22 +96,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
127
  f"({data.get('correct_count')}/{data.get('total_attempted')} correct)\n"
128
  f"**Message:** {data.get('message')}"
129
  )
130
-
131
  return status, pd.DataFrame(results)
132
 
133
-
134
- # --- Gradio Interface ---
135
  with gr.Blocks() as demo:
136
- gr.Markdown(WELCOME_TEXT)
137
  login = gr.LoginButton()
138
- run_btn = gr.Button("▶️ Run Benchmark & Submit")
139
- status_out = gr.Markdown()
140
- table_out = gr.Dataframe(headers=["Task ID","Question","Answer"], wrap=True)
141
 
142
  run_btn.click(
143
  fn=run_and_submit_all,
144
  inputs=[login],
145
- outputs=[status_out, table_out]
146
  )
147
 
148
  if __name__ == "__main__":
 
 
 
1
  import os
2
  import time
3
  import requests
4
  import pandas as pd
5
  import gradio as gr
6
 
7
+ # --- Config from Env ---
 
 
 
 
 
 
 
8
  API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
9
+ SPACE_ID = os.getenv("SPACE_ID")
10
+ HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
11
+ MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-2-7b-instruct")
12
 
13
  if not all([SPACE_ID, HF_TOKEN]):
14
  raise RuntimeError(
15
+ "Please set these in your Space Secrets:\n"
16
+ " • SPACE_ID (e.g. user/your-space)\n"
17
+ " • HUGGINGFACEHUB_API_TOKEN"
18
  )
19
 
20
+ WELCOME = """
21
+ ## GAIA Benchmark Runner 🎉
22
 
23
+ Build your agent, score **≥30%** to earn the Certificate of Completion,
24
+ and see where you land on the Student Leaderboard!
 
 
25
  """
26
 
27
+ # --- Simple HF-Inference Agent ---
28
  class GAIAAgent:
29
+ def __init__(self, model_id: str, token: str):
30
+ self.model_id = model_id
31
+ self.headers = {"Authorization": f"Bearer {token}"}
32
+
33
+ def answer(self, prompt: str) -> str:
34
+ payload = {
35
+ "inputs": prompt,
36
+ "parameters": {
37
+ "max_new_tokens": 512,
38
+ "temperature": 0.2
39
+ }
40
+ }
41
+ url = f"https://api-inference.huggingface.co/models/{self.model_id}"
42
+ resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
43
+ resp.raise_for_status()
44
+ data = resp.json()
45
+ # data is a list of {generated_text: "..."}
46
+ if isinstance(data, list) and data and "generated_text" in data[0]:
47
+ return data[0]["generated_text"].strip()
48
+ return str(data)
49
+
50
+ # --- Gradio callback ---
 
 
 
 
 
 
 
 
 
 
51
  def run_and_submit_all(profile: gr.OAuthProfile | None):
52
  if profile is None:
53
  return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
 
54
  username = profile.username
55
 
56
  # 1) Fetch GAIA questions
 
58
  q_resp.raise_for_status()
59
  questions = q_resp.json() or []
60
  if not questions:
61
+ return "❌ No questions found. Check your API_URL.", pd.DataFrame()
62
 
63
+ # 2) Init agent
64
+ agent = GAIAAgent(MODEL_ID, HF_TOKEN)
65
 
66
+ # 3) Answer each
67
+ results = []
68
+ payload = []
69
  for item in questions:
70
+ tid = item.get("task_id")
71
+ qtxt = item.get("question","")
72
+ # Some tasks include a file path; you can fetch and append it if you like.
 
73
  try:
74
+ ans = agent.answer(qtxt)
75
  except Exception as e:
76
+ ans = f"ERROR: {e}"
77
+ results.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
78
+ payload.append({"task_id": tid, "submitted_answer": ans})
79
+ time.sleep(0.5)
 
 
 
 
 
 
 
 
 
80
 
81
+ # 4) Submit
82
  submission = {
83
  "username": username,
84
  "agent_code": f"https://huggingface.co/spaces/{SPACE_ID}/tree/main",
 
88
  s_resp.raise_for_status()
89
  data = s_resp.json()
90
 
91
+ # 5) Build status text
92
  status = (
93
  f"✅ **Submission Successful!**\n\n"
94
  f"**User:** {data.get('username')}\n"
 
96
  f"({data.get('correct_count')}/{data.get('total_attempted')} correct)\n"
97
  f"**Message:** {data.get('message')}"
98
  )
 
99
  return status, pd.DataFrame(results)
100
 
101
+ # --- Gradio UI ---
 
102
  with gr.Blocks() as demo:
103
+ gr.Markdown(WELCOME)
104
  login = gr.LoginButton()
105
+ run_btn = gr.Button("▶️ Run GAIA Benchmark")
106
+ status_md = gr.Markdown()
107
+ table_df = gr.Dataframe(headers=["Task ID","Question","Answer"], wrap=True)
108
 
109
  run_btn.click(
110
  fn=run_and_submit_all,
111
  inputs=[login],
112
+ outputs=[status_md, table_df]
113
  )
114
 
115
  if __name__ == "__main__":