AgenticAI_Finals / agent_tools.py
Marek Stoj
Moah tools.
a623d90
import os
import requests
import pandas as pd
from langchain.tools import tool
from pytube import extract
from langchain_core.tools import tool
from openai import OpenAI
from youtube_transcript_api._api import YouTubeTranscriptApi
@tool
def transcribe_youtube_video(video_url: str) -> str:
"""Get the transcript of a YouTube video.
Args:
video_url (str): YouTube URL of the video
"""
video_id = extract.video_id(video_url)
ytt_api = YouTubeTranscriptApi()
transcript = ytt_api.fetch(video_id)
txt = "\n".join([s.text for s in transcript.snippets])
return txt
@tool
def transcribe_audio_file(audio_url: str) -> str:
"""Transcribe an audio file to text.
Args:
audio_url (str): The URL of the audio file
"""
response = requests.get(audio_url)
response.raise_for_status()
file_extension = "mp3"
file_name = f"audio-file.{file_extension}"
with open(file_name, 'wb') as file:
file.write(response.content)
openai_client = OpenAI()
with open(file_name, "rb") as audio_file:
transcription = \
openai_client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
os.remove(file_name)
return transcription.text
@tool
def read_excel_file(excel_file_url: str) -> str:
"""Read an Excel file and return as string.
Args:
excel_file_url (str): The URL of the Excel file
"""
response = requests.get(excel_file_url)
response.raise_for_status()
file_extension = "xlsx"
excel_file_name = f"excel-file.{file_extension}"
with open(excel_file_name, "wb") as file:
file.write(response.content)
df = pd.read_excel(excel_file_name)
text = df.to_string()
os.remove(excel_file_name)
return text
@tool
def download_webpage_content(page_url: str) -> str:
"""Load a web page and return its content as text.
Args:
page_url (str): the URL of web page to get
"""
response = requests.get(page_url)
response.raise_for_status()
return response.text
@tool
def multiply(a: float, b: float) -> float:
"""Multiplies two numbers.
Args:
a (float): the first number
b (float): the second number
"""
return a * b
@tool
def add(a: float, b: float) -> float:
"""Adds two numbers.
Args:
a (float): the first number
b (float): the second number
"""
return a + b
@tool
def subtract(a: float, b: float) -> float:
"""Subtracts two numbers.
Args:
a (float): the first number
b (float): the second number
"""
return a - b
@tool
def divide(a: float, b: float) -> float:
"""Divides two numbers.
Args:
a (float): the first float number
b (float): the second float number
"""
if b == 0:
raise ValueError("Cannot divided by zero.")
return a / b
@tool
def modulus(a: int, b: int) -> int:
"""Get the modulus of two numbers.
Args:
a (int): the first number
b (int): the second number
"""
return a % b
@tool
def power(a: float, b: float) -> float:
"""Get the power of two numbers.
Args:
a (float): the first number
b (float): the second number
"""
return a**b