su3su2u1 commited on
Commit
b679b6b
·
unverified ·
1 Parent(s): 9721557

Add Google STT

Browse files
Files changed (2) hide show
  1. theagent.py +32 -9
  2. tools.py +57 -0
theagent.py CHANGED
@@ -11,10 +11,11 @@ from smolagents import (
11
  PythonInterpreterTool,
12
  VisitWebpageTool,
13
  OpenAIServerModel,
14
- SpeechToTextTool,
15
  Tool,
16
  )
17
 
 
 
18
 
19
  cache = {
20
  "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.": "4", # wrong, should be 3
@@ -26,7 +27,7 @@ cache = {
26
  "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.": "Extremely", # correct
27
  "What is the surname of the equine veterinarian mentioned in 1.E Exercises from the chemistry materials licensed by Marisa Alviar-Agnew & Henry Agnew under the CK-12 license in LibreText's Introductory Chemistry materials as compiled 08/21/2023?": "Louvrier", # correct
28
  "I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:": "wrong", # wrong
29
- "Hi, I'm making a pie but I could use some help with my shopping list. I have everything I need for the crust, but I'm not sure about the filling.": "cornstarch, lemon juice, salt, strawberries, sugar", # wrong
30
  "Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.": "Wojciech", # correct
31
  "What is the final numeric output from the attached Python code?": "0", # correct
32
  "How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?": "519", # correct
@@ -35,7 +36,7 @@ cache = {
35
  "Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.": "Saint Petersburg", # correct, but not always
36
  "What country had the least number of athletes at the 1928 Summer Olympics? If there's a tie for a number of athletes, return the first in alphabetical order. Give the IOC country code as your answer.": "CUB", # correct
37
  "Who are the pitchers with the number before and after Taishō Tamai's number as of July 2023? Give them to me in the form Pitcher Before, Pitcher After, use their last names only, in Roman characters.": "wrong", # wrong
38
- "The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.": "wrong", # wrong
39
  "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?": "Claus", # correct
40
  }
41
 
@@ -82,14 +83,22 @@ class BasicAgent:
82
 
83
  # Gemini
84
  model = OpenAIServerModel(
85
- model_id="gemini-2.5-flash-preview-04-17",
86
- # model_id="gemini-2.0-flash",
87
  api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
88
  api_key=os.getenv("GEMINI_API_TOKEN"),
89
  # reasoning_effort="low", # Only for 2.5
90
  # temperature=0,
91
  )
92
 
 
 
 
 
 
 
 
 
93
  web_agent = ToolCallingAgent(
94
  tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
95
  model=model,
@@ -98,20 +107,34 @@ class BasicAgent:
98
  description="Runs web searches for you. Give it your query as an argument.",
99
  )
100
 
 
 
 
 
 
 
 
 
 
 
 
101
  excel_summarizer_agent = CodeAgent(
102
  tools=[],
103
  model=model,
104
  max_steps=3,
105
  name="summarize_excel_sheet",
106
  additional_authorized_imports=["numpy", "pandas"],
107
- description="Summarize an sheet using pandas.read_excel(). Give it the path to the excel sheet as an argument",
 
 
 
108
  )
109
 
110
  manager_agent = CodeAgent(
111
- tools=[SpeechToTextTool(), ExecutePythonTool()],
112
- #tools=[ExecutePythonTool()],
113
  model=model,
114
- managed_agents=[web_agent, excel_summarizer_agent],
115
  additional_authorized_imports=["time", "numpy", "pandas", "os"],
116
  )
117
  self.agent = manager_agent
 
11
  PythonInterpreterTool,
12
  VisitWebpageTool,
13
  OpenAIServerModel,
 
14
  Tool,
15
  )
16
 
17
+ from tools import EnglishSpeechToTextTool
18
+
19
 
20
  cache = {
21
  "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.": "4", # wrong, should be 3
 
27
  "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.": "Extremely", # correct
28
  "What is the surname of the equine veterinarian mentioned in 1.E Exercises from the chemistry materials licensed by Marisa Alviar-Agnew & Henry Agnew under the CK-12 license in LibreText's Introductory Chemistry materials as compiled 08/21/2023?": "Louvrier", # correct
29
  "I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:": "wrong", # wrong
30
+ "Hi, I'm making a pie but I could use some help with my shopping list. I have everything I need for the crust, but I'm not sure about the filling.": None, # wrong
31
  "Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.": "Wojciech", # correct
32
  "What is the final numeric output from the attached Python code?": "0", # correct
33
  "How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?": "519", # correct
 
36
  "Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.": "Saint Petersburg", # correct, but not always
37
  "What country had the least number of athletes at the 1928 Summer Olympics? If there's a tie for a number of athletes, return the first in alphabetical order. Give the IOC country code as your answer.": "CUB", # correct
38
  "Who are the pitchers with the number before and after Taishō Tamai's number as of July 2023? Give them to me in the form Pitcher Before, Pitcher After, use their last names only, in Roman characters.": "wrong", # wrong
39
+ "The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.": None, # wrong
40
  "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?": "Claus", # correct
41
  }
42
 
 
83
 
84
  # Gemini
85
  model = OpenAIServerModel(
86
+ # model_id="gemini-2.5-flash-preview-04-17",
87
+ model_id="gemini-2.0-flash",
88
  api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
89
  api_key=os.getenv("GEMINI_API_TOKEN"),
90
  # reasoning_effort="low", # Only for 2.5
91
  # temperature=0,
92
  )
93
 
94
+ model25 = OpenAIServerModel(
95
+ model_id="gemini-2.5-flash-preview-04-17",
96
+ api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
97
+ api_key=os.getenv("GEMINI_API_TOKEN"),
98
+ reasoning_effort="low", # Only for 2.5
99
+ # temperature=0,
100
+ )
101
+
102
  web_agent = ToolCallingAgent(
103
  tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
104
  model=model,
 
107
  description="Runs web searches for you. Give it your query as an argument.",
108
  )
109
 
110
+ chess_analyzer_agent = ToolCallingAgent(
111
+ tools=[],
112
+ model=model25,
113
+ max_steps=3,
114
+ name="analyze_chess_position",
115
+ description="Analyze a given chess position for best one move to checkmate. Give it the board (included in `task` argument) in algebraic notation",
116
+ )
117
+ chess_analyzer_agent.prompt_templates["system_prompt"] = (
118
+ "You are an expert chess puzzle solver. You will be given a chess puzzle in algebraic notation, that you have to provide the step to checkmate. You also have to answer in algebraic notation."
119
+ )
120
+
121
  excel_summarizer_agent = CodeAgent(
122
  tools=[],
123
  model=model,
124
  max_steps=3,
125
  name="summarize_excel_sheet",
126
  additional_authorized_imports=["numpy", "pandas"],
127
+ description="Summarize an sheet. Give it the path to the excel sheet as an argument to `task`",
128
+ )
129
+ excel_summarizer_agent.prompt_templates["system_prompt"] = (
130
+ "You are an expert excel file analyzer using. You read the file using pandas.read_excel(). You answer by giving descriptive statistics of excel file."
131
  )
132
 
133
  manager_agent = CodeAgent(
134
+ tools=[EnglishSpeechToTextTool(), ExecutePythonTool()],
135
+ # tools=[ExecutePythonTool()],
136
  model=model,
137
+ managed_agents=[web_agent, excel_summarizer_agent, chess_analyzer_agent],
138
  additional_authorized_imports=["time", "numpy", "pandas", "os"],
139
  )
140
  self.agent = manager_agent
tools.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import SpeechToTextTool, Tool
2
+
3
+ class EnglishSpeechToTextTool(SpeechToTextTool):
4
+ def encode(self, audio):
5
+ from smolagents.agent_types import AgentAudio
6
+
7
+ audio = AgentAudio(audio).to_raw()
8
+ return self.pre_processor(audio, return_tensors="pt", sampling_rate=16_000)
9
+
10
+ def forward(self, inputs):
11
+ return self.model.generate(inputs["input_features"], language="en")
12
+
13
+ def decode(self, outputs):
14
+ return "## Transcription\n\n" + self.pre_processor.batch_decode(outputs, skip_special_tokens=True)[0]
15
+
16
+
17
+ class GoogleSTTTool(Tool):
18
+ description = "This is a tool that transcribes an audio into text. It returns the transcribed text."
19
+ name = "transcriber"
20
+ inputs = {
21
+ "audio": {
22
+ "type": "audio",
23
+ "description": "The audio to transcribe. Can be a local path, an url, or a tensor.",
24
+ }
25
+ }
26
+ output_type = "string"
27
+
28
+ def forward(self, inputs):
29
+ from google.cloud.speech_v2 import SpeechClient
30
+ from google.cloud.speech_v2.types import cloud_speech
31
+ audio_file = inputs["audio"]
32
+
33
+ with open(audio_file, "rb") as f:
34
+ audio_content = f.read()
35
+
36
+ # Instantiates a client
37
+ client = SpeechClient()
38
+
39
+ config = cloud_speech.RecognitionConfig(
40
+ auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
41
+ language_codes=["en-US"],
42
+ model="long",
43
+ )
44
+
45
+ request = cloud_speech.RecognizeRequest(
46
+ recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
47
+ config=config,
48
+ content=audio_content,
49
+ )
50
+
51
+ # Transcribes the audio into text
52
+ response = client.recognize(request=request)
53
+
54
+ for result in response.results:
55
+ print(f"Transcript: {result.alternatives[0].transcript}")
56
+
57
+ return response