156 lines
5.2 KiB
Python
156 lines
5.2 KiB
Python
import whisper
|
|
import requests
|
|
import re
|
|
import json
|
|
import os
|
|
from datetime import datetime
|
|
|
|
from config import MISTY_IP
|
|
|
|
DATEN_DATEI = "session_daten.json"
|
|
FUELLWOERTER_DATEI = "fuellwoerter.json"
|
|
|
|
# Modell einmal beim Start laden
|
|
print("--- Lade Whisper-Modell ---")
|
|
_whisper_model = whisper.load_model("base")
|
|
|
|
def lade_fuellwoerter():
|
|
if os.path.exists(FUELLWOERTER_DATEI):
|
|
with open(FUELLWOERTER_DATEI, "r", encoding="utf-8") as f:
|
|
woerter = json.load(f)
|
|
return [rf'\b{w}\b' for w in woerter]
|
|
return [
|
|
r'\bäh\b', r'\bähm\b', r'\behm\b', r'\bmhm\b', r'\bhm\b',
|
|
r'\bhalt\b', r'\balso\b', r'\bsozusagen\b', r'\birgendwie\b',
|
|
]
|
|
|
|
def setze_status(status):
|
|
if os.path.exists(DATEN_DATEI):
|
|
with open(DATEN_DATEI, "r", encoding="utf-8") as f:
|
|
daten = json.load(f)
|
|
else:
|
|
daten = {"status": status, "sessions": []}
|
|
daten["status"] = status
|
|
with open(DATEN_DATEI, "w", encoding="utf-8") as f:
|
|
json.dump(daten, f, ensure_ascii=False, indent=2)
|
|
|
|
def speichere_session(text, anzahl, gefundene_woerter, tempo, feedback, gesicht):
|
|
if os.path.exists(DATEN_DATEI):
|
|
with open(DATEN_DATEI, "r", encoding="utf-8") as f:
|
|
daten = json.load(f)
|
|
else:
|
|
daten = {"status": "wartend", "sessions": []}
|
|
session = {
|
|
"nummer": len(daten["sessions"]) + 1,
|
|
"zeit": datetime.now().strftime("%H:%M"),
|
|
"text": text,
|
|
"fuellwoerter_anzahl": anzahl,
|
|
"gefundene_woerter": gefundene_woerter,
|
|
"tempo": tempo,
|
|
"feedback": feedback,
|
|
"gesicht": gesicht
|
|
}
|
|
daten["sessions"].append(session)
|
|
daten["status"] = "wartend"
|
|
with open(DATEN_DATEI, "w", encoding="utf-8") as f:
|
|
json.dump(daten, f, ensure_ascii=False, indent=2)
|
|
|
|
def analysiere(datei, echte_dauer=None):
|
|
setze_status("analysierend")
|
|
print("--- Whisper Analyse läuft ---")
|
|
result = _whisper_model.transcribe(
|
|
datei,
|
|
language="German",
|
|
initial_prompt="Dies ist eine Präsentation auf Deutsch. Äh, ähm, sozusagen, halt, also, irgendwie.",
|
|
temperature=0,
|
|
beam_size=5,
|
|
best_of=5
|
|
)
|
|
text_original = result["text"].strip()
|
|
text_lower = text_original.lower()
|
|
print(f"Erkannter Text: {text_original}")
|
|
|
|
# Füllwörter aus Datei laden (bei jeder Analyse aktuell)
|
|
FUELLWOERTER = lade_fuellwoerter()
|
|
|
|
anzahl = 0
|
|
gefundene_woerter = {}
|
|
for muster in FUELLWOERTER:
|
|
treffer = len(re.findall(muster, text_lower))
|
|
if treffer > 0:
|
|
wort = muster.replace(r'\b', '')
|
|
gefundene_woerter[wort] = treffer
|
|
print(f" → {wort}: {treffer}x")
|
|
anzahl += treffer
|
|
print(f"Füllwörter gesamt: {anzahl}")
|
|
|
|
anzahl_woerter = len(text_lower.split())
|
|
if echte_dauer and echte_dauer > 0:
|
|
tempo = round((anzahl_woerter / echte_dauer) * 60)
|
|
else:
|
|
segmente = result["segments"]
|
|
if segmente:
|
|
dauer = segmente[-1]["end"] - segmente[0]["start"]
|
|
tempo = round((anzahl_woerter / dauer) * 60) if dauer > 0 else 0
|
|
else:
|
|
tempo = 0
|
|
print(f"Sprechtempo: {tempo} Wörter/Minute")
|
|
|
|
return anzahl, text_original, gefundene_woerter, tempo
|
|
|
|
def formatiere_woerter(gefundene_woerter):
|
|
teile = []
|
|
for wort, anzahl in gefundene_woerter.items():
|
|
if anzahl == 1:
|
|
teile.append(f"{wort} einmal")
|
|
else:
|
|
teile.append(f"{wort} {anzahl}mal")
|
|
if len(teile) == 1:
|
|
return teile[0]
|
|
return ", ".join(teile[:-1]) + " und " + teile[-1]
|
|
|
|
def gib_feedback(anzahl, gefundene_woerter, tempo, text=""):
|
|
fuellwoerter_schlecht = anzahl >= 3
|
|
tempo_ok = 90 <= tempo <= 150
|
|
|
|
if not fuellwoerter_schlecht and tempo_ok:
|
|
img = "e_Love.jpg"
|
|
elif fuellwoerter_schlecht and not tempo_ok:
|
|
img = "e_Sadness.jpg"
|
|
else:
|
|
img = "e_Contempt.jpg"
|
|
|
|
if anzahl == 0:
|
|
fuellwort_msg = "Keine Füllwörter gefunden. Hervorragend!"
|
|
elif anzahl == 1:
|
|
woerter_text = formatiere_woerter(gefundene_woerter)
|
|
fuellwort_msg = f"Du hattest ein Füllwort. Du hast das Füllwort {woerter_text} benutzt."
|
|
elif anzahl <= 3:
|
|
woerter_text = formatiere_woerter(gefundene_woerter)
|
|
fuellwort_msg = f"Du hattest {anzahl} Füllwörter. Du hast benutzt: {woerter_text}."
|
|
else:
|
|
woerter_text = formatiere_woerter(gefundene_woerter)
|
|
fuellwort_msg = f"Du hattest {anzahl} Füllwörter. Du hast benutzt: {woerter_text}. Bitte übe noch etwas."
|
|
|
|
if tempo == 0:
|
|
tempo_msg = ""
|
|
elif tempo < 90:
|
|
tempo_msg = "Dein Sprechtempo war etwas zu langsam. Versuche etwas flüssiger zu sprechen."
|
|
elif tempo <= 150:
|
|
tempo_msg = "Dein Sprechtempo war sehr angenehm."
|
|
else:
|
|
tempo_msg = "Dein Sprechtempo war etwas zu schnell. Versuche dich etwas zu verlangsamen."
|
|
|
|
msg = fuellwort_msg
|
|
if tempo_msg:
|
|
msg += " " + tempo_msg
|
|
|
|
speichere_session(text, anzahl, gefundene_woerter, tempo, msg, img)
|
|
|
|
requests.post(f"http://{MISTY_IP}/api/images/display", json={"FileName": img})
|
|
requests.post(f"http://{MISTY_IP}/api/tts/speak", json={
|
|
"text": msg,
|
|
"voice": "de-de-x-deb-local"
|
|
})
|
|
print(f"Gesicht: {img}")
|
|
print(f"Misty sagt: {msg}")
|