HareemFatima
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,48 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
# Load audio classification model
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
st.write("
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
st.
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForTextToWaveform
|
3 |
+
|
4 |
+
# Load the audio classification model
|
5 |
+
audio_classification_model = pipeline("audio-classification", model="HareemFatima/distilhubert-finetuned-stutterdetection")
|
6 |
+
|
7 |
+
# Load the TTS tokenizer and model
|
8 |
+
tts_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
9 |
+
tts_model = AutoModelForTextToWaveform.from_pretrained("facebook/mms-tts-eng")
|
10 |
+
|
11 |
+
# Define a function to classify audio and generate speech
|
12 |
+
def classify_and_speak(audio_input):
|
13 |
+
# Classify the audio
|
14 |
+
classification_result = audio_classification_model(audio_input)
|
15 |
+
predicted_class = classification_result[0]["label"]
|
16 |
+
|
17 |
+
# Map predicted class to corresponding speech text
|
18 |
+
speech_text = map_class_to_speech(predicted_class)
|
19 |
+
|
20 |
+
# Generate speech
|
21 |
+
input_ids = tts_tokenizer(speech_text, return_tensors="pt").input_ids
|
22 |
+
speech = tts_model.generate(input_ids)
|
23 |
+
|
24 |
+
# Display classification result and play speech
|
25 |
+
st.write("Predicted Stutter Type:", predicted_class)
|
26 |
+
st.audio(speech, format="audio/wav")
|
27 |
+
|
28 |
+
# Define a function to map predicted class to speech text
|
29 |
+
def map_class_to_speech(predicted_class):
|
30 |
+
# Define speech text for each class
|
31 |
+
speech_texts = {
|
32 |
+
"nonstutter": "You are speaking fluently without any stutter.",
|
33 |
+
"prolongation": "You are experiencing prolongation stutter. Try to relax and speak slowly.",
|
34 |
+
"repetition": "You are experiencing repetition stutter. Focus on your breathing and try to speak smoothly.",
|
35 |
+
"blocks": "You are experiencing block stutter. Take a deep breath and try to speak slowly and calmly."
|
36 |
+
}
|
37 |
+
return speech_texts.get(predicted_class, "Unknown stutter type")
|
38 |
+
|
39 |
+
# Streamlit app
|
40 |
+
def main():
|
41 |
+
st.title("Stutter Classification and Therapy App")
|
42 |
+
audio_input = st.audio("Capture Audio", format="audio/wav", start_recording=True, channels=1)
|
43 |
+
if st.button("Stop Recording"):
|
44 |
+
with st.spinner("Classifying and speaking..."):
|
45 |
+
classify_and_speak(audio_input)
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
main()
|