-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
97 lines (73 loc) · 2.66 KB
/
init.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
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
import sys
import subprocess
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
AUDIO_FILE_PATH = None
LANGUAGE = "English" # Default language
def print_help():
help_message = """
Usage: python script.py <path_to_audio_file> [options]
Options:
-h, --help Show this help message and exit
-l, --language Specify the language for the summary (default is English)
Description:
This script converts an audio file to FLAC format, transcribes the audio using OpenAI's Whisper model,
and summarizes the transcription using OpenAI's GPT-4 model. The converted FLAC file is deleted after processing.
Arguments:
<path_to_audio_file> Path to the audio file you want to transcribe and summarize.
"""
print(help_message)
# Ensure the audio file path is provided or help is requested
if len(sys.argv) < 2 or "-h" in sys.argv or "--help" in sys.argv:
print_help()
sys.exit(0)
for i, arg in enumerate(sys.argv):
if arg not in ("-l", "--language"):
if AUDIO_FILE_PATH is None and arg not in (sys.argv[0], "-h", "--help"):
AUDIO_FILE_PATH = arg
if arg in ("-l", "--language") and i + 1 < len(sys.argv):
LANGUAGE = sys.argv[i + 1]
if AUDIO_FILE_PATH is None:
print("Error: Path to audio file is required.")
print_help()
sys.exit(1)
def convert_audio_file(file_path):
converted_file = "converted_audio.flac"
subprocess.run(
["ffmpeg", "-i", file_path, "-ar", "16000", "-ac", "1", converted_file],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
return converted_file
def transcribe_audio(file_path):
with open(file_path, "rb") as audio_file:
response = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
)
return response.text
def summarize_transcription(transcription_text):
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"Please summarize this text in {LANGUAGE}"},
{"role": "user", "content": transcription_text},
],
)
return response.choices[0].message.content
def main():
try:
converted_file_path = convert_audio_file(AUDIO_FILE_PATH)
transcription = transcribe_audio(converted_file_path)
summary = summarize_transcription(transcription)
print("Transcription:")
print(transcription, "\n")
print("Summary:")
print(summary, "\n")
finally:
if os.path.exists(converted_file_path):
os.remove(converted_file_path)
if __name__ == "__main__":
main()