Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,97 +1,94 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
-
import librosa.display
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
import plotly.express as px
|
7 |
-
|
8 |
-
import
|
9 |
-
import
|
10 |
-
import
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
#
|
17 |
-
st.
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
success_check=st.empty()
|
95 |
-
|
96 |
-
|
97 |
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import librosa.display
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import plotly.express as px
|
7 |
+
import torch
|
8 |
+
import torchaudio
|
9 |
+
import time
|
10 |
+
from transformers import WhisperForAudioClassification, AutoFeatureExtractor
|
11 |
+
|
12 |
+
|
13 |
+
# Set page title and favicon
|
14 |
+
st.set_page_config(page_title="Audio Visualization", page_icon="π§")
|
15 |
+
|
16 |
+
# Upload audio file
|
17 |
+
audio_file = st.file_uploader("Upload Audio file for Assessment", type=["wav", "mp3"])
|
18 |
+
|
19 |
+
# Load the model and processor
|
20 |
+
model = WhisperForAudioClassification.from_pretrained("Huma10/Whisper_Stuttered_Speech")
|
21 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("Huma10/Whisper_Stuttered_Speech")
|
22 |
+
total_inference_time = 0 # Initialize the total inference time
|
23 |
+
# Check if an audio file is uploaded
|
24 |
+
if audio_file is not None:
|
25 |
+
st.audio(audio_file, format="audio/wav")
|
26 |
+
# Load and preprocess the uploaded audio file
|
27 |
+
input_audio, _ = torchaudio.load(audio_file)
|
28 |
+
# Save the filename
|
29 |
+
audio_filename = audio_file.name
|
30 |
+
# Segment the audio into 3-second clips
|
31 |
+
target_duration = 3 # 3 seconds
|
32 |
+
target_samples = int(target_duration * 16000)
|
33 |
+
num_clips = input_audio.size(1) // target_samples
|
34 |
+
audio_clips = [input_audio[:, i * target_samples : (i + 1) * target_samples] for i in range(num_clips)]
|
35 |
+
|
36 |
+
predicted_labels_list = []
|
37 |
+
|
38 |
+
# Perform inference for each clip
|
39 |
+
for clip in audio_clips:
|
40 |
+
inputs = feature_extractor(clip.squeeze().numpy(), return_tensors="pt")
|
41 |
+
input_features = inputs.input_features
|
42 |
+
|
43 |
+
|
44 |
+
# Measure inference time
|
45 |
+
start_time = time.time()
|
46 |
+
# Perform inference
|
47 |
+
with torch.no_grad():
|
48 |
+
logits = model(input_features).logits
|
49 |
+
|
50 |
+
end_time = time.time()
|
51 |
+
inference_time = end_time - start_time
|
52 |
+
total_inference_time += inference_time # Accumulate inference time
|
53 |
+
|
54 |
+
# Convert logits to predictions
|
55 |
+
predicted_class_ids = torch.argmax(logits, dim=-1)
|
56 |
+
predicted_labels = [model.config.id2label[class_id.item()] for class_id in predicted_class_ids]
|
57 |
+
predicted_labels_list.extend(predicted_labels)
|
58 |
+
|
59 |
+
st.markdown(f"Total inference time: **{total_inference_time:.4f}** seconds")
|
60 |
+
def calculate_percentages(predicted_labels):
|
61 |
+
# Count each type of disfluency
|
62 |
+
disfluency_count = pd.Series(predicted_labels).value_counts(normalize=True)
|
63 |
+
return disfluency_count * 100 # Convert fractions to percentages
|
64 |
+
|
65 |
+
def plot_disfluency_percentages(percentages):
|
66 |
+
fig, ax = plt.subplots()
|
67 |
+
percentages.plot(kind='bar', ax=ax, color='#70bdbd')
|
68 |
+
ax.set_title('Percentage of Each Disfluency Type')
|
69 |
+
ax.set_xlabel('Disfluency Type')
|
70 |
+
ax.set_ylabel('Percentage')
|
71 |
+
plt.xticks(rotation=45)
|
72 |
+
return fig
|
73 |
+
|
74 |
+
# Streamlit application
|
75 |
+
def main():
|
76 |
+
st.title("Speech Profile")
|
77 |
+
st.write("This app analyzes the percentage of different types of disfluencies in stuttered speech.")
|
78 |
+
|
79 |
+
# Calculate percentages
|
80 |
+
percentages = calculate_percentages(predicted_labels_list)
|
81 |
+
|
82 |
+
# Plot
|
83 |
+
fig = plot_disfluency_percentages(percentages)
|
84 |
+
st.pyplot(fig)
|
85 |
+
|
86 |
+
|
87 |
+
main()
|
88 |
+
|
89 |
+
success_check=st.success(' Assessment Completed Successfully!', icon="β
")
|
90 |
+
time.sleep(5)
|
91 |
+
success_check=st.empty()
|
92 |
+
|
93 |
+
|
|
|
|
|
|
|
94 |
|