MK-316 commited on
Commit
15bb464
β€’
1 Parent(s): 5891982

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -34
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
- "The quick brown fox jumps over the lazy dog.",
13
- "An apple a day keeps the doctor away.",
14
  "To be or not to be, that is the question.",
15
- "All human beings are born free and equal in dignity and rights.",
 
 
16
  "She sells sea shells by the sea shore.",
17
- "How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
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
- "You can't judge a book by its cover."
 
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(file=tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
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(selected_sentence, file_info):
42
- expected_text = selected_sentence
43
  user_spoken_text = transcribe_audio(file_info)
44
  similarity = ratio(expected_text.lower(), user_spoken_text.lower())
45
- description = f"{similarity:.2f}" # Formats the float to 2 decimal places
46
-
47
- if similarity >= 0.9:
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
- feedback = "Poor pronunciation, try to focus more on clarity."
55
-
56
- return feedback, description
 
 
 
57
 
 
 
 
 
 
 
 
58
 
59
- iface = gr.Interface(
60
- fn=pronunciation_correction,
61
- inputs=[
62
- gr.Dropdown(choices=df['Sentences'].tolist(), label="Select a Sentence"),
63
- gr.Audio(label="Upload Audio File", type="numpy")
64
- ],
65
- outputs=[
66
- gr.Textbox(label="Pronunciation Feedback"), # Custom label for the text output
67
- gr.Number(label="Pronunciation Accuracy Score: 0 (No Match) ~ 1 (Perfect)") # Custom label for the numerical output
68
- ],
69
- title="πŸŒ€ Pronunciation Feedback Tool"
70
- )
71
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- iface.launch(share=True, debug=True)
 
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)