Sandra Sanchez commited on
Commit
04889fc
·
1 Parent(s): bd0f8f7

Run benchmark with a CodeAgent that uses gpt 4 turbo and the following tools: PythonInterpreter, GoogleSearch, VisitWebpage and Final_Answer. Achieve 30%

Browse files
Files changed (3) hide show
  1. .gitignore +13 -0
  2. app.py +36 -29
  3. requirements.txt +5 -1
.gitignore ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment files
2
+ .env
3
+
4
+ # IDE folders
5
+ .idea/
6
+ .vscode/
7
+
8
+ # Python cache
9
+ __pycache__/
10
+ *.pyc
11
+
12
+ # System files
13
+ .DS_Store
app.py CHANGED
@@ -3,43 +3,37 @@ import openai
3
  import gradio as gr
4
  import requests
5
  import inspect
 
 
6
  import pandas as pd
7
- from smolagents import CodeAgent, HfApiModel
 
 
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
 
13
  OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
14
-
15
-
16
- class OpenAIModel:
17
- def __init__(self):
18
- openai.api_key = OPEN_AI_KEY
19
-
20
- def run(self, prompt: str) -> str:
21
- response = openai.ChatCompletion.create(
22
- model="gpt-3.5-turbo", # or "gpt-4"
23
- messages=[{"role": "user", "content": prompt}],
24
- max_tokens=300
25
- )
26
- return response["choices"][0]["message"]["content"]
27
 
28
 
29
  # --- Basic Agent Definition ---
30
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
31
- class BasicAgent(CodeAgent):
32
- def __init__(self, tools, model, planning_interval):
33
- super().__init__(tools=tools, model=model, planning_interval=planning_interval)
34
- self.add_base_tools = True # Add any additional base tools
35
- self.planning_interval = planning_interval # Enable planning every 3 steps
36
- self.max_steps = 0
37
- print("BasicAgent initialized.")
38
- def __call__(self, question: str) -> str:
39
- print(f"Agent received question (first 50 chars): {question[:50]}...")
40
- answer = self.run(question[:50], max_steps=5) # "This is a default answer."
41
- print(f"Agent returning answer: {answer}")
42
- return answer
 
43
 
44
  def run_and_submit_all( profile: gr.OAuthProfile | None):
45
  """
@@ -62,8 +56,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
62
 
63
  # 1. Instantiate Agent ( modify this part to create your agent)
64
  try:
65
- model = OpenAIModel()
66
- agent = BasicAgent(tools=[], model=model, planning_interval=3)
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  except Exception as e:
68
  print(f"Error instantiating agent: {e}")
69
  return f"Error initializing agent: {e}", None
@@ -103,7 +110,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
103
  print(f"Skipping item with missing task_id or question: {item}")
104
  continue
105
  try:
106
- submitted_answer = agent(question_text)
107
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
108
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
109
  except Exception as e:
 
3
  import gradio as gr
4
  import requests
5
  import inspect
6
+ import whisper
7
+ from smolagents.tools import Tool, tool
8
  import pandas as pd
9
+ from dotenv import load_dotenv
10
+ from smolagents import CodeAgent, TransformersModel, GoogleSearchTool, \
11
+ VisitWebpageTool, PythonInterpreterTool, WebSearchTool, FinalAnswerTool, OpenAIServerModel
12
 
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+ load_dotenv()
18
  OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
19
+ SERPER_API_KEY = os.getenv("SERPER_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  # --- Basic Agent Definition ---
23
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
24
+
25
+
26
+ @tool
27
+ def transcribe(mp3_filepath: str) -> str:
28
+ """
29
+ This tool returns the transcription of a mp3 file.
30
+
31
+ Args:
32
+ mp3_filepath (str): the filepath name to the mp3 file we want to transcribe
33
+ """
34
+ model = whisper.load_model("base")
35
+ return model.transcribe(mp3_filepath)
36
+
37
 
38
  def run_and_submit_all( profile: gr.OAuthProfile | None):
39
  """
 
56
 
57
  # 1. Instantiate Agent ( modify this part to create your agent)
58
  try:
59
+ model = OpenAIServerModel(
60
+ model_id="gpt-4-turbo", # or "gpt-4"
61
+ api_key=OPEN_AI_KEY,
62
+ )
63
+ python_tool = PythonInterpreterTool()
64
+ google_search = GoogleSearchTool(provider="serper")
65
+ visit_web_page = VisitWebpageTool()
66
+ final_answer_tool = FinalAnswerTool()
67
+ web_agent = CodeAgent(
68
+ tools=[python_tool, google_search, visit_web_page, transcribe, final_answer_tool],
69
+ model=model,
70
+ name="web_agent",
71
+ description="Browses the web to find information",
72
+ planning_interval=3,
73
+ add_base_tools=True)
74
  except Exception as e:
75
  print(f"Error instantiating agent: {e}")
76
  return f"Error initializing agent: {e}", None
 
110
  print(f"Skipping item with missing task_id or question: {item}")
111
  continue
112
  try:
113
+ submitted_answer = web_agent.run(question_text, max_steps=5)
114
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
115
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
116
  except Exception as e:
requirements.txt CHANGED
@@ -1,4 +1,8 @@
 
1
  gradio
 
2
  requests
3
  smolagents
4
- openai
 
 
 
1
+ accelerate
2
  gradio
3
+ gradio[oauth]
4
  requests
5
  smolagents
6
+ openai
7
+ whisper
8
+ ffmpeg-python