Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Gerät auswählen (GPU, falls verfügbar, sonst CPU)
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# Lade das kleinere Modell
|
| 9 |
+
pipe = pipeline("text-generation", model="google/flan-t5-xxl", device=device)
|
| 10 |
+
|
| 11 |
+
def chat(message, history):
|
| 12 |
+
prompt = f"User: {message}\nAssistant:"
|
| 13 |
+
sequences = pipe(prompt, max_length=256)
|
| 14 |
+
response = sequences[0]['generated_text'].split("Assistant:")[1].strip()
|
| 15 |
+
history.append((message, response))
|
| 16 |
+
return history, history
|
| 17 |
+
|
| 18 |
+
def transcribe_and_send(audio, history):
|
| 19 |
+
if audio is None:
|
| 20 |
+
return history, "Keine Audioaufnahme erhalten."
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
| 24 |
+
transcription = asr(audio)["text"]
|
| 25 |
+
return chat(transcription, history)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Fehler bei der Transkription: {e}")
|
| 28 |
+
return history, "Fehler bei der Audioverarbeitung."
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
chatbot = gr.Chatbot()
|
| 32 |
+
state = gr.State([])
|
| 33 |
+
|
| 34 |
+
with gr.Row():
|
| 35 |
+
audio_input = gr.Audio(source="microphone", label="Sprachaufnahme")
|
| 36 |
+
text_input = gr.Textbox(placeholder="Nachricht eingeben...")
|
| 37 |
+
|
| 38 |
+
send_button = gr.Button("Senden (Text)")
|
| 39 |
+
clear_button = gr.Button("Chat löschen")
|
| 40 |
+
|
| 41 |
+
send_button.click(chat, [text_input, state], [state, chatbot])
|
| 42 |
+
audio_input.stop(transcribe_and_send, [audio_input, state], [state, chatbot])
|
| 43 |
+
clear_button.click(lambda: [], outputs=[chatbot])
|
| 44 |
+
text_input.submit(chat, [text_input, state], [state, chatbot])
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|