Spaces:
Running
Running
File size: 2,730 Bytes
ce69239 714d637 77866f8 104dc35 714d637 7e057ea 714d637 7e057ea 714d637 7e057ea 714d637 ffff64a 714d637 7e057ea 77866f8 7e057ea 77866f8 104dc35 77866f8 104dc35 77866f8 104dc35 66051de 77866f8 7e057ea |
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 |
from langchain_core.tools import tool
import datetime
import requests
import openai
import os
import tempfile
from urllib.parse import urlparse
from openai import OpenAI
@tool
def current_date(_) -> str :
""" Returns the current date in YYYY-MM-DD format """
return datetime.datetime.now().strftime("%Y-%m-%d")
@tool
def day_of_week(_) -> str :
""" Returns the current day of the week (e.g., Monday, Tuesday) """
return datetime.datetime.now().strftime("%A")
@tool
def days_until(date_str: str) -> str :
""" Returns the number of days from today until a given date (input format: YYYY-MM-DD) """
try:
future_date = datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
today = datetime.date.today()
delta_days = (future_date - today).days
return f"{delta_days} days until {date_str}"
except Exception as e:
return f"Error parsing date: {str(e)}"
datetime_tools = [current_date, day_of_week, days_until]
@tool
def transcribe_audio(audio_input: str) -> str:
"""
Transcribes an audio file from a local file or a URL
Args:
audio_input (str): A local file path or a direct URL to the audio file (.mp3, .m4a, etc.)
Returns:
str: The transcribed text from the audio.
"""
try:
# Detects if audio_input is a URL
is_url = audio_input.startswith("http://") or audio_input.startswith("https://")
if is_url:
parsed = urlparse(audio_input) # breaks down the URL into components (scheme, netloc, path, params, etc.)
extension = os.path.splitext(parsed.path)[1] or ".mp3" # get the actual file extension from the URL path or define it to .mp3 if no extension is found
# Download to temporary file
with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as tmp_file: # creates a temporary file
response = requests.get(audio_input) # downloads the content
response.raise_for_status() # checks if the http request was successful
tmp_file.write(response.content) # saves the file to disk
file_path = tmp_file.name
else:
file_path = audio_input
# Transcribing audio using OpenAI Whisper
client = OpenAI()
with open(file_path, "rb") as audio_file: # opens the audio file from disk in binary mode (rb); the "with" block ensures the file is automatically closed afterward
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
return transcription.text
except Exception as e:
return f"Transcription failed: {e}" |