voicebot / app.py
mgokg's picture
Update app.py
51a7232 verified
raw
history blame
1.3 kB
import gradio as gr
import torch
from transformers import pipeline
# Laden der Modelle (einmalig beim Start)
device = "cuda" if torch.cuda.is_available() else "cpu"
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=device)
text_to_speech = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech", device=device)
def audio_to_audio_chatbot(audio):
if audio is None:
return None, "Bitte eine Audio-Datei hochladen."
# 1. Speech-to-Text
text = speech_to_text(audio)["text"]
print(f"User: {text}")
# 2. Text-to-Text (Hier wird ein einfacher Echo-Bot verwendet, kann durch ein komplexeres Modell ersetzt werden)
response_text = f"Du hast gesagt: {text}"
print(f"Bot: {response_text}")
# 3. Text-to-Speech
speech = text_to_speech(response_text)
return speech["audio"], response_text
if __name__ == "__main__":
iface = gr.Interface(
fn=audio_to_audio_chatbot,
inputs=gr.Audio(source="microphone", type="filepath"),
outputs=[gr.Audio(), gr.Textbox()],
title="Audio-zu-Audio-Chatbot (Streaming)",
description="Spreche in das Mikrofon und der Bot antwortet mit einer Audio-Ausgabe.",
live=True # Aktiviert Streaming
)
iface.launch()