Spaces:
Paused
Paused
File size: 3,278 Bytes
a623d90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
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
|