jinggujiwoo7 commited on
Commit
74e03af
β€’
1 Parent(s): e5af005

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -61
app.py CHANGED
@@ -1,72 +1,73 @@
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
- from transformers import pipeline
8
 
9
- # Load a grammar checking pipeline
10
- grammar_checker = pipeline("text-classification", model="textattack/bert-base-uncased-CoLA")
11
 
12
- def transcribe_audio(file_info):
13
- r = sr.Recognizer()
14
- with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
15
- sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
16
- tmpfile.seek(0)
17
- with sr.AudioFile(tmpfile.name) as source:
18
- audio_data = r.record(source)
19
- try:
20
- text = r.recognize_google(audio_data)
21
- return text
22
- except sr.UnknownValueError:
23
- return "Could not understand audio"
24
- except sr.RequestError as e:
25
- return f"Could not request results; {e}"
26
 
27
- def pronunciation_correction(expected_text, file_info):
28
- user_spoken_text = transcribe_audio(file_info)
29
- similarity = ratio(expected_text.lower(), user_spoken_text.lower())
30
- description = f"{similarity:.2f}"
31
 
32
- if similarity >= 0.9:
33
- feedback = "Excellent pronunciation!"
34
- elif similarity >= 0.7:
35
- feedback = "Good pronunciation!"
36
- elif similarity >= 0.5:
37
- feedback = "Needs improvement."
38
- else:
39
- feedback = "Poor pronunciation, try to focus more on clarity."
40
 
41
- return feedback, description
42
-
43
- def check_sentence_accuracy(sentence):
44
- results = grammar_checker(sentence)
45
- if results[0]['label'] == 'LABEL_1': # Assuming LABEL_1 indicates grammatical correctness
46
- return "No errors found. Your sentence is correct!", []
47
- else:
48
- return "Errors found:", ["The sentence might be grammatically incorrect."]
 
 
49
 
 
50
  with gr.Blocks() as app:
51
- with gr.Row():
52
- user_input_textbox = gr.Textbox(label="Type a Sentence", placeholder="Type the sentence you want to practice here...")
53
- audio_input = gr.Audio(label="Upload Audio File", type="numpy")
54
- check_pronunciation_button = gr.Button("Check Pronunciation")
55
- pronunciation_feedback = gr.Textbox(label="Pronunciation Feedback")
56
- pronunciation_score = gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)")
57
- check_accuracy_button = gr.Button("Check Sentence Accuracy")
58
- sentence_accuracy_feedback = gr.Textbox(label="Sentence Accuracy Feedback")
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- check_pronunciation_button.click(
61
- pronunciation_correction,
62
- inputs=[user_input_textbox, audio_input],
63
- outputs=[pronunciation_feedback, pronunciation_score]
64
- )
65
 
66
- check_accuracy_button.click(
67
- check_sentence_accuracy,
68
- inputs=[user_input_textbox],
69
- outputs=[sentence_accuracy_feedback]
70
- )
71
 
72
- app.launch(debug=True)
 
 
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 [], []
23
+ return [rec["voice"] for rec in recordings[selected_student]], recordings[selected_student][0]["comments"]
 
 
 
24
 
25
+ # λŒ“κΈ€ μž‘μ„±
26
+ def write_comment(selected_student, commenter_name, comment):
27
+ if not selected_student or selected_student not in recordings:
28
+ return "Selected student's recording not found."
29
+ if not commenter_name:
30
+ return "Please enter your name."
31
+ if not comment:
32
+ return "Please enter a comment."
33
+ recordings[selected_student][0]["comments"].append(f"{commenter_name}: {comment}")
34
+ return f"Comment added successfully by {commenter_name}!"
35
 
36
+ # Gradio μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
37
  with gr.Blocks() as app:
38
+ with gr.Tab("Record Voice"):
39
+ student_name_input = gr.Textbox(placeholder="Enter your name", label="Your Name")
40
+ voice_input = gr.Audio(source="microphone", type="numpy", label="Record your voice")
41
+ submit_voice_button = gr.Button("Submit Voice")
42
+ voice_output = gr.Textbox(label="Status")
43
+
44
+ submit_voice_button.click(
45
+ record_and_submit_voice,
46
+ inputs=[student_name_input, voice_input],
47
+ outputs=voice_output
48
+ )
49
+
50
+ with gr.Tab("Listen and Comment"):
51
+ student_selector = gr.Dropdown(choices=get_student_recordings, label="Select a student to listen")
52
+ play_voice_button = gr.Button("Play Voice")
53
+ recording_output = gr.Audio(label="Selected Recording", interactive=False)
54
+ comment_input = gr.Textbox(placeholder="Write your comment here...", label="Write Comment")
55
+ commenter_name_input = gr.Textbox(placeholder="Enter your name", label="Your Name")
56
+ submit_comment_button = gr.Button("Submit Comment")
57
+ comment_status_output = gr.Textbox(label="Comment Status")
58
+ comments_display = gr.Textbox(label="Comments", interactive=False)
59
 
60
+ play_voice_button.click(
61
+ play_recording,
62
+ inputs=student_selector,
63
+ outputs=[recording_output, comments_display]
64
+ )
65
 
66
+ submit_comment_button.click(
67
+ write_comment,
68
+ inputs=[student_selector, commenter_name_input, comment_input],
69
+ outputs=comment_status_output
70
+ )
71
 
72
+ # μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
73
+ app.launch()