Spaces:
Runtime error

Mathematician786 commited on
Commit
3a69753
·
verified ·
1 Parent(s): 2bf6761

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -39
app.py CHANGED
@@ -1,39 +1,43 @@
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()
 
 
 
 
 
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
+
30
+ label_raw = model.config.id2label[pred_id]
31
+ label_human = "Normal" if label_raw == "LABEL_0" else "Stutter"
32
+
33
+ return f"Prediction: {label_human} (Model Label: {label_raw})"
34
+
35
+
36
+ # Gradio interface
37
+ gr.Interface(
38
+ fn=detect_stuttering,
39
+ inputs=gr.Audio(type="filepath", label="Upload Your Audio File"),
40
+ outputs=gr.Text(label="Stuttering Detection Result"),
41
+ title="Wav2Vec2 Stuttering Detector",
42
+ description="Upload a speech file (WAV, MP3, M4A, etc.) to detect if it contains stuttering. Powered by a fine-tuned Wav2Vec2 model."
43
+ ).launch()