File size: 5,390 Bytes
9d4a77e
 
518d206
41f4425
4124ee1
41f4425
9d4a77e
41f4425
518d206
41f4425
 
 
9d4a77e
41f4425
 
 
 
 
 
 
 
 
 
518d206
a8c79fb
41f4425
a8c79fb
4124ee1
74e03af
4124ee1
 
 
 
 
 
 
 
74e03af
3695d99
518d206
9d4a77e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e504517
9d4a77e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# app.py

import gradio as gr
import speech_recognition as sr
from Levenshtein import ratio
import tempfile
import numpy as np
import soundfile as sf

def transcribe_audio(file_info):
    r = sr.Recognizer()
    with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
        sf.write(tmpfile.name, data=file_info, 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}"

def pronunciation_correction(expected_text, file_info):
    user_spoken_text = transcribe_audio(file_info)
    similarity = ratio(expected_text.lower(), user_spoken_text.lower())
    description = f"{similarity:.2f}"

    if similarity >= 0.9:
        feedback = "Excellent pronunciation!"
    elif similarity >= 0.7:
        feedback = "Good pronunciation!"
    elif similarity >= 0.5:
        feedback = "Needs improvement."
    else:
        feedback = "Poor pronunciation, try to focus more on clarity."

    return feedback, description

def grammar_check(sentence):
    # Example implementation of a grammar checker function
    # Ideally, you would use a grammar checking API or tool
    # This is a placeholder to show the structure
    if not sentence.endswith("."):
        return "Sentence should end with a period."
    if len(sentence.split()) < 3:
        return "Sentence is too short. Please provide more details."
    return "No grammar issues found."

with gr.Blocks() as app:
    with gr.Column():
        text_input = gr.Textbox(label="Type your sentence here", lines=5, placeholder="Type here...")
        grammar_check_button = gr.Button("Check Grammar")
        grammar_feedback = gr.Textbox(label="Grammar Feedback", interactive=False)
        
        audio_input = gr.Audio(label="Upload Audio File", type="numpy")
        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)")

        grammar_check_button.click(
            grammar_check,
            inputs=text_input,
            outputs=grammar_feedback
        )
        
        check_pronunciation_button.click(
            pronunciation_correction,
            inputs=[text_input, audio_input],
            outputs=[pronunciation_feedback, pronunciation_score]
        )

app.launch(debug=True)
# app.py

import gradio as gr
import speech_recognition as sr
from Levenshtein import ratio
import tempfile
import numpy as np
import soundfile as sf

def transcribe_audio(file_info):
    r = sr.Recognizer()
    with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
        sf.write(tmpfile.name, data=file_info, 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}"

def pronunciation_correction(expected_text, file_info):
    user_spoken_text = transcribe_audio(file_info)
    similarity = ratio(expected_text.lower(), user_spoken_text.lower())
    description = f"{similarity:.2f}"

    if similarity >= 0.9:
        feedback = "Excellent pronunciation!"
    elif similarity >= 0.7:
        feedback = "Good pronunciation!"
    elif similarity >= 0.5:
        feedback = "Needs improvement."
    else:
        feedback = "Poor pronunciation, try to focus more on clarity."

    return feedback, description

def grammar_check(sentence):
    # Example implementation of a grammar checker function
    # Ideally, you would use a grammar checking API or tool
    # This is a placeholder to show the structure
    if not sentence.endswith("."):
        return "Sentence should end with a period."
    if len(sentence.split()) < 3:
        return "Sentence is too short. Please provide more details."
    return "No grammar issues found."

with gr.Blocks() as app:
    with gr.Column():
        text_input = gr.Textbox(label="Type your sentence here", lines=5, placeholder="Type here...")
        grammar_check_button = gr.Button("Check Grammar")
        grammar_feedback = gr.Textbox(label="Grammar Feedback", interactive=False)
        
        audio_input = gr.Audio(label="Upload Audio File", type="numpy")
        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)")

        grammar_check_button.click(
            grammar_check,
            inputs=text_input,
            outputs=grammar_feedback
        )
        
        check_pronunciation_button.click(
            pronunciation_correction,
            inputs=[text_input, audio_input],
            outputs=[pronunciation_feedback, pronunciation_score]
        )

app.launch(debug=True)