Ubik80 commited on
Commit
94aca96
·
verified ·
1 Parent(s): 6484aeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import os
3
  import gradio as gr
4
  import requests
@@ -6,36 +5,35 @@ import pandas as pd
6
 
7
  from tools import AnswerTool
8
  from smolagents import CodeAgent, OpenAIServerModel
9
- from smolagents import DuckDuckGoSearchTool # ← importiamo il search tool
10
 
11
- # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
  class BasicAgent:
15
  def __init__(self):
16
- # Initialize CodeAgent with GPT-4o, AnswerTool e DuckDuckGoSearchTool
17
- model = OpenAIServerModel(model_id="gpt-4o")
18
  answer_tool = AnswerTool()
19
  web_tool = DuckDuckGoSearchTool()
 
20
  self.agent = CodeAgent(
21
  model=model,
22
- tools=[answer_tool, web_tool], # ← due tool
23
- add_base_tools=False, # niente altri tool
24
- max_steps=2, # fino a due passaggi
25
  verbosity_level=0
26
  )
27
 
28
  def __call__(self, question: str) -> str:
29
- # Fai girare l'agente: prima prova AnswerTool, poi DuckDuckGo se serve
30
  return self.agent.run(question)
31
 
32
-
33
  def run_and_submit_all(username):
34
- # Username fornito manualmente
35
  if not username:
36
  return "Please enter your Hugging Face username.", None
37
 
38
- # Fetch questions
39
  try:
40
  resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
41
  if resp.status_code == 429:
@@ -45,30 +43,30 @@ def run_and_submit_all(username):
45
  except Exception as e:
46
  return f"Error fetching questions: {e}", None
47
 
48
- # Run agent on all questions
49
- agent = BasicAgent()
50
  results = []
51
  payload = []
52
  for q in questions:
53
- tid = q.get('task_id')
54
- text = q.get('question')
55
  if not (tid and text):
56
  continue
57
  try:
58
  ans = agent(text)
59
  except Exception as e:
60
  ans = f"ERROR: {e}"
61
- results.append({'Task ID': tid, 'Question': text, 'Answer': ans})
62
- payload.append({'task_id': tid, 'submitted_answer': ans})
63
 
64
  if not payload:
65
  return "Agent returned no answers.", pd.DataFrame(results)
66
 
67
- # Submit answers
68
  submission = {
69
- 'username': username,
70
- 'agent_code': f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
71
- 'answers': payload
72
  }
73
  try:
74
  sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
@@ -85,20 +83,18 @@ def run_and_submit_all(username):
85
 
86
  return status, pd.DataFrame(results)
87
 
88
-
89
  def test_random_question(username):
90
  if not username:
91
  return "Please enter your Hugging Face username.", ""
92
  try:
93
  q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
94
- question = q.get('question', '')
95
- ans = BasicAgent()(question)
96
  return question, ans
97
  except Exception as e:
98
  return f"Error during test: {e}", ""
99
 
100
-
101
- # --- Gradio UI ---
102
  with gr.Blocks() as demo:
103
  gr.Markdown("# Basic Agent Evaluation Runner")
104
  gr.Markdown(
@@ -111,8 +107,8 @@ with gr.Blocks() as demo:
111
  )
112
 
113
  username_input = gr.Textbox(label="Hugging Face Username", placeholder="your-username")
114
- run_btn = gr.Button("Run Evaluation & Submit All Answers")
115
- test_btn = gr.Button("Test Random Question")
116
 
117
  status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
118
  table_out = gr.DataFrame(label="Full Results Table", wrap=True)
 
 
1
  import os
2
  import gradio as gr
3
  import requests
 
5
 
6
  from tools import AnswerTool
7
  from smolagents import CodeAgent, OpenAIServerModel
8
+ from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool
9
 
10
+ # Constants
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
  class BasicAgent:
14
  def __init__(self):
15
+ # Initialize CodeAgent with GPT-4o, custom AnswerTool, DuckDuckGo and Wikipedia tools
16
+ model = OpenAIServerModel(model_id="gpt-4o")
17
  answer_tool = AnswerTool()
18
  web_tool = DuckDuckGoSearchTool()
19
+ wiki_tool = WikipediaSearchTool()
20
  self.agent = CodeAgent(
21
  model=model,
22
+ tools=[answer_tool, web_tool, wiki_tool],
23
+ add_base_tools=True,
24
+ max_steps=2,
25
  verbosity_level=0
26
  )
27
 
28
  def __call__(self, question: str) -> str:
29
+ # Run the agent on the question
30
  return self.agent.run(question)
31
 
 
32
  def run_and_submit_all(username):
 
33
  if not username:
34
  return "Please enter your Hugging Face username.", None
35
 
36
+ # 1. Fetch questions
37
  try:
38
  resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
39
  if resp.status_code == 429:
 
43
  except Exception as e:
44
  return f"Error fetching questions: {e}", None
45
 
46
+ # 2. Run agent on all questions
47
+ agent = BasicAgent()
48
  results = []
49
  payload = []
50
  for q in questions:
51
+ tid = q.get("task_id")
52
+ text = q.get("question")
53
  if not (tid and text):
54
  continue
55
  try:
56
  ans = agent(text)
57
  except Exception as e:
58
  ans = f"ERROR: {e}"
59
+ results.append({"Task ID": tid, "Question": text, "Answer": ans})
60
+ payload.append({"task_id": tid, "submitted_answer": ans})
61
 
62
  if not payload:
63
  return "Agent returned no answers.", pd.DataFrame(results)
64
 
65
+ # 3. Submit answers
66
  submission = {
67
+ "username": username,
68
+ "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
69
+ "answers": payload
70
  }
71
  try:
72
  sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
 
83
 
84
  return status, pd.DataFrame(results)
85
 
 
86
  def test_random_question(username):
87
  if not username:
88
  return "Please enter your Hugging Face username.", ""
89
  try:
90
  q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
91
+ question = q.get("question", "")
92
+ ans = BasicAgent()(question)
93
  return question, ans
94
  except Exception as e:
95
  return f"Error during test: {e}", ""
96
 
97
+ # Build Gradio interface
 
98
  with gr.Blocks() as demo:
99
  gr.Markdown("# Basic Agent Evaluation Runner")
100
  gr.Markdown(
 
107
  )
108
 
109
  username_input = gr.Textbox(label="Hugging Face Username", placeholder="your-username")
110
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
111
+ test_btn = gr.Button("Test Random Question")
112
 
113
  status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
114
  table_out = gr.DataFrame(label="Full Results Table", wrap=True)