Spaces:
Runtime error
Runtime error
File size: 2,391 Bytes
a9bf4b2 |
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 |
import base64
import os
import time
from dataclasses import dataclass
from datetime import timedelta
import banana_dev as banana
import gradio as gr
from loguru import logger
from pydub import AudioSegment
api_key = os.environ["BANANA_API_KEY"]
model_key = os.environ["BANANA_MODEL_KEY"]
password = os.environ["PASSWORD"]
def transcribe(audio=None, url=None):
if audio:
audio_b64 = base64.b64encode(audio.export().read()).decode("ascii")
payload = {"audio_b64": audio_b64}
else:
payload = {"url": url}
response = banana.run(api_key, model_key, payload)
print(response)
if "error" in response:
raise gr.Error(response["error"])
# TODO: not sure why response dict contains multiple model outputs
return response["modelOutputs"][0]
def run_demo(password, url, file_upload):
if password not in [os.environ["PASSWORD"], os.environ["ROOT_PASSWORD"]]:
raise gr.Error("Der Zugriffscode ist falsch.")
if (url is not None) and (file_upload is not None):
logger.warning(
"Achtung: Sie haben sowohl eine URL angegeben als auch eine Datei hochgeladen."
" Wir verwenden nur die Datei, die Sie hochgeladen haben."
)
elif (url is None) and (file_upload is None):
raise gr.Error(
"Sie müssen entweder eine URL angeben oder eine Datei hochladen."
)
start = time.time()
if file_upload is not None:
cutoff = None if password == os.environ["ROOT_PASSWORD"] else 60_000
audio = AudioSegment.from_file(file_upload, format="mp3")[:cutoff]
transcription = transcribe(audio=audio, url=None)
else:
transcription = transcribe(audio=None, url=url)
logger.info(f"transcription took {time.time()-start:.3f}s")
return transcription["text"]
demo = gr.Interface(
fn=run_demo,
inputs=[
# gr.Textbox(label="Email", type="email"),
gr.Textbox(label="Zugriffscode (siehe oben)"),
# gr.Audio(source="microphone", type="filepath", label="Aufnehmen"),
gr.Textbox(label="URL (z.B. YouTube-Video, Dropbox-Datei, etc.)"),
gr.Audio(source="upload", type="filepath", label="Datei hochladen"),
],
outputs=gr.Textbox(label="Automatisches Transkript"),
allow_flagging="never",
css="footer {visibility: hidden} .meta-text {visibility: hidden}",
)
demo.launch()
|