File size: 2,649 Bytes
8fc7f5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import os
import time

import banana_dev as banana
import gradio as gr
from loguru import logger

api_key = os.environ["BANANA_API_KEY"]
model_key = os.environ["BANANA_MODEL_KEY"]
password = os.environ["PASSWORD"]


def transcribe(email: str, file=None, url=None):
    if file:
        with open(file, "rb") as f:
            audio = f.read()
            audio_b64 = base64.b64encode(audio).decode("ISO-8859-1")
        payload = {"audio_b64": audio_b64}
    else:
        payload = {"url": url}
    payload["email"] = email
    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(email, password, microphone, file_upload):
    if password != os.environ["PASSWORD"]:
        raise gr.Error("Das Kennwort ist falsch.")

    if (microphone is not None) and (file_upload is not None):
        logger.warning(
            "Achtung: Sie haben sowohl eine Datei hochgeladen als auch über das Mikrofon aufgenommen."
            " Wir verwenden nur die Datei, die Sie hochgeladen haben."
        )

    elif (microphone is None) and (file_upload is None):
        raise gr.Error(
            "Sie müssen entweder eine Datei hochladen oder über das Mikrofon aufnehmen."
        )

    file = microphone if microphone is not None else file_upload

    start = time.time()
    transcription = transcribe(email, file)
    logger.info(f"transcription took {time.time()-start:.3f}s")
    return "\n\n".join([seg["text"].strip() for seg in transcription["segments"]])


demo = gr.Interface(
    fn=run_demo,
    inputs=[
        gr.Textbox(label="Email", type="email"),
        gr.Textbox(label="Zugriffs-Kennwort (siehe oben)"),
        gr.Audio(source="microphone", type="filepath", label="Aufnehmen"),
        gr.Audio(source="upload", type="filepath", label="Datei hochladen"),
    ],
    outputs=gr.Textbox(label="Transkription"),
    title="Transkriptionsservice",
    description="<p>Die untenstehende, automatische Transkription wurde von uns für interne Zwecke entwickelt."
    " Sie liefert die bei weitem beste Transkriptionsqualität bei Aufnahmen nicht-fachspezifischer Gespräche.</p>"
    "<p><strong>Bitte geben Sie Ihre Email-Adresse an, damit wir Ihnen die fertige Transkription per Email zusenden können.</strong>"
    " Wir werden Ihre Email-Adresse nicht für andere Zwecke verwenden.</p>"
    "",
    article="",
    allow_flagging="never",
    css="footer {visibility: hidden}",
)

demo.launch(share=True)