bori0824 commited on
Commit
cec8f00
·
verified ·
1 Parent(s): 08525e7

Create app.py

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