forked from leopoldpoldus/streamlit_whisper_transcription
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.py
116 lines (87 loc) · 3.39 KB
/
app.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
import
import sys
import datetime
import openai
import streamlit as st
from audio_recorder_streamlit import audio_recorder
working_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(working_dir)
openai.api_key = os.getenv("OPENAI_API_KEY")
def transcribe(audio_file):
transcript = openai.Audio.transcribe("whisper-1", audio_file)
return transcript
def summarize(text):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=(
f"Please summarize the following text:\n"
f"{text}"
),
temperature=0.5,
max_tokens=260,
)
return response.choices[0].text.strip()
st.text("Whisper Transcription and Summarization")
st.sidebar.title("Whisper Transcription and Summarization")
# Explanation of the app
st.sidebar.markdown("""
This is an app that allows you to transcribe audio files using the OpenAI API.
You can either record audio using the 'Record Audio' tab, or upload an audio file
using the 'Upload Audio' tab. Once you have recorded or uploaded an audio file,
click the 'Transcribe' button to transcribe the audio and generate a summary of the
transcript. The transcript and summary can be downloaded using the 'Download Transcript'
and 'Download Summary' buttons respectively.
""")
# tab record audio and upload audio
tab1, tab2 = st.tabs(["Record Audio", "Upload Audio"])
with tab1:
audio_bytes = audio_recorder(pause_threshold=180)
if audio_bytes:
st.audio(audio_bytes, format="audio/wav")
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# save audio file to mp3
with open(f"audio_{timestamp}.mp3", "wb") as f:
f.write(audio_bytes)
with tab2:
audio_file = st.file_uploader("Upload Audio", type=["mp3", "mp4", "wav", "m4a"])
if audio_file:
# st.audio(audio_file.read(), format={audio_file.type})
timestamp = timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# save audio file with correct extension
with open(f"audio_{timestamp}.{audio_file.type.split('/')[1]}", "wb") as f:
f.write(audio_file.read())
if st.button("Transcribe"):
# check if audio file exists
audio_files = [f for f in os.listdir(".") if f.startswith("audio")]
if not audio_files:
st.warning("Please record or upload an audio file first.")
else:
# find newest audio file
audio_file_path = max(
audio_files,
key=os.path.getctime,
)
# transcribe
audio_file = audio_file.read()
transcript = transcribe(audio_file)
text = transcript["text"]
st.header("Transcript")
st.write(text)
# summarize
summary = summarize(text)
st.header("Summary")
st.write(summary)
# save transcript and summary to text files
with open("transcript.txt", "w") as f:
f.write(text)
with open("summary.txt", "w") as f:
f.write(summary)
# download transcript and summary
st.download_button('Download Transcript', text)
st.download_button('Download Summary', summary)
# delete audio and text files when leaving app
if not st.session_state.get('cleaned_up'):
files = [f for f in os.listdir(".") if f.startswith("audio") or f.endswith(".txt")]
for file in files:
os.remove(file)
st.session_state['cleaned_up'] = True