LamiaYT commited on
Commit
f96a820
·
1 Parent(s): d12c64d

Deploy GAIA agent

Browse files
Files changed (1) hide show
  1. app.py +47 -37
app.py CHANGED
@@ -4,24 +4,24 @@ import os
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
- from bs4 import BeautifulSoup
8
 
9
- from smolagents import CodeAgent, tool, InferenceClientModel
 
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
- # --- Custom Web Search Tool (avoiding proxies) ---
15
  @tool
16
  def simple_search(query: str) -> str:
17
  """
18
- Search DuckDuckGo via HTML parsing.
19
 
20
  Args:
21
- query (str): search query string.
22
 
23
  Returns:
24
- str: top 3 titles and URLs.
25
  """
26
  try:
27
  resp = requests.get(
@@ -30,57 +30,64 @@ def simple_search(query: str) -> str:
30
  timeout=10
31
  )
32
  resp.raise_for_status()
 
33
  soup = BeautifulSoup(resp.text, "html.parser")
34
- results = []
35
- for a in soup.select("a.result__a")[:3]:
36
- title = a.get_text()
37
- url = a["href"]
38
- results.append(f"- {title}\n {url}")
39
- return "\n".join(results) if results else "No results."
40
  except Exception as e:
41
  return f"Search error: {e}"
42
 
43
- # --- Enhanced Agent ---
44
  class BasicAgent:
45
  def __init__(self):
46
- print("BasicAgent initialized.")
47
- self.model = InferenceClientModel()
 
 
 
 
48
  self.agent = CodeAgent(
49
  model=self.model,
50
  tools=[simple_search]
51
  )
52
 
53
  def __call__(self, question: str) -> str:
54
- print("Received:", question[:50], "...")
55
  try:
56
  return self.agent.run(question)
57
  except Exception as e:
58
- return f"Agent run error: {e}"
59
 
60
  def run_and_submit_all(profile: gr.OAuthProfile | None):
61
  space_id = os.getenv("SPACE_ID")
62
  if not profile:
63
- return "Please login to Hugging Face", None
64
  username = profile.username
 
65
  questions_url = f"{DEFAULT_API_URL}/questions"
66
  submit_url = f"{DEFAULT_API_URL}/submit"
67
 
68
- try: agent = BasicAgent()
 
69
  except Exception as e:
70
- return f"Agent init error: {e}", None
71
 
72
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
73
  try:
74
- r = requests.get(questions_url, timeout=15)
75
- r.raise_for_status()
76
- qs = r.json()
77
  except Exception as e:
78
- return f"Fetch questions error: {e}", None
79
 
80
  logs, payload = [], []
81
  for item in qs:
82
- tid, q = item.get("task_id"), item.get("question")
83
- if not tid or q is None: continue
 
 
84
  ans = agent(q)
85
  payload.append({"task_id": tid, "submitted_answer": ans})
86
  logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
@@ -90,26 +97,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
90
 
91
  submit = {"username": username, "agent_code": agent_code, "answers": payload}
92
  try:
93
- resp = requests.post(submit_url, json=submit, timeout=60)
94
- resp.raise_for_status()
95
- data = resp.json()
96
  status = (
97
- f"✅ Score: {data.get('score','N/A')}% "
98
- f"({data.get('correct_count','?')}/{data.get('total_attempted','?')} correct)\n"
99
- f"{data.get('message','No message')}"
 
100
  )
101
  return status, pd.DataFrame(logs)
102
  except Exception as e:
103
- return f"Submit error: {e}", pd.DataFrame(logs)
104
 
105
  # --- Gradio UI ---
106
  with gr.Blocks() as demo:
107
- gr.Markdown("# GAIA Evaluation Runner")
108
  gr.LoginButton()
109
- run_btn = gr.Button("Run Evaluation & Submit")
110
- status = gr.Textbox(lines=5, interactive=False)
111
- results = gr.DataFrame(wrap=True)
 
112
  run_btn.click(run_and_submit_all, outputs=[status, results])
113
 
114
  if __name__ == "__main__":
 
115
  demo.launch(debug=True, share=False)
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
 
7
 
8
+ from smolagents import CodeAgent, tool
9
+ from litellm import LiteLLMModel
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
+ # --- Tool: simple DuckDuckGo search via HTML scraping ---
15
  @tool
16
  def simple_search(query: str) -> str:
17
  """
18
+ Perform a basic DuckDuckGo search.
19
 
20
  Args:
21
+ query (str): Search query.
22
 
23
  Returns:
24
+ str: Top 3 titles and URLs from search.
25
  """
26
  try:
27
  resp = requests.get(
 
30
  timeout=10
31
  )
32
  resp.raise_for_status()
33
+ from bs4 import BeautifulSoup
34
  soup = BeautifulSoup(resp.text, "html.parser")
35
+ items = soup.select("a.result__a")[:3]
36
+ results = [f"{a.get_text()}\n{a['href']}" for a in items]
37
+ return "\n\n".join(results) if results else "No results found."
 
 
 
38
  except Exception as e:
39
  return f"Search error: {e}"
40
 
41
+ # --- Enhanced Agent using a public model ---
42
  class BasicAgent:
43
  def __init__(self):
44
+ print("BasicAgent initialized with Falcon 7B Instruct model.")
45
+ self.model = LiteLLMModel(
46
+ model_id="tiiuae/falcon-7b-instruct",
47
+ max_tokens=512,
48
+ temperature=0.1
49
+ )
50
  self.agent = CodeAgent(
51
  model=self.model,
52
  tools=[simple_search]
53
  )
54
 
55
  def __call__(self, question: str) -> str:
56
+ print(f"Received question: {question[:50]}...")
57
  try:
58
  return self.agent.run(question)
59
  except Exception as e:
60
+ return f"Agent error: {e}"
61
 
62
  def run_and_submit_all(profile: gr.OAuthProfile | None):
63
  space_id = os.getenv("SPACE_ID")
64
  if not profile:
65
+ return "Please log in to Hugging Face to submit.", None
66
  username = profile.username
67
+
68
  questions_url = f"{DEFAULT_API_URL}/questions"
69
  submit_url = f"{DEFAULT_API_URL}/submit"
70
 
71
+ try:
72
+ agent = BasicAgent()
73
  except Exception as e:
74
+ return f"Agent init failed: {e}", None
75
 
76
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
77
+
78
  try:
79
+ resp = requests.get(questions_url, timeout=15)
80
+ resp.raise_for_status()
81
+ qs = resp.json()
82
  except Exception as e:
83
+ return f"Failed to fetch questions: {e}", None
84
 
85
  logs, payload = [], []
86
  for item in qs:
87
+ tid = item.get("task_id")
88
+ q = item.get("question")
89
+ if not tid or q is None:
90
+ continue
91
  ans = agent(q)
92
  payload.append({"task_id": tid, "submitted_answer": ans})
93
  logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
 
97
 
98
  submit = {"username": username, "agent_code": agent_code, "answers": payload}
99
  try:
100
+ r2 = requests.post(submit_url, json=submit, timeout=60)
101
+ r2.raise_for_status()
102
+ data = r2.json()
103
  status = (
104
+ f"✅ Submission Successful!\n"
105
+ f"Score: {data.get('score','N/A')}% "
106
+ f"({data.get('correct_count','?')}/{data.get('total_attempted','?')})\n"
107
+ f"{data.get('message','')}"
108
  )
109
  return status, pd.DataFrame(logs)
110
  except Exception as e:
111
+ return f"Submit failed: {e}", pd.DataFrame(logs)
112
 
113
  # --- Gradio UI ---
114
  with gr.Blocks() as demo:
115
+ gr.Markdown("# GAIA Agent Evaluation Runner")
116
  gr.LoginButton()
117
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
118
+ status = gr.Textbox(label="Status / Submission Result", lines=5, interactive=False)
119
+ results = gr.DataFrame(label="Questions & Submitted Answers", wrap=True)
120
+
121
  run_btn.click(run_and_submit_all, outputs=[status, results])
122
 
123
  if __name__ == "__main__":
124
+ print("Launching Gradio app...")
125
  demo.launch(debug=True, share=False)