import gradio as gr import torch import torchaudio from pydub import AudioSegment from transformers import Wav2Vec2Processor, Wav2Vec2ForSequenceClassification import os # Load model and processor model_id = "kh2001/wav2vec2-stutter-test2" processor = Wav2Vec2Processor.from_pretrained(model_id) model = Wav2Vec2ForSequenceClassification.from_pretrained(model_id) # Convert any audio file to 16kHz mono WAV def convert_audio(file_path): if file_path is None or not os.path.exists(file_path): raise ValueError("No valid audio file provided.") audio = AudioSegment.from_file(file_path) audio = audio.set_frame_rate(16000).set_channels(1) wav_path = "converted.wav" audio.export(wav_path, format="wav") return wav_path # Inference logic def detect_stuttering(audio_file): try: wav_path = convert_audio(audio_file) waveform, sr = torchaudio.load(wav_path) inputs = processor(waveform.squeeze(), sampling_rate=sr, return_tensors="pt") with torch.no_grad(): logits = model(**inputs).logits pred_id = torch.argmax(logits).item() label_raw = model.config.id2label[pred_id] label_human = "Normal" if label_raw == "LABEL_0" else "Stutter" return f"Prediction: {label_human} (Model Label: {label_raw})" except Exception as e: return f"Error: {str(e)}" # Gradio UI gr.Interface( fn=detect_stuttering, inputs=gr.Audio(type="filepath", label="Upload Your Audio File"), outputs=gr.Text(label="Stuttering Detection Result"), title="Wav2Vec2 Stuttering Detector", description="Upload a speech file (WAV, MP3, AAC, M4A, etc.) to detect if it contains stuttering. Powered by a fine-tuned Wav2Vec2 model.", ).launch()