MK-316 commited on
Commit
3f61986
β€’
1 Parent(s): d34b65b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import speech_recognition as sr
3
+ from Levenshtein import ratio
4
+ import tempfile
5
+ import numpy as np
6
+ import soundfile as sf
7
+ import pandas as pd
8
+
9
+ # Sample dataframe with sentences
10
+ data = {
11
+ "Sentences": [
12
+ "A stitch in time saves nine.",
13
+ "To be or not to be, that is the question.",
14
+ "Five cats were living in safe caves.",
15
+ "Hives give shelter to bees in large caves.",
16
+ "His decision to plant a rose was amazing.",
17
+ "She sells sea shells by the sea shore.",
18
+ "The colorful parrot likes rolling berries.",
19
+ "Time flies like an arrow; fruit flies like a banana.",
20
+ "Good things come to those who wait.",
21
+ "All human beings are born free and equal in dignity and rights."
22
+ ]
23
+ }
24
+ df = pd.DataFrame(data)
25
+ user_scores = {}
26
+
27
+ def transcribe_audio(file_info):
28
+ r = sr.Recognizer()
29
+ with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
30
+ sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
31
+ tmpfile.seek(0)
32
+ with sr.AudioFile(tmpfile.name) as source:
33
+ audio_data = r.record(source)
34
+ try:
35
+ text = r.recognize_google(audio_data)
36
+ return text
37
+ except sr.UnknownValueError:
38
+ return "Could not understand audio"
39
+ except sr.RequestError as e:
40
+ return f"Could not request results; {e}"
41
+
42
+ def pronunciation_correction(name, expected_text, file_info):
43
+ user_spoken_text = transcribe_audio(file_info)
44
+ similarity = ratio(expected_text.lower(), user_spoken_text.lower())
45
+ score = float(f"{similarity:.2f}")
46
+ if name in user_scores:
47
+ user_scores[name].append(score) # Track scores for each user
48
+ else:
49
+ user_scores[name] = [score]
50
+ feedback = "Excellent pronunciation!" if score >= 0.9 else \
51
+ "Good pronunciation!" if score >= 0.7 else \
52
+ "Needs improvement." if score >= 0.5 else \
53
+ "Poor pronunciation, try to focus more on clarity."
54
+ return feedback, score
55
+
56
+ def calculate_average(name):
57
+ if name in user_scores and user_scores[name]:
58
+ filtered_scores = [score for score in user_scores[name] if score > 0] # Ignore zeros
59
+ average_score = sum(filtered_scores) / len(filtered_scores)
60
+ else:
61
+ average_score = 0
62
+ return f"😍 Great job, {name}! \n\nYour average score (excluding zeros) is: {average_score:.2f}. \nRemember, this score only focuses on the accuracy of individual sounds. \nKeep up the fun and enjoyment as you continue learning English!"
63
+
64
+ with gr.Blocks() as app:
65
+ name_input = gr.Textbox(label="Enter your name", placeholder="Type your name here...", value="")
66
+ with gr.Row():
67
+ sentence_dropdown = gr.Dropdown(choices=df['Sentences'].tolist(), label="Select a Sentence")
68
+ selected_sentence_output = gr.Textbox(label="Selected Text", interactive=False)
69
+ audio_input = gr.Audio(label="Upload Audio File", type="numpy")
70
+ check_pronunciation_button = gr.Button("Check Pronunciation")
71
+ pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
72
+ pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
73
+ complete_button = gr.Button("Complete")
74
+ average_output = gr.Textbox(label="Average Score Output", visible=True)
75
+
76
+ sentence_dropdown.change(lambda x: x, inputs=sentence_dropdown, outputs=selected_sentence_output)
77
+ check_pronunciation_button.click(
78
+ pronunciation_correction,
79
+ inputs=[name_input, sentence_dropdown, audio_input],
80
+ outputs=[pronunciation_feedback, pronunciation_score]
81
+ )
82
+ complete_button.click(
83
+ calculate_average,
84
+ inputs=[name_input],
85
+ outputs=average_output
86
+ )
87
+
88
+ app.launch(debug=True)