Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +39 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchaudio
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
from transformers import Wav2Vec2Processor, Wav2Vec2ForSequenceClassification
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load model and processor
|
| 9 |
+
model_id = "kh2001/wav2vec2-stutter-test2"
|
| 10 |
+
processor = Wav2Vec2Processor.from_pretrained(model_id)
|
| 11 |
+
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_id)
|
| 12 |
+
|
| 13 |
+
# Convert audio to WAV and 16kHz mono
|
| 14 |
+
def convert_audio(file_path):
|
| 15 |
+
audio = AudioSegment.from_file(file_path)
|
| 16 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 17 |
+
wav_path = "converted.wav"
|
| 18 |
+
audio.export(wav_path, format="wav")
|
| 19 |
+
return wav_path
|
| 20 |
+
|
| 21 |
+
# Inference function
|
| 22 |
+
def detect_stuttering(audio_file):
|
| 23 |
+
wav_path = convert_audio(audio_file)
|
| 24 |
+
waveform, sr = torchaudio.load(wav_path)
|
| 25 |
+
inputs = processor(waveform.squeeze(), sampling_rate=sr, return_tensors="pt")
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
logits = model(**inputs).logits
|
| 28 |
+
pred_id = torch.argmax(logits).item()
|
| 29 |
+
label = model.config.id2label[pred_id]
|
| 30 |
+
return f"Prediction: {label}"
|
| 31 |
+
|
| 32 |
+
# Gradio interface
|
| 33 |
+
gr.Interface(
|
| 34 |
+
fn=detect_stuttering,
|
| 35 |
+
inputs=gr.Audio(type="filepath", label="Upload Your Audio File"),
|
| 36 |
+
outputs=gr.Text(label="Stuttering Detection Result"),
|
| 37 |
+
title="Wav2Vec2 Stuttering Detector",
|
| 38 |
+
description="Upload a speech file (WAV, MP3, M4A, etc.) to detect if it contains stuttering. Powered by a fine-tuned Wav2Vec2 model."
|
| 39 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torchaudio
|
| 3 |
+
gradio
|
| 4 |
+
pydub
|