Spaces:
Runtime error
Runtime error
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() | |