-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
470 lines (338 loc) · 17.3 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
### Ragnar
app_version = "0.6.0"
### Author: Micke Kring
### Contact: mikael.kring@ri.se
### Fun fact: The app is named after Ragnar Sohlman, the assistant
### of Alfred Nobel.
### https://sv.wikipedia.org/wiki/Ragnar_Sohlman
import os
import streamlit as st
from datetime import datetime
from sys import platform
import hashlib
# Creating Word documents
from docx import Document
# Streamlit Audio recorder
from audiorecorder import audiorecorder
# Importing functions from functions.py, transcribe.py
# and translate.py
from functions import convert_to_mono_and_compress
from transcribe import transcribe_with_whisper_stable
from translate import translate_with_whisper_stable
# Checks OS for future reference (not used now)
if platform == "linux" or platform == "linux2":
os_version = "linux"
elif platform == "darwin":
os_version = "macos"
elif platform == "win32":
os_version = "windows"
print("\n\n--- --- --- ---\nSTART: App start")
print(f"OS Version: {os_version}")
### INITIAL VARIABLES
# Creates folder if they don't exist
os.makedirs("audio", exist_ok=True) # Where audio/video files are stored for transcription
os.makedirs("text", exist_ok=True) # Where transcribed document are beeing stored
# Check and set default values if not set in session_state
# of Streamlit
if "translation" not in st.session_state: # If audio has been translated
st.session_state["translation"] = False
if "cpu_vs_gpu" not in st.session_state: # If user device has GPU support
st.session_state["cpu_vs_gpu"] = False
if "spoken_language" not in st.session_state: # What language source audio is in
st.session_state["spoken_language"] = "Automatiskt"
if "transcribe_model" not in st.session_state: # What model of Whisper to use
st.session_state["transcribe_model"] = "Stor (bäst kvalitet)"
if "file_name_converted" not in st.session_state: # Audio file name
st.session_state["file_name_converted"] = None
# Checking if uploaded or recorded audio file has been transcribed
def compute_file_hash(uploaded_file):
print("\nSTART: Check if audio file has been transcribed - hash")
# Compute the MD5 hash of a file
hasher = hashlib.md5()
for chunk in iter(lambda: uploaded_file.read(4096), b""):
hasher.update(chunk)
uploaded_file.seek(0) # Reset the file pointer to the beginning
print("DONE: Check if audio file has been transcribed - hash")
return hasher.hexdigest()
### MAIN APP ###########################
# Page configuration
st.set_page_config(
page_title="Ragnar",
page_icon=None,
layout="centered",
initial_sidebar_state="auto"
)
def main():
global translation
global model_map_transcribe_model
### SIDEBAR
# Sidebar image of Ragnar
st.sidebar.image("images/ragge3.png", width = 220)
###### SIDEBAR SETTINGS
st.sidebar.header("Inställningar")
st.sidebar.markdown("")
# Dropdown menu - choose Whisper model
transcribe_model = st.sidebar.selectbox(
"Välj modell för transkribering",
["Stor (bäst kvalitet)", "Medium", "Bas (sämst kvalitet)"],
index=["Stor (bäst kvalitet)", "Medium", "Bas (sämst kvalitet)"].index(st.session_state["transcribe_model"]),
)
model_map_transcribe_model = {
"Bas (sämst kvalitet)": "base",
"Medium": "medium",
"Stor (bäst kvalitet)": "large"
}
# Dropdown menu - choose source language of audio
spoken_language = st.sidebar.selectbox(
"Välj språk som talas",
["Automatiskt", "Svenska", "Engelska", "Franska", "Tyska", "Spanska"],
index=["Automatiskt", "Svenska", "Engelska", "Franska", "Tyska", "Spanska"].index(st.session_state["spoken_language"]),
)
model_map_spoken_language = {
"Automatiskt": None,
"Svenska": "sv",
"Engelska": "en",
"Franska": "fr",
"Tyska": "de",
"Spanska": "sp"
}
# Update the session_state directly
st.session_state["transcribe_model"] = transcribe_model
st.session_state["spoken_language"] = spoken_language
# Fututre functionality - Diarization
#number_of_speakers = st.sidebar.number_input("Antal talare", value = 1)
#st.sidebar.markdown("#")
#Toggle switch for tranlation to english, if source audio is not in english
translation = st.sidebar.toggle(
"Översättning engelska",
help = "Transkriberar text på orgnialspråk, men skapar även en översättning till engelska"
)
st.sidebar.markdown(
"#"
)
# Update the session_state directly
st.session_state["translation"] = translation
### ### ### ### ### ### ### ### ### ### ###
### MAIN PAGE
# Title
st.markdown("""
# Ragnar
### Din GDPR- och sekretessäkrade transkriberare
""")
# CREATE TWO TABS FOR FILE UPLOAD VS RECORDED AUDIO
tab1, tab2 = st.tabs(["Ladda upp", "Spela in"])
# FILE UPLOADER
with tab1:
uploaded_file = st.file_uploader(
"Ladda upp din ljud- eller videofil här",
type=["mp3", "wav", "flac", "mp4", "m4a", "aifc"],
help="Max 2GB stora filer", label_visibility="collapsed",
)
if uploaded_file:
# Checks if uploaded file has already been transcribed
current_file_hash = compute_file_hash(uploaded_file)
# If the uploaded file hash is different from the one in session state, reset the state
if "file_hash" not in st.session_state or st.session_state.file_hash != current_file_hash:
st.session_state.file_hash = current_file_hash
if "transcribed" in st.session_state:
del st.session_state.transcribed
# If audio has not been transcribed
if "transcribed" not in st.session_state:
# Sends audio to be converted to mp3 and compressed
with st.spinner('Din ljudfil är lite stor. Jag ska bara komprimera den lite först...'):
st.session_state.file_name_converted = convert_to_mono_and_compress(uploaded_file, uploaded_file.name)
st.success('Inspelning komprimerad och klar. Startar transkribering.')
# Transcribes audio with Whisper
with st.spinner('Transkriberar. Det här kan ta ett litet tag beroende på hur lång inspelningen är...'):
st.session_state.transcribed = transcribe_with_whisper_stable(st.session_state.file_name_converted,
uploaded_file.name,
model_map_transcribe_model[st.session_state["transcribe_model"]],
model_map_spoken_language[st.session_state["spoken_language"]])
st.success('Transkribering klar.')
st.balloons()
# If translation switch is on, translates audio
if st.session_state["translation"]:
with st.spinner('Översätter. Det här kan ta ett litet tag beroende på hur lång inspelningen är...'):
st.session_state.transcribed_en = translate_with_whisper_stable(st.session_state.file_name_converted,
uploaded_file.name,
model_map_transcribe_model[st.session_state["transcribe_model"]])
st.success('Översättning klar.')
st.balloons()
# If audio has been translated, creates Word document with translation
if st.session_state["translation"]:
document = Document()
document.add_paragraph(st.session_state.transcribed_en)
document.save('text/' + uploaded_file.name + '_en.docx')
with open("text/" + uploaded_file.name + "_en.docx", "rb") as template_file_en:
template_byte_en = template_file_en.read()
# Creates a Word document with the transcribed text
document = Document()
document.add_paragraph(st.session_state.transcribed)
document.save('text/' + uploaded_file.name + '.docx')
with open("text/" + uploaded_file.name + ".docx", "rb") as template_file:
template_byte = template_file.read()
# Creates a grid of four columns for the different transcribed document download buttons
col1, col2, col3, col4 = st.columns(4)
# Text
with col1:
with open('text/' + uploaded_file.name + '.txt', "rb") as file_txt:
st.download_button(
label = ":flag-se: Ladda ned text",
data = file_txt,
file_name = uploaded_file.name + '.txt',
mime = 'text/plain',
)
# Word
with col2:
st.download_button(
label = ":flag-se: Ladda ned word",
data = template_byte,
file_name = uploaded_file.name + '.docx',
mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
)
# srt - subtitle
with col3:
with open('text/' + uploaded_file.name + '.srt', "rb") as file_srt:
st.download_button(
label = ":flag-se: Ladda ned srt",
data = file_srt,
file_name = uploaded_file.name + '.srt',
mime = 'text/plain',
)
# Json
with col4:
with open('text/' + uploaded_file.name + '.json', "rb") as file_json:
st.download_button(
label = ":flag-se: Ladda ned json",
data = file_json,
file_name = uploaded_file.name + '.json',
mime = 'application/json',
)
#If text is also translated it creates another row with four columns
if st.session_state["translation"]:
col5, col6, col7, col8 = st.columns(4)
# Text
with col5:
with open('text/' + uploaded_file.name + '_en.txt', "rb") as file_txt:
st.download_button(
label = ":flag-gb: Download text",
data = file_txt,
file_name = uploaded_file.name + '_en.txt',
mime = 'text/plain',
)
# Word
with col6:
st.download_button(
label = ":flag-gb: Download word",
data = template_byte_en,
file_name = uploaded_file.name + '_en.docx',
mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
)
st.markdown("### Transkribering")
if st.session_state.file_name_converted is not None:
st.audio(st.session_state.file_name_converted, format='audio/wav')
st.write(st.session_state.transcribed)
# AUDIO RECORDER ###### ###### ######
with tab2:
# Creates the audio recorder
audio = audiorecorder(start_prompt="Spela in", stop_prompt="Stoppa", pause_prompt="", key=None)
# The rest of the code in tab2 works the same way as in tab1, so it's not going to be
# commented.
if len(audio) > 0:
# To save audio to a file, use pydub export method
audio.export("audio/local_recording.wav", format="wav")
# Open the saved audio file and compute its hash
with open("audio/local_recording.wav", 'rb') as file:
current_file_hash = compute_file_hash(file)
# If the uploaded file hash is different from the one in session state, reset the state
if "file_hash" not in st.session_state or st.session_state.file_hash != current_file_hash:
st.session_state.file_hash = current_file_hash
if "transcribed" in st.session_state:
del st.session_state.transcribed
if "transcribed" not in st.session_state:
with st.spinner('Din ljudfil är lite stor. Jag ska bara komprimera den lite först...'):
st.session_state.file_name_converted = convert_to_mono_and_compress("audio/local_recording.wav", "local_recording.wav")
st.success('Inspelning komprimerad och klar. Startar transkribering.')
with st.spinner('Transkriberar. Det här kan ta ett litet tag beroende på hur lång inspelningen är...'):
st.session_state.transcribed = transcribe_with_whisper_stable(st.session_state.file_name_converted,
"local_recording.mp3",
model_map_transcribe_model[st.session_state["transcribe_model"]],
model_map_spoken_language[st.session_state["spoken_language"]]
)
st.success('Transkribering klar.')
st.balloons()
if st.session_state["translation"]:
with st.spinner('Översätter. Det här kan ta ett litet tag beroende på hur lång inspelningen är...'):
st.session_state.transcribed_en = translate_with_whisper_stable(st.session_state.file_name_converted,
"local_recording.mp3",
model_map_transcribe_model[st.session_state["transcribe_model"]]
)
st.success('Översättning klar.')
st.balloons()
if st.session_state["translation"]:
document = Document()
document.add_paragraph(st.session_state.transcribed_en)
document.save('text/' + uploaded_file.name + '_en.docx')
with open("text/" + uploaded_file.name + "_en.docx", "rb") as template_file_en:
template_byte_en = template_file_en.read()
local_recording_name = "local_recording.mp3"
document = Document()
document.add_paragraph(st.session_state.transcribed)
document.save('text/' + local_recording_name + '.docx')
with open("text/local_recording.mp3.docx", "rb") as template_file:
template_byte = template_file.read()
col1, col2, col3, col4 = st.columns(4)
with col1:
with open('text/' + local_recording_name + '.txt', "rb") as file_txt:
st.download_button(
label = ":flag-se: Ladda ned text",
data = file_txt,
file_name = local_recording_name + '.txt',
mime = 'text/plain',
)
with col2:
st.download_button(
label = ":flag-se: Ladda ned word",
data = template_byte,
file_name = local_recording_name + '.docx',
mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
)
with col3:
with open('text/' + local_recording_name+ '.srt', "rb") as file_srt:
st.download_button(
label = ":flag-se: Ladda ned srt",
data = file_srt,
file_name = local_recording_name + '.srt',
mime = 'text/plain',
)
with col4:
with open('text/' + local_recording_name + '.json', "rb") as file_json:
st.download_button(
label = ":flag-se: Ladda ned json",
data = file_json,
file_name = local_recording_name + '.json',
mime = 'application/json',
)
if st.session_state["translation"]:
col5, col6, col7, col8 = st.columns(4)
with col5:
with open('text/' + local_recording_name + '_en.txt', "rb") as file_txt:
st.download_button(
label = ":flag-gb: Download text",
data = file_txt,
file_name = local_recording_name + '_en.txt',
mime = 'text/plain',
)
with col6:
st.download_button(
label = ":flag-gb: Download word",
data = template_byte_en,
file_name = local_recording_name + '_en.docx',
mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
)
st.markdown("### Transkribering")
if st.session_state.file_name_converted is not None:
st.audio(st.session_state.file_name_converted, format='audio/wav')
st.write(st.session_state.transcribed)
if __name__ == "__main__":
main()