Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import speech_recognition as sr | |
| from Levenshtein import ratio | |
| import tempfile | |
| import numpy as np | |
| import soundfile as sf | |
| import random | |
| # Datasets | |
| data1 = [ | |
| "They reserved a seat at the grand feast.", | |
| "Tim used a mitt to catch the ball.", | |
| "They walked past the end of the trail.", | |
| "Who'd thought the wood to visit?", | |
| "Luke took two steps to look further.", | |
| "The boss bought soy milk.", | |
| "A ton of people waited for the bus to come.", | |
| "The girl watched the perfect red bird." | |
| ] | |
| data2 = [ | |
| "They chose the right light.", | |
| "They’ll leave the lamp on late.", | |
| "The magic show brought no pleasure.", | |
| "She asks for help to pave the path.", | |
| "The theme was about making thin dough.", | |
| "The pine forest looked fine.", | |
| "The butter was better than any batter." | |
| ] | |
| # Initialize a list to keep track of scores | |
| scores = [] | |
| # Function to transcribe audio and get the similarity score | |
| def transcribe_audio(file_info): | |
| r = sr.Recognizer() | |
| with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile: | |
| sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV') | |
| tmpfile.seek(0) | |
| with sr.AudioFile(tmpfile.name) as source: | |
| audio_data = r.record(source) | |
| try: | |
| text = r.recognize_google(audio_data) | |
| return text | |
| except sr.UnknownValueError: | |
| return "Could not understand audio" | |
| except sr.RequestError as e: | |
| return f"Could not request results; {e}" | |
| # Function to calculate pronunciation correction | |
| def pronunciation_correction(expected_text, file_info): | |
| user_spoken_text = transcribe_audio(file_info) | |
| similarity = ratio(expected_text.lower(), user_spoken_text.lower()) | |
| similarity = similarity | |
| description = f"{similarity:.2f}" | |
| if similarity >= 0.95: | |
| feedback = "Excellent!" | |
| elif similarity >= 0.9: | |
| feedback = "Got it; No problem!" | |
| elif similarity >= 0.8: | |
| feedback = "I'll listen carefully when you say :-)" | |
| elif similarity >= 0.5: | |
| feedback = "I think I heard you :-)" | |
| else: | |
| feedback = "What did you say?" | |
| # Append the score to the scores list | |
| scores.append(similarity) | |
| return feedback, description | |
| # Function to display average score | |
| def show_average_score(): | |
| if scores: | |
| average_score = sum(scores) / len(scores) | |
| return f"Average Pronunciation Score: {average_score:.2f}" | |
| else: | |
| return "No scores available yet." | |
| # Select a random sentence from the chosen dataset | |
| def random_sentence(data_selected): | |
| if data_selected == "Vowels": | |
| dataset = data1 | |
| elif data_selected == "Consonants": | |
| dataset = data2 | |
| else: | |
| return "Please select a dataset." | |
| return random.choice(dataset) | |
| # Gradio app | |
| with gr.Blocks() as app: | |
| # Dataset selection using Radio Buttons | |
| gr.Markdown("## ✨ Pronunciation Checker") | |
| gr.Markdown("To reset, refresh the page.") | |
| data_radio = gr.Radio(choices=["Vowels", "Consonants"], label="Select practice focus:", interactive=True) | |
| show_button = gr.Button("Show Sentence") | |
| sentence_output = gr.Textbox(label="Sentence to Practice", interactive=False) | |
| # Show a new random sentence each time "Show Sentence" is clicked | |
| show_button.click( | |
| lambda data_selected: random_sentence(data_selected), | |
| inputs=data_radio, | |
| outputs=sentence_output | |
| ) | |
| audio_input = gr.Audio(label="Upload Audio File", type="numpy") | |
| gr.Markdown("### Instructions\nWait until the wave is reloaded.") | |
| check_pronunciation_button = gr.Button("Check Pronunciation") | |
| pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback") | |
| pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)") | |
| # Check pronunciation when button is clicked | |
| check_pronunciation_button.click( | |
| pronunciation_correction, | |
| inputs=[sentence_output, audio_input], | |
| outputs=[pronunciation_feedback, pronunciation_score] | |
| ) | |
| # Button and output to show the average score report | |
| show_report_button = gr.Button("Show Report (Average)") | |
| average_score_output = gr.Textbox(label="Average Pronunciation Score") | |
| # Display average score when the Show Report button is clicked | |
| show_report_button.click( | |
| show_average_score, | |
| outputs=average_score_output | |
| ) | |
| app.launch(debug=True) | |