Alexander Seifert commited on
Commit
e414a67
1 Parent(s): cf239e0
Files changed (1) hide show
  1. app.py +60 -26
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import base64
 
2
  import os
3
 
4
  import modal
@@ -11,43 +12,76 @@ run_transcription = modal.lookup("ffpub-transcription", "run_transcription")
11
  st.set_page_config(page_title="Speech to Text Transcription App")
12
 
13
 
14
- @st.cache
15
  def transcribe(url, audio_b64):
16
  return run_transcription.call(url=url, audio_b64=audio_b64)
17
 
18
 
 
 
 
 
 
 
 
 
19
  def run():
 
 
 
 
 
 
20
  password = st.text_input("Zugriffscode (siehe oben)")
21
- audio_file = st.file_uploader(
22
- "Datei auswählen", type=[".wav", ".mp3", ".flac", ".m4a", ".ogg"]
23
- )
24
- url = st.text_input(
25
- "URL (e.g. YouTube video, Dropbox file, etc.)",
26
- value="",
27
- )
 
 
 
 
 
 
28
  # https://www.youtube.com/watch?v=pLAaQO1iPz0
29
- submit_button = st.button(
30
- label="Transkribieren", disabled=(not audio_file and not url)
 
 
31
  )
32
 
33
- if password not in [os.environ["PASSWORD"], os.environ["ROOT_PASSWORD"]]:
34
- st.error("Zugriffscode ist falsch.")
35
- st.stop()
36
-
37
- if audio_file is not None:
38
- st.audio(audio_file)
39
- cutoff = None if password == os.environ["ROOT_PASSWORD"] else 60_000
40
- audio_file = AudioSegment.from_file(audio_file)[:cutoff]
41
- audio_b64 = base64.b64encode(audio_file.export().read()).decode("ascii")
42
- if url:
43
- st.video(url)
44
-
45
- if submit_button:
46
- audio_b64 = None
47
- transcription = transcribe(url, audio_b64)
 
 
 
 
 
48
  for seg in transcription["text"].split("\n\n"):
49
  st.write(seg)
50
- st.json(transcription)
 
 
 
 
 
 
51
 
52
 
53
  try:
 
1
  import base64
2
+ import json
3
  import os
4
 
5
  import modal
 
12
  st.set_page_config(page_title="Speech to Text Transcription App")
13
 
14
 
15
+ @st.cache(show_spinner=False)
16
  def transcribe(url, audio_b64):
17
  return run_transcription.call(url=url, audio_b64=audio_b64)
18
 
19
 
20
+ def password_is_correct(password):
21
+ return password in [os.environ["PASSWORD"], os.environ["ROOT_PASSWORD"]]
22
+
23
+
24
+ def input_is_ready(password, audio_file, url):
25
+ return password_is_correct(password) and (audio_file or url)
26
+
27
+
28
  def run():
29
+ submit_button = False
30
+ if "is_expanded" not in st.session_state:
31
+ st.session_state["is_expanded"] = True
32
+
33
+ # expander = st.expander("Einstellungen", expanded=st.session_state["is_expanded"])
34
+ # with expander:
35
  password = st.text_input("Zugriffscode (siehe oben)")
36
+ url = audio_file = None
37
+
38
+ col1, col2 = st.columns([1, 3])
39
+ type = col1.radio("", ["URL", "Datei-Upload"])
40
+ if type == "URL":
41
+ url = col2.text_input(
42
+ "URL (e.g. YouTube video, Dropbox file, etc.)",
43
+ value="",
44
+ )
45
+ else:
46
+ audio_file = col2.file_uploader(
47
+ "Datei auswählen", type=[".wav", ".mp3", ".flac", ".m4a", ".ogg"]
48
+ )
49
  # https://www.youtube.com/watch?v=pLAaQO1iPz0
50
+ submit_button = col1.button(
51
+ label="Transkribieren"
52
+ + (" (Zugriffscode inkorrekt)" if not password_is_correct(password) else ""),
53
+ disabled=(not password_is_correct(password) or (not audio_file and not url)),
54
  )
55
 
56
+ audio_b64 = None
57
+ if audio_file or url:
58
+ with st.expander("Medien-Preview"):
59
+ if audio_file:
60
+ st.audio(audio_file)
61
+ cutoff = None if password == os.environ["ROOT_PASSWORD"] else 60_000
62
+ audio_file = AudioSegment.from_file(audio_file)[:cutoff]
63
+ audio_b64 = base64.b64encode(audio_file.export().read()).decode("ascii")
64
+ if url:
65
+ st.video(url)
66
+
67
+ if input_is_ready(password, audio_file, url) and submit_button:
68
+ # my_bar = st.progress(0)
69
+ # for percent_complete in range(100):
70
+ # time.sleep(1)
71
+ # my_bar.progress(percent_complete + 1)
72
+
73
+ with st.spinner("Transkription läuft..."):
74
+ transcription = transcribe(url, audio_b64)
75
+
76
  for seg in transcription["text"].split("\n\n"):
77
  st.write(seg)
78
+
79
+ st.download_button(
80
+ label="OTR-Datei herunterladen",
81
+ data=json.dumps(transcription["otr"], indent=2, ensure_ascii=False),
82
+ file_name="transkript.otr",
83
+ mime="application/json",
84
+ )
85
 
86
 
87
  try: