-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
58 lines (50 loc) · 1.76 KB
/
audio.py
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
from dotenv import load_dotenv
from icecream import ic # noqa: F401
from openai import OpenAI
from rich.console import Console
from utils import display_output, handle_error
load_dotenv()
client = OpenAI()
def transcribe_audio(audio_file, verbose=False):
"""
Generate a transcription from an audio file.
Args:
audio_file (str): The path to the audio file.
verbose (bool, optional): Whether to display verbose output. Defaults to False.
"""
try:
console = Console()
with console.status("[bold green]transcribing..."):
transcript = client.audio.transcriptions.create(
file=open(audio_file, "rb"),
model="whisper-1",
response_format="text",
)
display_output(
"Below is the content of the given audio file in text form:\n", "yellow"
)
display_output(f"{transcript}\n")
except Exception as e:
handle_error(e, verbose)
def translate_audio(audio_file, verbose=False):
"""
Generate a translated transcription from an audio file.
Args:
audio_file (str): The path to the audio file.
verbose (bool, optional): Whether to display verbose output. Defaults to False.
"""
try:
console = Console()
with console.status("[bold green]translating..."):
translation = client.audio.translations.create(
file=open(audio_file, "rb"),
model="whisper-1",
response_format="text",
)
display_output(
"Below is the translated content of the given audio file in text form:\n",
"yellow",
)
display_output(f"{translation}\n")
except Exception as e:
handle_error(e, verbose)