seba3y commited on
Commit
e4592bd
1 Parent(s): f4212a0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -148
app.py CHANGED
@@ -1,53 +1,13 @@
1
  import gradio as gr
2
- # from logic import Speaker_speech_analysis
3
  from scipy.io import wavfile
4
  from wav2vec_aligen import speaker_pronunciation_assesment
5
 
6
 
7
 
8
- def create_html_from_scores(word_levels):
9
- html_output = ''
10
- for word, level in word_levels:
11
- if level == '/':
12
- html_output += f'<span style="color: #0000ff;">{level}</span> '
13
- elif level == 'Wrong':
14
- html_output += f'<span style="color: #dc3545;">{word}</span> '
15
- elif level == 'Understandable':
16
- html_output += f'<span style="color: #ffc107;">{word}</span> '
17
- else:
18
- html_output += f'<span style="color: #28a745;">{word}</span> '
19
- return html_output
20
-
21
- def generate_progress_bar(score, label):
22
- score = round(score, 2)
23
- score_text = f"{score:.2f}" if score < 100 else "100"
24
- if score < 30:
25
- bar_color = "#dc3545"
26
- elif score < 60:
27
- bar_color = "#dc6545"
28
- elif score < 80:
29
- bar_color = "#ffc107"
30
- else:
31
- bar_color = "#28a745"
32
- bar_length = f"{(score / 100) * 100}%"
33
- return f"""
34
- <div class="progress-label">{label}:</div>
35
- <div class="progress-container">
36
- <div class="progress-bar" style="width: {bar_length}; background-color: {bar_color};">
37
- <div class="progress-score">{score_text}</div>
38
- </div>
39
- </div>
40
- <div class="progress-max">Max: 100</div>
41
- """
42
- # CSS to be used in the Gradio Interface
43
-
44
-
45
-
46
-
47
- def analyze_audio(text, audio):
48
  # Write the processed audio to a temporary WAV file
49
- if text is None or audio is None:
50
- return 'the audio or the text is missing'
51
  temp_filename = 'temp_audio.wav'
52
  wavfile.write(temp_filename, audio[0], audio[1])
53
 
@@ -57,110 +17,44 @@ def analyze_audio(text, audio):
57
  fluency_score = result['fluency_score']
58
  total_score = result['total_score']
59
  content_scores = result['content_scores']
60
-
61
- pronunciation_progress_bar = generate_progress_bar(accuracy_score, "Pronunciation Accuracy")
62
- fluency_progress_bar = generate_progress_bar(fluency_score, "Fluency Score")
63
- content_progress_bar = generate_progress_bar(content_scores, "Content Score")
64
- total_progress_bar = generate_progress_bar(total_score, "Total Score")
65
-
66
-
67
- html_with_css = f"""
68
- <style>
69
- .legend {{
70
- font-size: 22px;
71
- display: flex;
72
- align-items: center;
73
- gap: 12px;
74
- }}
75
-
76
- .legend-dot {{
77
- height: 15px;
78
- width: 15px;
79
- border-radius: 50%;
80
- display: inline-block;
81
- }}
82
-
83
- .good {{ color: #28a745;
84
- }}
85
- .average {{ color: #ffc107;
86
- }}
87
- .bad {{ color: #dc3545;
88
- }}
89
-
90
- .wrong {{ color: #dc3545;
91
- }}
92
-
93
- .text {{
94
- font-size: 20px;
95
- margin-bottom: 20px;
96
- }}
97
-
98
- .progress-container {{
99
- width: 100%;
100
- background-color: #ddd;
101
- border-radius: 13px;
102
- overflow: hidden;
103
- }}
104
-
105
- .progress-bar {{
106
- height: 30px;
107
- line-height: 30px;
108
- text-align: center;
109
- font-size: 16px;
110
- border-radius: 15px;
111
- transition: width 1s ease;
112
- }}
113
-
114
- .progress-label {{
115
- font-weight: bold;
116
- font-size: 22px;
117
- margin-bottom: 20px;
118
- margin-top: 5px;
119
- text-align: center;
120
- }}
121
-
122
- .progress-score {{
123
- display: inline-block;
124
- color: black;
125
- }}
126
-
127
- .progress-max {{
128
- text-align: right;
129
- margin: 10px;
130
- font-size: 16px;
131
- }}
132
-
133
- </style>
134
-
135
-
136
- <div class="legend">
137
- <span class="legend-dot" style="background-color: #28a745;"></span><span>Good</span>
138
- <span class="legend-dot" style="background-color: #ffc107;"></span><span>Understandable</span>
139
- <span class="legend-dot" style="background-color: #dc3545;"></span><span>Bad</span>
140
- <span class="legend-dot" style="background-color: #0000ff;"></span><span>No Speech</span>
141
- </div>
142
-
143
- {total_progress_bar}
144
- {pronunciation_progress_bar}
145
- {fluency_progress_bar}
146
- {content_progress_bar}
147
  """
148
- #
149
-
150
- return html_with_css
151
-
152
- # Define the Gradio interface
153
- iface = gr.Interface(fn=analyze_audio,
154
- inputs=[gr.Textbox(label='Training Text', placeholder='Write the text for pronunciation task', interactive=True, visible=True, show_copy_button=True,),
155
- gr.Audio(label="Recoreded Audio", sources=['microphone', 'upload'])
156
- ],
157
- outputs=[gr.HTML(label="Analysis of pronunciation"),
158
- ],
159
- # css=additional_css,
160
- # title="Audio Analysis Tool",
161
- description="Write any text and recored an audio to predict pronunciation erors"
162
- )
163
 
164
- # Run the Gradio app
165
- if __name__ == "__main__":
166
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  from scipy.io import wavfile
3
  from wav2vec_aligen import speaker_pronunciation_assesment
4
 
5
 
6
 
7
+ def analyze_audio(audio):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Write the processed audio to a temporary WAV file
9
+ if audio is None:
10
+ return 'the audio is missing'
11
  temp_filename = 'temp_audio.wav'
12
  wavfile.write(temp_filename, audio[0], audio[1])
13
 
 
17
  fluency_score = result['fluency_score']
18
  total_score = result['total_score']
19
  content_scores = result['content_scores']
20
+
21
+ result_markdown = f"""|Language Aspect| Score|
22
+ |---|---|
23
+ |Pronunciation Accuracy| {accuracy_score}|
24
+ |Fluency| {fluency_score}|
25
+ |Total Score| {total_score}|
26
+ |Content Score| {content_scores}|
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  """
28
+ return result_markdown
29
+
30
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ CHOICES = ['Daibers', 'Carbon', 'Reptiles']
33
+ SENTENCES = [
34
+ """In Germany, over 100,000 tons of diapers are discarded each year, resulting in the wastage of valuable resources. Diaper liners, which contain special polymers known as superabsorbers, are among the materials that end up in landfills. However, researchers have made significant progress in enhancing the recycling process for these liners, leading to substantial improvements.""",
35
+
36
+ """Across the globe, there is a wide spread effort to explore methods for extracting carbon dioxide from the atmosphere or power plant emissions and transforming it into a valuable resource. Among the various ideas being explored, the concept of converting car bondioxide into a stable fuel shows significant promise.""",
37
+
38
+ """Around 250 million years ago,700 species of reptiles closely related to the modern-day crocodile roamed the earth, now new research reveals how a complex interplay between climate change, species competition and habitat can help explain why just 23 species of crocodile survive today."""
39
+ ]
40
+
41
+ PAIRED_TEXT = {k: v for k, v in zip(CHOICES, SENTENCES)}
42
+
43
+ def get_paired_text(value):
44
+ text = '## ' + PAIRED_TEXT.get(value, '')
45
+ return text
46
+
47
+ with gr.Blocks() as demo:
48
+ with gr.Row():
49
+ with gr.Column():
50
+ with gr.Row():
51
+ drp_down = gr.Dropdown(choices=CHOICES, scale=2)
52
+ show_text_btn = gr.Button("Select", scale=1)
53
+ read_text = gr.Markdown(label='Read the follwing text')
54
+ show_text_btn.click(get_paired_text, inputs=drp_down, outputs=read_text)
55
+ audio_area = gr.Audio(label='Read the sentence')
56
+ analyize_audio_btn = gr.Button("Submit", scale=1)
57
+ with gr.Column():
58
+ capt_area = gr.Markdown(label='CAPT Scores')
59
+ analyize_audio_btn.click(analyze_audio, inputs=audio_area, outputs=capt_area)
60
+ demo.launch()