-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
135 lines (108 loc) · 4.66 KB
/
main.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
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
import os
import whisper
import streamlit as st
from pydub import AudioSegment
import openai
from dotenv import load_dotenv
from pathlib import Path
load_dotenv()
st.set_page_config(
page_title="Whisplit",
page_icon="🔊",
layout="wide",
initial_sidebar_state="auto",
)
upload_path = "./data/uploads/"
download_path = "./data/downloads/"
transcript_path = "./data/transcripts/"
def create_directory(directory_path):
path = Path(directory_path)
if not path.is_dir():
path.mkdir(parents=True)
for d in [upload_path, download_path, transcript_path]:
create_directory(d)
@st.cache_data()
# @st.cache(persist=True, allow_output_mutation=False, show_spinner=True, suppress_st_warning=True)
def to_mp3(audio_file, output_audio_file, upload_path, download_path):
# get file extension using pathlib
ext = Path(audio_file.name).suffix.lower()
# create an audio segment from the given file
audio_data = AudioSegment.from_file(
os.path.join(upload_path, audio_file.name), format=ext[1:]
)
# export the audio segment to MP3 format
audio_data.export(
os.path.join(download_path, output_audio_file), format="mp3"
)
return output_audio_file
@st.cache_data()
# @st.cache(persist=True, allow_output_mutation=False, show_spinner=True, suppress_st_warning=True)
def process_audio(filename, model_type):
model = whisper.load_model(model_type)
result = model.transcribe(filename)
return result["text"]
@st.cache_data()
# @st.cache(persist=True, allow_output_mutation=False, show_spinner=True, suppress_st_warning=True)
def save_transcript(transcript_data, txt_file):
with open(os.path.join(transcript_path, txt_file), "w") as f:
f.write(transcript_data)
st.title("🔊 OpenAI Whisper 🔊")
st.info('✨ Supports all popular audio formats - WAV, MP3, MP4, OGG, WMA, AAC, FLAC, FLV 😉')
st.markdown("First upload your audio file and then select the model type. \nThen click on the button to transcribe.")
uploaded_file = st.file_uploader("Upload audio file", type=[
"wav", "mp3", "ogg", "wma", "aac", "flac", "mp4", "flv"])
audio_file = None
if uploaded_file is not None:
audio_bytes = uploaded_file.read()
with open(os.path.join(upload_path, uploaded_file.name), "wb") as f:
f.write((uploaded_file).getbuffer())
with st.spinner(f"Processing Audio ... 💫"):
output_audio_file = uploaded_file.name.split('.')[0] + '.mp3'
output_audio_file = to_mp3(
uploaded_file, output_audio_file, upload_path, download_path)
audio_file = open(os.path.join(download_path, output_audio_file), 'rb')
audio_bytes = audio_file.read()
print("Opening ", audio_file)
st.markdown("---")
col1, col2 = st.columns(2)
with col1:
st.markdown("Feel free to play your uploaded audio file 🎼")
st.audio(audio_bytes)
with col2:
whisper_model_type = st.radio(
"Please choose your model type", ('Tiny', 'Base', 'Small', 'Medium', 'Large'))
if st.button("Generate Transcript"):
with st.spinner(f"Generating Transcript... 💫"):
transcript = process_audio(str(os.path.abspath(os.path.join(
download_path, output_audio_file))), whisper_model_type.lower())
# print(transcript)
if transcript is not None:
st.header("Transcript:")
st.markdown(transcript)
st.success('✅ Successful !!')
# if st.button("Generate Transcript and Classification"):
# with st.spinner(f"Generating Transcript... 💫"):
# transcript = process_audio(str(os.path.abspath(os.path.join(
# download_path, output_audio_file))), whisper_model_type.lower())
# print(transcript)
# if transcript is not None:
# st.header("Transcript:")
# st.text(transcript)
# openai.api_key = os.getenv("OPENAI_API_KEY")
# response = openai.Completion.create(
# model="text-davinci-002",
# prompt=f"\n\n\n\n {transcript} \n\n",
# temperature=0.7,
# max_tokens=256,
# top_p=1,
# frequency_penalty=0,
# presence_penalty=0
# )
# st.header("Sentiment analysis:")
# st.caption(
# "very negativ, negativ, neutral, positive, very positive")
# st.text("Classified as:" + response.choices[0].text)
# st.balloons()
# st.success('✅ Successful !!')
else:
st.warning('⚠ Please upload your audio file 😯')