Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +108 -0
- requirements.txt +2 -1
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from audio import predict_all # This is your custom module for predictions
|
3 |
+
import re # Regular expressions for text processing
|
4 |
+
|
5 |
+
# Additional CSS for styling the confidence bars and the result layout
|
6 |
+
additional_css = """
|
7 |
+
/* CSS for the confidence bars */
|
8 |
+
.confidence-section {
|
9 |
+
display: flex;
|
10 |
+
align-items: center;
|
11 |
+
margin-top: 10px;
|
12 |
+
}
|
13 |
+
|
14 |
+
.confidence-label {
|
15 |
+
margin-right: 10px;
|
16 |
+
font-weight: bold;
|
17 |
+
}
|
18 |
+
.confidence-bar {
|
19 |
+
height: 20px;
|
20 |
+
width: 100%;
|
21 |
+
background-color: #eee;
|
22 |
+
border-radius: 10px;
|
23 |
+
margin: 10px 0;
|
24 |
+
}
|
25 |
+
|
26 |
+
.confidence-fill {
|
27 |
+
height: 100%;
|
28 |
+
border-radius: 10px;
|
29 |
+
background-color: #4caf50; /* Change color based on confidence level if desired */
|
30 |
+
text-align: center;
|
31 |
+
color: white;
|
32 |
+
line-height: 20px;
|
33 |
+
}
|
34 |
+
/* Additional CSS for styling the rest of your results */
|
35 |
+
"""
|
36 |
+
|
37 |
+
# Function to generate custom HTML for the confidence bar
|
38 |
+
def custom_confidence_bar(confidence):
|
39 |
+
color = "#4caf50" if confidence > 75 else "#FFC107" if confidence > 50 else "#F44336"
|
40 |
+
return f"""
|
41 |
+
<div class="confidence-section">
|
42 |
+
<span class="confidence-label">Model Confidence:</span>
|
43 |
+
<div class="confidence-bar">
|
44 |
+
<div class="confidence-fill" style="width: {confidence}%; background-color: {color};">
|
45 |
+
{confidence}%
|
46 |
+
</div>
|
47 |
+
</div>
|
48 |
+
</div>
|
49 |
+
"""
|
50 |
+
|
51 |
+
|
52 |
+
# Function to extract score level from message
|
53 |
+
def extract_score_level(message):
|
54 |
+
match = re.search(r'Score: (\d+)-(\d+)', message)
|
55 |
+
score_level = f"{match.group(1)} of 10" if match else "N/A"
|
56 |
+
return score_level
|
57 |
+
def message_markdown(label, message, task, score_level):
|
58 |
+
md = f'''# {label}
|
59 |
+
**Model Prediction:** {message}
|
60 |
+
|
61 |
+
**{task} Score:** {score_level}
|
62 |
+
'''
|
63 |
+
return md
|
64 |
+
# Function to process the audio file and analyze it
|
65 |
+
def analyze_audio(audio_data):
|
66 |
+
# Assuming predict_all returns a tuple of (message, confidence) for accuracy and fluency
|
67 |
+
accuracy, fluency = predict_all(audio_data)
|
68 |
+
|
69 |
+
# Unpack the results
|
70 |
+
accuracy_message, accuracy_confidence = accuracy
|
71 |
+
fluency_message, fluency_confidence = fluency
|
72 |
+
|
73 |
+
# Extract the score level from the message
|
74 |
+
accuracy_score = extract_score_level(accuracy_message)
|
75 |
+
fluency_score = extract_score_level(fluency_message)
|
76 |
+
|
77 |
+
# Remove the score level from the original message
|
78 |
+
accuracy_message = accuracy_message.split(",")[1].strip() if "," in accuracy_message else accuracy_message
|
79 |
+
fluency_message = fluency_message.split(",")[1].strip() if "," in fluency_message else fluency_message
|
80 |
+
|
81 |
+
# Generate the confidence bar HTML
|
82 |
+
accuracy_confidence_html = custom_confidence_bar(accuracy_confidence * 100)
|
83 |
+
fluency_confidence_html = custom_confidence_bar(fluency_confidence * 100)
|
84 |
+
|
85 |
+
accuracy_markdown = message_markdown('Accuracy of Pronunciation', accuracy_message, 'Pronunciation', accuracy_score)
|
86 |
+
|
87 |
+
fluency_markdown = message_markdown('Speaker Fluency', fluency_message, 'Fluency', fluency_score)
|
88 |
+
|
89 |
+
return accuracy_markdown, accuracy_confidence_html, fluency_markdown, fluency_confidence_html
|
90 |
+
|
91 |
+
# Define the Gradio interface
|
92 |
+
iface = gr.Interface(
|
93 |
+
fn=analyze_audio,
|
94 |
+
inputs=gr.Audio(label="Upload Audio"),
|
95 |
+
outputs=[
|
96 |
+
gr.Markdown(label="Accuracy Score Level"),
|
97 |
+
gr.HTML(label="Accuracy Confidence"),
|
98 |
+
gr.Markdown(label="Fluency Score Level"),
|
99 |
+
gr.HTML(label="Fluency Confidence"),
|
100 |
+
],
|
101 |
+
css=additional_css,
|
102 |
+
title="Audio Analysis Tool",
|
103 |
+
description="Upload an audio file to analyze its accuracy and fluency."
|
104 |
+
)
|
105 |
+
|
106 |
+
# Run the Gradio app
|
107 |
+
if __name__ == "__main__":
|
108 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
torch
|
2 |
transformers
|
3 |
-
|
|
|
|
1 |
torch
|
2 |
transformers
|
3 |
+
gradio
|
4 |
+
regex
|