MK-316 commited on
Commit
3c9a8b2
β€’
1 Parent(s): c431c00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -15
app.py CHANGED
@@ -9,25 +9,24 @@ import pandas as pd
9
  # Sample dataframe with sentences
10
  data = {
11
  "Sentences": [
12
- "A stitch in time saves nine.",
 
13
  "To be or not to be, that is the question.",
14
- "Five cats were living in safe caves.",
15
- "Hives give shelter to bees in large caves.",
16
- "His decision to plant a rose was amazing.",
17
  "She sells sea shells by the sea shore.",
18
- "The colorful parrot likes rolling berries.",
19
- "Time flies like an arrow; fruit flies like a banana.",
20
  "Good things come to those who wait.",
21
- "All human beings are born free and equal in dignity and rights."
 
22
  ]
23
  }
24
  df = pd.DataFrame(data)
25
- user_scores = {}
26
 
27
  def transcribe_audio(file_info):
28
  r = sr.Recognizer()
29
  with tempfile.NamedTemporaryFile(delete=True, suffix=".wav") as tmpfile:
30
- sf.write(tmpfile.name, data=file_info[1], samplerate=44100, format='WAV')
31
  tmpfile.seek(0)
32
  with sr.AudioFile(tmpfile.name) as source:
33
  audio_data = r.record(source)
@@ -39,12 +38,36 @@ def transcribe_audio(file_info):
39
  except sr.RequestError as e:
40
  return f"Could not request results; {e}"
41
 
42
- def pronunciation_correction(name, expected_text, file_info):
 
43
  user_spoken_text = transcribe_audio(file_info)
44
  similarity = ratio(expected_text.lower(), user_spoken_text.lower())
45
- score = float(f"{similarity:.2f}")
46
- if name in user_scores:
47
- user_scores[name].append(score) # Track scores for each user
 
 
 
 
 
48
  else:
49
- user_scores[name] = [score]
50
- feedback = "Excellent pr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  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(debug=True)