yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
6815848
v1.0.0-beta03
1import io 2import joblib 3from logger import log , log_err 4import numpy as np 5import pandas as pd 6from pathlib import Path 7import pronouncing 8import re 9import sys 10 11def count_syllables ( word ) : 12"""Count syllables in a word using pronouncing library with regex fallback.""" 13phones = pronouncing . phones_for_word ( word . lower ()) 14return pronouncing . syllable_count ( phones [ 0 ]) 15def text_syllable_count ( text ) : 16"""Count total syllables in text.""" 17words = re . findall ( r'\b\w+\b' , text ) 18return sum ( count_syllables ( word ) for word in words ) 19class HallucinationFilter : 20"""Filter for detecting hallucinated segments in speech-to-text output.""" 21def __init__ ( self , model_path : Path = None ) : 22""" 23Initialize the hallucination filter. 24Args: 25model_path: Optional path to the model file. If not provided, 26uses the default path. 27""" 28self . model = None 29self . threshold = None 30self . features = None 31# Get the project root directory 32app_root = Path ( __file__ ) . resolve () . parent 33project_root = app_root . parent 34# Use provided path or default 35if model_path is None : 36model_path = project_root / "Models" / "thankyou_filter_gb.pkl" 37# Try to load the model 38log_err ( f"Loading hallucination filter" ) 39bundle = joblib . load ( model_path ) 40self . model = bundle [ "model" ] 41self . threshold = bundle [ "threshold" ] 42self . features = bundle [ "features" ] 43log_err ( f"Loaded hallucination filter model from { model_path } " ) 44def is_hallucination ( self , segment ) -> bool : 45""" 46Check if a segment is likely a hallucination. 47Returns False if model is not available. 48Args: 49segment: A segment object with attributes avg_logprob, audio_len_s, 50no_speech_prob, compression_ratio, text, start, and end. 51Returns: 52bool: True if the segment is likely a hallucination, False otherwise. 53""" 54# Calculate text-based features 55text = getattr ( segment , 'text' , '' ) 56duration = segment . audio_len_s 57raw_duration = segment . end_ts - segment . start_ts 58n_syllables = text_syllable_count ( text ) 59sps = n_syllables / duration 60raw_sps = n_syllables / raw_duration 61duration_ratio = raw_duration / duration 62X = pd . DataFrame ([[ 63segment . avg_logprob , 64segment . no_speech_prob , 65segment . compression_ratio , 66np . log1p ( duration ) , 67np . log1p ( sps ) , 68np . log1p ( raw_duration ) , 69np . log1p ( raw_sps ) , 70duration_ratio , 71segment . avg_logprob * duration 72]] , columns = self . features ) 73# Get probability 74prob = self . model . predict_proba ( X )[ 0 , 1 ] 75return prob >= self . threshold