File size: 3,062 Bytes
714d637
 
77866f8
 
 
 
 
104dc35
714d637
 
 
 
 
 
 
 
 
ffff64a
 
 
 
 
714d637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77866f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104dc35
77866f8
 
104dc35
 
 
 
77866f8
 
 
 
104dc35
 
66051de
77866f8
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.tools import Tool
import datetime
import requests
import openai
import os
import tempfile
from urllib.parse import urlparse
from openai import OpenAI

def current_date(_):
    return datetime.datetime.now().strftime("%Y-%m-%d")

def day_of_week(_):
    return datetime.datetime.now().strftime("%A")

def days_until(date_str):
    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 = [
    Tool(
        name="current_date",
        func=current_date,
        description="Returns the current date in YYYY-MM-DD format."
    ),
    Tool(
        name="current_day_of_week",
        func=day_of_week,
        description="Returns the current day of the week (e.g., Monday, Tuesday)."
    ),
    Tool(
        name="days_until",
        func=days_until,
        description="Returns the number of days from today until a given date (input format: YYYY-MM-DD)."
    )
]

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}"

transcribe_audio_tool = Tool(
        name="transcribe_audio", 
        func=transcribe_audio,
        description="Transcribes an audio file from a local file or a URL"
)