Ubik80 commited on
Commit
211cd46
·
verified ·
1 Parent(s): d1ff106

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import gradio as gr
3
  import requests
@@ -7,28 +8,29 @@ 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
@@ -44,7 +46,7 @@ def run_and_submit_all(username):
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:
@@ -64,9 +66,9 @@ def run_and_submit_all(username):
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,6 +85,7 @@ def run_and_submit_all(username):
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.", ""
@@ -94,7 +97,8 @@ def test_random_question(username):
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(
@@ -120,3 +124,4 @@ with gr.Blocks() as demo:
120
 
121
  if __name__ == "__main__":
122
  demo.launch(debug=True, share=False)
 
 
1
+ # app.py
2
  import os
3
  import gradio as gr
4
  import requests
 
8
  from smolagents import CodeAgent, OpenAIServerModel
9
  from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool
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, Wikipedia, DuckDuckGo, and AnswerTool
17
  model = OpenAIServerModel(model_id="gpt-4o")
 
 
18
  wiki_tool = WikipediaSearchTool()
19
+ web_tool = DuckDuckGoSearchTool()
20
+ answer_tool = AnswerTool()
21
  self.agent = CodeAgent(
22
  model=model,
23
+ # try wiki first, then web, then direct answer
24
+ tools=[wiki_tool, web_tool, answer_tool],
25
+ add_base_tools=True, # include python_eval, image_ocr, etc.
26
+ max_steps=3, # allow up to 3 planning steps
27
  verbosity_level=0
28
  )
29
 
30
  def __call__(self, question: str) -> str:
 
31
  return self.agent.run(question)
32
 
33
+
34
  def run_and_submit_all(username):
35
  if not username:
36
  return "Please enter your Hugging Face username.", None
 
46
  return f"Error fetching questions: {e}", None
47
 
48
  # 2. Run agent on all questions
49
+ agent = BasicAgent()
50
  results = []
51
  payload = []
52
  for q in questions:
 
66
 
67
  # 3. 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
 
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.", ""
 
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(
 
124
 
125
  if __name__ == "__main__":
126
  demo.launch(debug=True, share=False)
127
+