jinggujiwoo7 commited on
Commit
41f4425
โ€ข
1 Parent(s): 96ee2ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -65
app.py CHANGED
@@ -1,77 +1,94 @@
1
  import gradio as gr
 
 
 
 
 
 
 
2
 
3
- # ์ „์—ญ ๋ณ€์ˆ˜
4
- recordings = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- # ์Œ์„ฑ ๋…น์Œ ๋ฐ ์ €์žฅ
7
- def record_and_submit_voice(student_name, voice):
8
- if not student_name:
9
- return "Please enter your name."
10
- if student_name not in recordings:
11
- recordings[student_name] = []
12
- recordings[student_name].append({"voice": voice, "comments": []})
13
- return f"Voice recorded and submitted successfully by {student_name}!"
14
 
15
- # ํ•™์ƒ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
16
- def get_student_recordings():
17
- return list(recordings.keys())
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # ๋…น์Œ๋œ ์Œ์„ฑ ์žฌ์ƒ ๋ฐ ๋Œ“๊ธ€ ๊ฐ€์ ธ์˜ค๊ธฐ
20
- def play_recording(selected_student):
21
- if selected_student not in recordings:
22
- return [], "No recordings found for this student."
23
- voices = [rec["voice"] for rec in recordings[selected_student]]
24
- comments = "\n".join(
25
- [f"{c[0]}: {c[1]}" for rec in recordings[selected_student] for c in rec["comments"]]
26
- )
27
- return voices[0] if voices else None, comments
28
 
29
- # ๋Œ“๊ธ€ ์ž‘์„ฑ
30
- def write_comment(selected_student, commenter_name, comment):
31
- if not selected_student or selected_student not in recordings:
32
- return "Selected student's recording not found."
33
- if not commenter_name:
34
- return "Please enter your name."
35
- if not comment:
36
- return "Please enter a comment."
37
- recordings[selected_student][0]["comments"].append((commenter_name, comment))
38
- return f"Comment added successfully by {commenter_name}!"
39
 
40
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
41
- with gr.Blocks() as app:
42
- with gr.Tab("Record Voice"):
43
- student_name_input = gr.Textbox(placeholder="Enter your name", label="Your Name")
44
- voice_input = gr.Audio(type="filepath", label="Record your voice")
45
- submit_voice_button = gr.Button("Submit Voice")
46
- voice_output = gr.Textbox(label="Status")
47
 
48
- submit_voice_button.click(
49
- record_and_submit_voice,
50
- inputs=[student_name_input, voice_input],
51
- outputs=voice_output
52
- )
 
53
 
54
- with gr.Tab("Listen and Comment"):
55
- student_selector = gr.Dropdown(choices=get_student_recordings(), label="Select a student to listen", interactive=True)
56
- play_voice_button = gr.Button("Play Voice")
57
- recording_output = gr.Audio(label="Selected Recording", interactive=False)
58
- comment_input = gr.Textbox(placeholder="Write your comment here...", label="Write Comment")
59
- commenter_name_input = gr.Textbox(placeholder="Enter your name", label="Your Name")
60
- submit_comment_button = gr.Button("Submit Comment")
61
- comment_status_output = gr.Textbox(label="Comment Status")
62
- comments_display = gr.Textbox(label="Comments", interactive=False)
63
 
64
- play_voice_button.click(
65
- play_recording,
66
- inputs=student_selector,
67
- outputs=[recording_output, comments_display]
68
- )
69
 
70
- submit_comment_button.click(
71
- write_comment,
72
- inputs=[student_selector, commenter_name_input, comment_input],
73
- outputs=comment_status_output
74
- )
 
 
 
 
 
 
75
 
76
- # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
77
- app.launch()
 
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
+ import language_tool_python
9
 
10
+ # Sample dataframe with sentences ordered from easy to hard
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
 
27
+ tool = language_tool_python.LanguageTool('en-US')
 
 
 
 
 
 
 
28
 
29
+ def transcribe_audio(file_info):
30
+ r = sr.Recognizer()
31
+ with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
32
+ sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
33
+ tmpfile.seek(0)
34
+ with sr.AudioFile(tmpfile.name) as source:
35
+ audio_data = r.record(source)
36
+ try:
37
+ text = r.recognize_google(audio_data)
38
+ return text
39
+ except sr.UnknownValueError:
40
+ return "Could not understand audio"
41
+ except sr.RequestError as e:
42
+ return f"Could not request results; {e}"
43
 
44
+ def pronunciation_correction(expected_text, file_info):
45
+ user_spoken_text = transcribe_audio(file_info)
46
+ similarity = ratio(expected_text.lower(), user_spoken_text.lower())
47
+ description = f"{similarity:.2f}"
 
 
 
 
 
48
 
49
+ if similarity >= 0.9:
50
+ feedback = "Excellent pronunciation!"
51
+ elif similarity >= 0.7:
52
+ feedback = "Good pronunciation!"
53
+ elif similarity >= 0.5:
54
+ feedback = "Needs improvement."
55
+ else:
56
+ feedback = "Poor pronunciation, try to focus more on clarity."
 
 
57
 
58
+ return feedback, description
 
 
 
 
 
 
59
 
60
+ def check_grammar(text):
61
+ matches = tool.check(text)
62
+ if not matches:
63
+ return "No grammar issues found."
64
+ else:
65
+ return "Grammar issues found: " + "; ".join([f"{match.ruleId}: {match.message}" for match in matches])
66
 
67
+ with gr.Blocks() as app:
68
+ with gr.Row():
69
+ sentence_dropdown = gr.Dropdown(choices=df['Sentences'].tolist(), label="Select a Sentence")
70
+ selected_sentence_output = gr.Textbox(label="Selected Text", interactive=False)
71
+
72
+ with gr.Row():
73
+ user_sentence_input = gr.Textbox(label="Enter Your Sentence")
74
+ grammar_check_button = gr.Button("Check Grammar")
75
+ grammar_feedback = gr.Textbox(label="Grammar Feedback", interactive=False)
76
 
77
+ audio_input = gr.Audio(label="Upload Audio File", type="numpy")
78
+ check_pronunciation_button = gr.Button("Check Pronunciation")
79
+ pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
80
+ pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
 
81
 
82
+ sentence_dropdown.change(lambda x: x, inputs=sentence_dropdown, outputs=selected_sentence_output)
83
+ check_pronunciation_button.click(
84
+ pronunciation_correction,
85
+ inputs=[sentence_dropdown, audio_input],
86
+ outputs=[pronunciation_feedback, pronunciation_score]
87
+ )
88
+ grammar_check_button.click(
89
+ check_grammar,
90
+ inputs=[user_sentence_input],
91
+ outputs=[grammar_feedback]
92
+ )
93
 
94
+ app.launch(debug=True)