LamiaYT commited on
Commit
61f4b08
·
1 Parent(s): 7e87c38

Deploy GAIA agent

Browse files
Files changed (1) hide show
  1. app.py +35 -39
app.py CHANGED
@@ -6,22 +6,19 @@ 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(
@@ -33,15 +30,14 @@ def simple_search(query: str) -> str:
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,
@@ -53,17 +49,17 @@ class BasicAgent:
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"
@@ -71,35 +67,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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})
94
 
95
- if not payload:
96
- return "No answers generated.", pd.DataFrame(logs)
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')}% "
@@ -108,17 +104,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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...")
 
6
  import pandas as pd
7
 
8
  from smolagents import CodeAgent, tool
9
+ from smolagents.models import LiteLLMModel # ✅ correct import
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
+ # --- Simple Web Search Tool ---
15
  @tool
16
  def simple_search(query: str) -> str:
17
  """
 
 
18
  Args:
19
+ query (str): Search query text.
 
20
  Returns:
21
+ str: Top 3 DuckDuckGo search result titles & links.
22
  """
23
  try:
24
  resp = requests.get(
 
30
  from bs4 import BeautifulSoup
31
  soup = BeautifulSoup(resp.text, "html.parser")
32
  items = soup.select("a.result__a")[:3]
33
+ return "\n\n".join(f"{a.get_text()}\n{a['href']}" for a in items) or "No results found."
 
34
  except Exception as e:
35
  return f"Search error: {e}"
36
 
37
+ # --- Enhanced Agent using Light Model ---
38
  class BasicAgent:
39
  def __init__(self):
40
+ print("BasicAgent initialized with LiteLLMModel (falcon-7b-instruct).")
41
  self.model = LiteLLMModel(
42
  model_id="tiiuae/falcon-7b-instruct",
43
  max_tokens=512,
 
49
  )
50
 
51
  def __call__(self, question: str) -> str:
52
+ print(f"Question: {question[:60]}...")
53
  try:
54
  return self.agent.run(question)
55
  except Exception as e:
56
  return f"Agent error: {e}"
57
 
58
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
59
  if not profile:
60
+ return "Please log in to Hugging Face to submit answers.", None
61
  username = profile.username
62
+ space_id = os.getenv("SPACE_ID", "")
63
 
64
  questions_url = f"{DEFAULT_API_URL}/questions"
65
  submit_url = f"{DEFAULT_API_URL}/submit"
 
67
  try:
68
  agent = BasicAgent()
69
  except Exception as e:
70
+ return f"Agent initialization failed: {e}", None
71
 
72
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
73
 
74
  try:
75
+ r = requests.get(questions_url, timeout=15)
76
+ r.raise_for_status()
77
+ questions = r.json()
78
  except Exception as e:
79
+ return f"Error fetching questions: {e}", None
80
 
81
+ logs, answers = [], []
82
+ for item in questions:
83
+ task_id = item.get("task_id")
84
+ question = item.get("question")
85
+ if not task_id or question is None:
86
  continue
87
+ ans = agent(question)
88
+ answers.append({"task_id": task_id, "submitted_answer": ans})
89
+ logs.append({"Task ID": task_id, "Question": question, "Submitted Answer": ans})
90
 
91
+ if not answers:
92
+ return "Agent produced no answers.", pd.DataFrame(logs)
93
 
94
+ payload = {"username": username, "agent_code": agent_code, "answers": answers}
95
  try:
96
+ resp = requests.post(submit_url, json=payload, timeout=60)
97
+ resp.raise_for_status()
98
+ data = resp.json()
99
  status = (
100
  f"✅ Submission Successful!\n"
101
  f"Score: {data.get('score','N/A')}% "
 
104
  )
105
  return status, pd.DataFrame(logs)
106
  except Exception as e:
107
+ return f"Submission failed: {e}", pd.DataFrame(logs)
108
 
109
+ # --- Gradio Interface ---
110
  with gr.Blocks() as demo:
111
  gr.Markdown("# GAIA Agent Evaluation Runner")
112
  gr.LoginButton()
113
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
114
+ status_box = gr.Textbox(label="Status / Submission Result", lines=5, interactive=False)
115
+ result_table = gr.DataFrame(label="Questions & Agent Answers", wrap=True)
116
 
117
+ run_button.click(run_and_submit_all, outputs=[status_box, result_table])
118
 
119
  if __name__ == "__main__":
120
  print("Launching Gradio app...")