File size: 1,753 Bytes
3a69753
 
 
 
 
 
 
 
 
 
 
 
e3004c6
3a69753
e3004c6
 
3a69753
 
 
 
 
 
e3004c6
3a69753
e3004c6
 
 
3a69753
e3004c6
 
 
 
3a69753
e3004c6
 
 
 
 
 
 
 
3a69753
 
 
 
 
e3004c6
3a69753
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()