Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import speech_recognition as sr
|
| 3 |
from Levenshtein import ratio
|
|
@@ -9,24 +10,25 @@ import pandas as pd
|
|
| 9 |
# Sample dataframe with sentences
|
| 10 |
data = {
|
| 11 |
"Sentences": [
|
| 12 |
-
"
|
| 13 |
-
"An apple a day keeps the doctor away.",
|
| 14 |
"To be or not to be, that is the question.",
|
| 15 |
-
"
|
|
|
|
|
|
|
| 16 |
"She sells sea shells by the sea shore.",
|
| 17 |
-
"
|
| 18 |
-
"A stitch in time saves nine.",
|
| 19 |
-
"Good things come to those who wait.",
|
| 20 |
"Time flies like an arrow; fruit flies like a banana.",
|
| 21 |
-
"
|
|
|
|
| 22 |
]
|
| 23 |
}
|
| 24 |
df = pd.DataFrame(data)
|
|
|
|
| 25 |
|
| 26 |
def transcribe_audio(file_info):
|
| 27 |
r = sr.Recognizer()
|
| 28 |
with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
|
| 29 |
-
sf.write(
|
| 30 |
tmpfile.seek(0)
|
| 31 |
with sr.AudioFile(tmpfile.name) as source:
|
| 32 |
audio_data = r.record(source)
|
|
@@ -38,36 +40,50 @@ def transcribe_audio(file_info):
|
|
| 38 |
except sr.RequestError as e:
|
| 39 |
return f"Could not request results; {e}"
|
| 40 |
|
| 41 |
-
def pronunciation_correction(
|
| 42 |
-
expected_text = selected_sentence
|
| 43 |
user_spoken_text = transcribe_audio(file_info)
|
| 44 |
similarity = ratio(expected_text.lower(), user_spoken_text.lower())
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
feedback = "Excellent pronunciation!"
|
| 49 |
-
elif similarity >= 0.7:
|
| 50 |
-
feedback = "Good pronunciation!"
|
| 51 |
-
elif similarity >= 0.5:
|
| 52 |
-
feedback = "Needs improvement."
|
| 53 |
else:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
gr.Dropdown(choices=df['Sentences'].tolist(), label="Select a Sentence")
|
| 63 |
-
gr.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
|
|
|
| 1 |
+
#@markdown Accuracy Score, Average, user name
|
| 2 |
import gradio as gr
|
| 3 |
import speech_recognition as sr
|
| 4 |
from Levenshtein import ratio
|
|
|
|
| 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)
|
|
|
|
| 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)
|