Spaces:
Runtime error
Runtime error
Alexander Seifert
commited on
Commit
•
a9bf4b2
1
Parent(s):
6e406cd
switch to streamlit app
Browse files- app.py +52 -122
- gradio-app.py +75 -0
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,129 +1,59 @@
|
|
1 |
import base64
|
|
|
2 |
import os
|
3 |
-
import time
|
4 |
-
from dataclasses import dataclass
|
5 |
-
from datetime import timedelta
|
6 |
|
7 |
-
import
|
8 |
-
import
|
|
|
9 |
from loguru import logger
|
10 |
from pydub import AudioSegment
|
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 |
-
def end_ts(self):
|
61 |
-
return timedelta_to_otr_timestamp(timedelta(seconds=self.end))
|
62 |
-
|
63 |
-
def __str__(self):
|
64 |
-
return f"{self.start_ts} {self.text}"
|
65 |
-
|
66 |
-
def to_otr(self):
|
67 |
-
sep = " "
|
68 |
-
return f'<p><span class="timestamp" data-timestamp="{self.start}">{self.start_ts}</span>{sep}{self.text}</p>'
|
69 |
-
|
70 |
-
|
71 |
-
def transcribe(audio=None, url=None):
|
72 |
-
if audio:
|
73 |
-
audio_b64 = base64.b64encode(audio.export().read()).decode("ascii")
|
74 |
-
payload = {"audio_b64": audio_b64}
|
75 |
-
else:
|
76 |
-
payload = {"url": url}
|
77 |
-
response = banana.run(api_key, model_key, payload)
|
78 |
-
print(response)
|
79 |
-
|
80 |
-
if "error" in response:
|
81 |
-
raise gr.Error(response["error"])
|
82 |
-
|
83 |
-
# TODO: not sure why response dict contains multiple model outputs
|
84 |
-
return response["modelOutputs"][0]
|
85 |
-
|
86 |
-
|
87 |
-
def run_demo(password, url, file_upload):
|
88 |
-
if password not in [os.environ["PASSWORD"], os.environ["ROOT_PASSWORD"]]:
|
89 |
-
raise gr.Error("Der Zugriffscode ist falsch.")
|
90 |
-
|
91 |
-
if (url is not None) and (file_upload is not None):
|
92 |
-
logger.warning(
|
93 |
-
"Achtung: Sie haben sowohl eine URL angegeben als auch eine Datei hochgeladen."
|
94 |
-
" Wir verwenden nur die Datei, die Sie hochgeladen haben."
|
95 |
-
)
|
96 |
-
|
97 |
-
elif (url is None) and (file_upload is None):
|
98 |
-
raise gr.Error(
|
99 |
-
"Sie müssen entweder eine URL angeben oder eine Datei hochladen."
|
100 |
)
|
101 |
-
|
102 |
-
start = time.time()
|
103 |
-
|
104 |
-
if file_upload is not None:
|
105 |
-
cutoff = None if password == os.environ["ROOT_PASSWORD"] else 60_000
|
106 |
-
audio = AudioSegment.from_file(file_upload, format="mp3")[:cutoff]
|
107 |
-
transcription = transcribe(audio=audio, url=None)
|
108 |
-
else:
|
109 |
-
transcription = transcribe(audio=None, url=url)
|
110 |
-
|
111 |
-
logger.info(f"transcription took {time.time()-start:.3f}s")
|
112 |
-
return transcription["text"]
|
113 |
-
|
114 |
-
|
115 |
-
demo = gr.Interface(
|
116 |
-
fn=run_demo,
|
117 |
-
inputs=[
|
118 |
-
# gr.Textbox(label="Email", type="email"),
|
119 |
-
gr.Textbox(label="Zugriffscode (siehe oben)"),
|
120 |
-
# gr.Audio(source="microphone", type="filepath", label="Aufnehmen"),
|
121 |
-
gr.Textbox(label="URL (z.B. YouTube-Video, Dropbox-Datei, etc.)"),
|
122 |
-
gr.Audio(source="upload", type="filepath", label="Datei hochladen"),
|
123 |
-
],
|
124 |
-
outputs=gr.Textbox(label="Automatisches Transkript"),
|
125 |
-
allow_flagging="never",
|
126 |
-
css="footer {visibility: hidden} .meta-text {visibility: hidden}",
|
127 |
-
)
|
128 |
-
|
129 |
-
demo.launch()
|
|
|
1 |
import base64
|
2 |
+
import json
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
+
import modal
|
6 |
+
import requests
|
7 |
+
import streamlit as st
|
8 |
from loguru import logger
|
9 |
from pydub import AudioSegment
|
10 |
|
11 |
+
# password = os.environ["PASSWORD"]
|
12 |
+
run_transcription = modal.lookup("ffpub-transcription", "run_transcription")
|
13 |
+
|
14 |
+
st.set_page_config(page_title="Speech to Text Transcription App")
|
15 |
+
|
16 |
+
|
17 |
+
@st.cache
|
18 |
+
def transcribe(url, audio_b64):
|
19 |
+
return run_transcription.call(url=url, audio_b64=audio_b64)
|
20 |
+
|
21 |
+
|
22 |
+
def run():
|
23 |
+
password = st.text_input("Zugriffscode (siehe oben)")
|
24 |
+
audio_file = st.file_uploader(
|
25 |
+
"Datei auswählen", type=[".wav", ".mp3", ".flac", ".m4a", ".ogg"]
|
26 |
+
)
|
27 |
+
url = st.text_input(
|
28 |
+
"URL (e.g. YouTube video, Dropbox file, etc.)",
|
29 |
+
value="",
|
30 |
+
)
|
31 |
+
# https://www.youtube.com/watch?v=pLAaQO1iPz0
|
32 |
+
submit_button = st.button(
|
33 |
+
label="Transkribieren", disabled=(not audio_file and not url)
|
34 |
+
)
|
35 |
+
|
36 |
+
if audio_file is not None:
|
37 |
+
st.audio(audio_file)
|
38 |
+
cutoff = None if password == "" else 60_000
|
39 |
+
audio_file = AudioSegment.from_file(audio_file)[:cutoff]
|
40 |
+
audio_b64 = base64.b64encode(audio_file.export().read()).decode("ascii")
|
41 |
+
if url:
|
42 |
+
st.video(url)
|
43 |
+
|
44 |
+
if submit_button:
|
45 |
+
audio_b64 = None
|
46 |
+
transcription = transcribe(url, audio_b64)
|
47 |
+
for seg in transcription["text"].split("\n\n"):
|
48 |
+
st.write(seg)
|
49 |
+
st.json(transcription)
|
50 |
+
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
try:
|
54 |
+
run()
|
55 |
+
except Exception as e:
|
56 |
+
logger.error(e)
|
57 |
+
st.error(
|
58 |
+
"Leider ist ein unerwarter Fehler aufgetreten. Ich könnte mir das Problem sofort ansehen, Sie erreichen mich unter alexander.seifert@gmail.com"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gradio-app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
from dataclasses import dataclass
|
5 |
+
from datetime import timedelta
|
6 |
+
|
7 |
+
import banana_dev as banana
|
8 |
+
import gradio as gr
|
9 |
+
from loguru import logger
|
10 |
+
from pydub import AudioSegment
|
11 |
+
|
12 |
+
api_key = os.environ["BANANA_API_KEY"]
|
13 |
+
model_key = os.environ["BANANA_MODEL_KEY"]
|
14 |
+
password = os.environ["PASSWORD"]
|
15 |
+
|
16 |
+
|
17 |
+
def transcribe(audio=None, url=None):
|
18 |
+
if audio:
|
19 |
+
audio_b64 = base64.b64encode(audio.export().read()).decode("ascii")
|
20 |
+
payload = {"audio_b64": audio_b64}
|
21 |
+
else:
|
22 |
+
payload = {"url": url}
|
23 |
+
response = banana.run(api_key, model_key, payload)
|
24 |
+
print(response)
|
25 |
+
|
26 |
+
if "error" in response:
|
27 |
+
raise gr.Error(response["error"])
|
28 |
+
|
29 |
+
# TODO: not sure why response dict contains multiple model outputs
|
30 |
+
return response["modelOutputs"][0]
|
31 |
+
|
32 |
+
|
33 |
+
def run_demo(password, url, file_upload):
|
34 |
+
if password not in [os.environ["PASSWORD"], os.environ["ROOT_PASSWORD"]]:
|
35 |
+
raise gr.Error("Der Zugriffscode ist falsch.")
|
36 |
+
|
37 |
+
if (url is not None) and (file_upload is not None):
|
38 |
+
logger.warning(
|
39 |
+
"Achtung: Sie haben sowohl eine URL angegeben als auch eine Datei hochgeladen."
|
40 |
+
" Wir verwenden nur die Datei, die Sie hochgeladen haben."
|
41 |
+
)
|
42 |
+
|
43 |
+
elif (url is None) and (file_upload is None):
|
44 |
+
raise gr.Error(
|
45 |
+
"Sie müssen entweder eine URL angeben oder eine Datei hochladen."
|
46 |
+
)
|
47 |
+
|
48 |
+
start = time.time()
|
49 |
+
|
50 |
+
if file_upload is not None:
|
51 |
+
cutoff = None if password == os.environ["ROOT_PASSWORD"] else 60_000
|
52 |
+
audio = AudioSegment.from_file(file_upload, format="mp3")[:cutoff]
|
53 |
+
transcription = transcribe(audio=audio, url=None)
|
54 |
+
else:
|
55 |
+
transcription = transcribe(audio=None, url=url)
|
56 |
+
|
57 |
+
logger.info(f"transcription took {time.time()-start:.3f}s")
|
58 |
+
return transcription["text"]
|
59 |
+
|
60 |
+
|
61 |
+
demo = gr.Interface(
|
62 |
+
fn=run_demo,
|
63 |
+
inputs=[
|
64 |
+
# gr.Textbox(label="Email", type="email"),
|
65 |
+
gr.Textbox(label="Zugriffscode (siehe oben)"),
|
66 |
+
# gr.Audio(source="microphone", type="filepath", label="Aufnehmen"),
|
67 |
+
gr.Textbox(label="URL (z.B. YouTube-Video, Dropbox-Datei, etc.)"),
|
68 |
+
gr.Audio(source="upload", type="filepath", label="Datei hochladen"),
|
69 |
+
],
|
70 |
+
outputs=gr.Textbox(label="Automatisches Transkript"),
|
71 |
+
allow_flagging="never",
|
72 |
+
css="footer {visibility: hidden} .meta-text {visibility: hidden}",
|
73 |
+
)
|
74 |
+
|
75 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
-
gradio
|
3 |
loguru
|
4 |
pydub
|
|
|
1 |
+
modal-client
|
|
|
2 |
loguru
|
3 |
pydub
|