mr2along commited on
Commit
d8608a9
1 Parent(s): 00b9487

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -24
app.py CHANGED
@@ -7,13 +7,17 @@ from gtts import gTTS
7
  import io
8
  from pydub import AudioSegment
9
  import time
10
- import re
 
11
 
12
  # Create audio directory if it doesn't exist
13
  if not os.path.exists('audio'):
14
  os.makedirs('audio')
15
 
16
- # Function to upload file to server
 
 
 
17
  def upfilepath(local_filename):
18
  ts = time.time()
19
  upload_url = f"https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id={ts}"
@@ -78,22 +82,35 @@ def create_pronunciation_audio(word):
78
  except Exception as e:
79
  return f"Failed to create pronunciation audio: {e}"
80
 
81
- # Function to respell words phonetically
82
- def phonetic_respelling(word):
83
- word = word.lower()
84
- respelling = word
85
- # Simplified phonetic transformations
86
- respelling = re.sub(r'th', 'th', respelling) # 'th' as in 'the'
87
- respelling = re.sub(r'[aeiou]', lambda m: f'{m.group(0)}', respelling) # Simplify vowels
88
- respelling = re.sub(r'c', 'k', respelling) # 'c' sounds like 'k'
89
- respelling = re.sub(r'ph', 'f', respelling) # 'ph' as 'f'
90
- respelling = re.sub(r'ch', 'ch', respelling) # 'ch' as in 'church'
91
- respelling = re.sub(r'qu', 'kw', respelling) # 'qu' as 'kw'
92
- respelling = re.sub(r'sh', 'sh', respelling) # 'sh' as in 'shoe'
93
- respelling = re.sub(r'[^a-z]', '', respelling) # Remove non-alphabet characters
 
 
 
 
 
 
 
 
 
94
  return respelling
95
 
96
- # Step 3: Compare the transcribed text with the input paragraph and add phonetic respelling
 
 
 
 
97
  def compare_texts(reference_text, transcribed_text):
98
  reference_words = reference_text.split()
99
  transcribed_words = transcribed_text.split()
@@ -115,11 +132,9 @@ def compare_texts(reference_text, transcribed_text):
115
 
116
  html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
117
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
118
-
119
- # Add phonetic respelling for the entire sentence
120
- phonetic_transcription = " ".join([phonetic_respelling(word) for word in transcribed_words])
121
- html_output += f"<strong>Phonetic Respelling:</strong> {phonetic_transcription}<br>"
122
-
123
  html_output += "<strong>Word Score List:</strong><br>"
124
 
125
  # Generate colored word score list
@@ -145,12 +160,11 @@ def compare_texts(reference_text, transcribed_text):
145
  for word, audio in incorrect_words_audios:
146
  suggestion = difflib.get_close_matches(word, reference_words, n=1)
147
  suggestion_text = f" (Did you mean: <em>{suggestion[0]}</em>?)" if suggestion else ""
148
- up_audio=upfilepath(audio)
149
- audio_src=f"https://mr2along-speech-recognize.hf.space/gradio_api/file={up_audio}"
150
  html_output += f'{word}: '
151
  html_output += f'<audio controls><source src="{audio_src}" type="audio/mpeg">Your browser does not support the audio tag.</audio>{suggestion_text}<br>'
152
 
153
- # Return the final result with phonetic respelling
154
  return [html_output]
155
 
156
  # Step 4: Text-to-Speech Function
 
7
  import io
8
  from pydub import AudioSegment
9
  import time
10
+ import pronouncing
11
+ import epitran
12
 
13
  # Create audio directory if it doesn't exist
14
  if not os.path.exists('audio'):
15
  os.makedirs('audio')
16
 
17
+ # Initialize the epitran object for English
18
+ epi = epitran.Epitran('eng-Latn')
19
+
20
+ # Step 2: Create pronunciation audio for incorrect words
21
  def upfilepath(local_filename):
22
  ts = time.time()
23
  upload_url = f"https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id={ts}"
 
82
  except Exception as e:
83
  return f"Failed to create pronunciation audio: {e}"
84
 
85
+ # Function for phonetic respelling
86
+ def phonetic_respelling(sentence):
87
+ words = sentence.split()
88
+ respelled = []
89
+
90
+ for word in words:
91
+ # Find close matches for each word
92
+ close_matches = pronouncing.search(word)
93
+ if close_matches:
94
+ # Get the first close match
95
+ closest_word = close_matches[0]
96
+ respelled.append(pronouncing.phones_for_word(closest_word)[0]) # Use phonemes for the closest match
97
+ else:
98
+ respelled.append(word)
99
+
100
+ # Convert phonemes to respelling
101
+ respelling = ' '.join(respelled)
102
+
103
+ # Replace phonemes with common respellings
104
+ respelling = respelling.replace('ˈ', '').replace('ˌ', '').replace('ː', '') # Clean up phoneme symbols
105
+ respelling = respelling.replace('ɑ', 'a').replace('ə', 'uh').replace('ɪ', 'i').replace('ʊ', 'u') # Sample conversions
106
+
107
  return respelling
108
 
109
+ # Function for IPA transcription
110
+ def ipa_transcription(sentence):
111
+ return epi.transliterate(sentence)
112
+
113
+ # Step 3: Compare the transcribed text with the input paragraph
114
  def compare_texts(reference_text, transcribed_text):
115
  reference_words = reference_text.split()
116
  transcribed_words = transcribed_text.split()
 
132
 
133
  html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
134
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
135
+ html_output += f"<strong>Input Sentence:</strong> {reference_text}<br>"
136
+ html_output += f"<strong>Phonetic Respelling:</strong> {phonetic_respelling(reference_text)}<br>"
137
+ html_output += f"<strong>IPA Transcription:</strong> {ipa_transcription(reference_text)}<br>"
 
 
138
  html_output += "<strong>Word Score List:</strong><br>"
139
 
140
  # Generate colored word score list
 
160
  for word, audio in incorrect_words_audios:
161
  suggestion = difflib.get_close_matches(word, reference_words, n=1)
162
  suggestion_text = f" (Did you mean: <em>{suggestion[0]}</em>?)" if suggestion else ""
163
+ up_audio = upfilepath(audio)
164
+ audio_src = f"https://mr2along-speech-recognize.hf.space/gradio_api/file={up_audio}"
165
  html_output += f'{word}: '
166
  html_output += f'<audio controls><source src="{audio_src}" type="audio/mpeg">Your browser does not support the audio tag.</audio>{suggestion_text}<br>'
167
 
 
168
  return [html_output]
169
 
170
  # Step 4: Text-to-Speech Function