FrancescaScipioni commited on
Commit
5579ccc
·
verified ·
1 Parent(s): 3f8f511

Added additional useful tools

Browse files
Files changed (1) hide show
  1. agent.py +65 -1
agent.py CHANGED
@@ -1,6 +1,11 @@
1
  from langchain.tools import Tool
2
  from langchain.utilities import WikipediaAPIWrapper, ArxivAPIWrapper, DuckDuckGoSearchRun
3
  import math
 
 
 
 
 
4
 
5
  ## ----- TOOLS DEFINITION ----- ##
6
 
@@ -150,6 +155,54 @@ arxiv_tool = Tool.from_function(
150
  description="Use this tool to search ArXiv for scientific papers. Input should be a research topic or query."
151
  )
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  ## ----- TOOLS LIST ----- ##
154
 
155
  tools = [
@@ -165,4 +218,15 @@ tools = [
165
  # Search
166
  web_search_tool,
167
  wikipedia_tool,
168
- arxiv_tool
 
 
 
 
 
 
 
 
 
 
 
 
1
  from langchain.tools import Tool
2
  from langchain.utilities import WikipediaAPIWrapper, ArxivAPIWrapper, DuckDuckGoSearchRun
3
  import math
4
+ import whisper
5
+ from youtube_transcript_api import YouTubeTranscriptApi
6
+ from PIL import Image
7
+ import pytesseract
8
+ import pandas as pd
9
 
10
  ## ----- TOOLS DEFINITION ----- ##
11
 
 
155
  description="Use this tool to search ArXiv for scientific papers. Input should be a research topic or query."
156
  )
157
 
158
+ # ** Audio Transcription Tool ** #
159
+
160
+ model = whisper.load_model("base")
161
+
162
+ @tool
163
+ def transcribe_audio(file_path: str) -> str:
164
+ """Transcribe spoken words from an audio file into text."""
165
+ result = model.transcribe(file_path)
166
+ return result["text"]
167
+
168
+ # ** youtube-transcript-api Tool ** #
169
+
170
+ @tool
171
+ def get_youtube_transcript(video_id: str) -> str:
172
+ """Get transcript of a YouTube video from its video ID."""
173
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
174
+ return " ".join([entry["text"] for entry in transcript])
175
+
176
+ # ** Image Tool ** #
177
+
178
+ @tool
179
+ def extract_text_from_image(image_path: str) -> str:
180
+ """Extract text from an image using OCR."""
181
+ return pytesseract.image_to_string(Image.open(image_path))
182
+
183
+ # ** Code Execution Tool ** #
184
+
185
+ @tool
186
+ def execute_python_code(code: str) -> str:
187
+ """Execute a Python code string and return the output."""
188
+ try:
189
+ local_vars = {}
190
+ exec(code, {}, local_vars)
191
+ return str(local_vars)
192
+ except Exception as e:
193
+ return f"Error: {e}"
194
+
195
+ # ** Excel Parsing Tool ** #
196
+
197
+ @tool
198
+ def total_sales_from_excel(file_path: str) -> str:
199
+ """Compute total food sales from an Excel file."""
200
+ df = pd.read_excel(file_path)
201
+ food_df = df[df["Category"] == "Food"]
202
+ total_sales = food_df["Sales"].sum()
203
+ return f"{total_sales:.2f} USD"
204
+
205
+
206
  ## ----- TOOLS LIST ----- ##
207
 
208
  tools = [
 
218
  # Search
219
  web_search_tool,
220
  wikipedia_tool,
221
+ arxiv_tool,
222
+ # Audio
223
+ Tool.from_function(func=transcribe_audio, name="Transcribe Audio", description="Transcribe audio files to text."),
224
+ # Youtube
225
+ Tool.from_function(func=get_youtube_transcript, name="YouTube Transcript", description="Extract transcript from YouTube video."),
226
+ # Image
227
+ Tool.from_function(func=extract_text_from_image, name="Image OCR", description="Extract text from an image file."),
228
+ # Code Execution
229
+ Tool.from_function(func=execute_python_code, name="Python Code Executor", description="Run and return output from a Python script."),
230
+ # Excel parsing
231
+ Tool.from_function(func=total_sales_from_excel, name="Excel Sales Parser", description="Compute total food sales from Excel file."),
232
+ ]