mr2along commited on
Commit
746e430
·
verified ·
1 Parent(s): a225d4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
app.py CHANGED
@@ -7,7 +7,6 @@ from gtts import gTTS
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
@@ -20,6 +19,27 @@ try:
20
  except Exception as e:
21
  print(f"Error initializing Epitran: {e}")
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Step 1: Transcribe the audio file
24
  def transcribe_audio(audio):
25
  if audio is None:
@@ -54,6 +74,13 @@ def transcribe_audio(audio):
54
  except sr.RequestError as e:
55
  return f"Error with Google Speech Recognition service: {e}"
56
 
 
 
 
 
 
 
 
57
  # Step 2: Create pronunciation audio for incorrect words (locally)
58
  def create_pronunciation_audio(word):
59
  try:
@@ -64,38 +91,6 @@ def create_pronunciation_audio(word):
64
  except Exception as e:
65
  return f"Failed to create pronunciation audio: {e}"
66
 
67
- # Function for phonetic respelling
68
- def phonetic_respelling(sentence):
69
- words = sentence.split()
70
- respelled = []
71
-
72
- for word in words:
73
- # Find close matches for each word
74
- close_matches = pronouncing.search(word)
75
- if close_matches:
76
- # Get the first close match
77
- closest_word = close_matches[0]
78
- respelled.append(pronouncing.phones_for_word(closest_word)[0]) # Use phonemes for the closest match
79
- else:
80
- respelled.append(word)
81
-
82
- # Convert phonemes to respelling
83
- respelling = ' '.join(respelled)
84
-
85
- # Replace phonemes with common respellings
86
- respelling = respelling.replace('ˈ', '').replace('ˌ', '').replace('ː', '') # Clean up phoneme symbols
87
- respelling = respelling.replace('ɑ', 'a').replace('ə', 'uh').replace('ɪ', 'i').replace('ʊ', 'u') # Sample conversions
88
-
89
- return respelling
90
-
91
- # Function for IPA transcription
92
- def ipa_transcription(sentence):
93
- try:
94
- return epi.transliterate(sentence)
95
- except Exception as e:
96
- print(f"Error during IPA transcription: {e}")
97
- return "IPA transcription failed."
98
-
99
  # Step 3: Compare the transcribed text with the input paragraph
100
  def compare_texts(reference_text, transcribed_text):
101
  reference_words = reference_text.split()
@@ -118,9 +113,7 @@ def compare_texts(reference_text, transcribed_text):
118
 
119
  html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
120
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
121
- html_output += f"<strong>Input Sentence:</strong> {reference_text}<br>"
122
- html_output += f"<strong>Phonetic Respelling:</strong> {phonetic_respelling(reference_text)}<br>"
123
- html_output += f"<strong>IPA Transcription:</strong> {ipa_transcription(reference_text)}<br>"
124
  html_output += "<strong>Word Score List:</strong><br>"
125
 
126
  # Generate colored word score list
 
7
  import io
8
  from pydub import AudioSegment
9
  import time
 
10
  import epitran
11
 
12
  # Create audio directory if it doesn't exist
 
19
  except Exception as e:
20
  print(f"Error initializing Epitran: {e}")
21
 
22
+ # Step 2: Create pronunciation audio for incorrect words
23
+ def upfilepath(local_filename):
24
+ ts = time.time()
25
+ upload_url = f"https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id={ts}"
26
+ files = {'files': open(local_filename, 'rb')}
27
+
28
+ try:
29
+ response = requests.post(upload_url, files=files, timeout=30) # Set timeout (e.g., 30 seconds)
30
+
31
+ if response.status_code == 200:
32
+ result = response.json()
33
+ extracted_path = result[0]
34
+ return extracted_path
35
+ else:
36
+ return None
37
+
38
+ except requests.exceptions.Timeout:
39
+ return "Request timed out. Please try again."
40
+ except Exception as e:
41
+ return f"An error occurred: {e}"
42
+
43
  # Step 1: Transcribe the audio file
44
  def transcribe_audio(audio):
45
  if audio is None:
 
74
  except sr.RequestError as e:
75
  return f"Error with Google Speech Recognition service: {e}"
76
 
77
+ # Function to get IPA transcription
78
+ def ipa_transcription(sentence):
79
+ try:
80
+ return epi.transliterate(sentence)
81
+ except Exception as e:
82
+ return f"Error during IPA transcription: {e}"
83
+
84
  # Step 2: Create pronunciation audio for incorrect words (locally)
85
  def create_pronunciation_audio(word):
86
  try:
 
91
  except Exception as e:
92
  return f"Failed to create pronunciation audio: {e}"
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  # Step 3: Compare the transcribed text with the input paragraph
95
  def compare_texts(reference_text, transcribed_text):
96
  reference_words = reference_text.split()
 
113
 
114
  html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
115
  html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
116
+ html_output += f"<strong>IPA Transcription:</strong> {ipa_transcription(reference_text)}<br>" # Display IPA transcription
 
 
117
  html_output += "<strong>Word Score List:</strong><br>"
118
 
119
  # Generate colored word score list