Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import whisper
|
| 5 |
+
from TTS.api import TTS
|
| 6 |
+
|
| 7 |
+
# 🧠 Lade Sprach-KI Modell (z. B. Mistral)
|
| 8 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
torch_dtype=torch.float16
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# 🎤 Sprach-zu-Text Modell (OpenAI Whisper)
|
| 17 |
+
stt_model = whisper.load_model("base")
|
| 18 |
+
|
| 19 |
+
# 🗣️ Text-zu-Sprache Modell (deutsche Stimme)
|
| 20 |
+
tts_model = TTS(model_name="tts_models/de/thorsten/tacotron2-DCA", progress_bar=False, gpu=torch.cuda.is_available())
|
| 21 |
+
|
| 22 |
+
# 🧩 Chat-Verlauf und Antwortgenerierung
|
| 23 |
+
def chat_with_ai(prompt, history=[]):
|
| 24 |
+
full_prompt = ""
|
| 25 |
+
for user, bot in history:
|
| 26 |
+
full_prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 27 |
+
full_prompt += f"User: {prompt}\nAssistant:"
|
| 28 |
+
|
| 29 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 30 |
+
outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.7, top_p=0.95)
|
| 31 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
+
answer = reply.split("Assistant:")[-1].strip()
|
| 33 |
+
history.append((prompt, answer))
|
| 34 |
+
return answer, history
|
| 35 |
+
|
| 36 |
+
# 🎤 Spracheingabe verarbeiten (Speech to Text)
|
| 37 |
+
def speech_to_text(audio_path):
|
| 38 |
+
audio = whisper.load_audio(audio_path)
|
| 39 |
+
audio = whisper.pad_or_trim(audio)
|
| 40 |
+
mel = whisper.log_mel_spectrogram(audio).to(stt_model.device)
|
| 41 |
+
result = stt_model.decode(mel)
|
| 42 |
+
return result.text
|
| 43 |
+
|
| 44 |
+
# 🗣️ Text in Sprache umwandeln
|
| 45 |
+
def text_to_speech(text):
|
| 46 |
+
tts_model.tts_to_file(text=text, file_path="tts_output.wav")
|
| 47 |
+
return "tts_output.wav"
|
| 48 |
+
|
| 49 |
+
# 🧩 Gradio Oberfläche
|
| 50 |
+
with gr.Blocks(title="Meine KI") as demo:
|
| 51 |
+
gr.Markdown("## 🤖 Meine eigene KI (deutsch, mit Stimme)")
|
| 52 |
+
|
| 53 |
+
chatbot = gr.Chatbot()
|
| 54 |
+
text_input = gr.Textbox(label="💬 Nachricht eingeben")
|
| 55 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="🎤 Spracheingabe")
|
| 56 |
+
audio_output = gr.Audio(label="🗣️ KI-Antwort als Audio", type="filepath")
|
| 57 |
+
state = gr.State([])
|
| 58 |
+
|
| 59 |
+
def handle_text(message, history):
|
| 60 |
+
reply, updated_history = chat_with_ai(message, history)
|
| 61 |
+
voice = text_to_speech(reply)
|
| 62 |
+
return reply, updated_history, voice
|
| 63 |
+
|
| 64 |
+
def handle_audio(audio, history):
|
| 65 |
+
transcribed = speech_to_text(audio)
|
| 66 |
+
reply, updated_history = chat_with_ai(transcribed, history)
|
| 67 |
+
voice = text_to_speech(reply)
|
| 68 |
+
return reply, updated_history, voice
|
| 69 |
+
|
| 70 |
+
text_input.submit(handle_text, [text_input, state], [chatbot, state, audio_output])
|
| 71 |
+
audio_input.change(handle_audio, [audio_input, state], [chatbot, state, audio_output])
|
| 72 |
+
|
| 73 |
+
demo.launch()
|