Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ def upfilepath(local_filename):
|
|
| 20 |
files = {'files': open(local_filename, 'rb')}
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
response = requests.post(upload_url, files=files, timeout=30)
|
| 24 |
|
| 25 |
if response.status_code == 200:
|
| 26 |
result = response.json()
|
|
@@ -41,7 +41,6 @@ def transcribe_audio(audio):
|
|
| 41 |
|
| 42 |
recognizer = sr.Recognizer()
|
| 43 |
|
| 44 |
-
# Check if the file exists
|
| 45 |
if not os.path.isfile(audio):
|
| 46 |
return "Audio file not found."
|
| 47 |
|
|
@@ -91,6 +90,7 @@ def compare_texts(reference_text, transcribed_text):
|
|
| 91 |
reference_words = reference_text.split()
|
| 92 |
transcribed_words = transcribed_text.split()
|
| 93 |
incorrect_words_audios = [] # Store audio paths for incorrect words
|
|
|
|
| 94 |
|
| 95 |
sm = difflib.SequenceMatcher(None, reference_text, transcribed_text)
|
| 96 |
similarity_score = round(sm.ratio() * 100, 2)
|
|
@@ -108,24 +108,27 @@ def compare_texts(reference_text, transcribed_text):
|
|
| 108 |
|
| 109 |
html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
|
| 110 |
html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
|
| 111 |
-
html_output += f"<strong>IPA Transcription:</strong> {ipa_transcription(reference_text)}<br>"
|
| 112 |
html_output += "<strong>Word Score List:</strong><br>"
|
| 113 |
|
| 114 |
# Generate colored word score list
|
| 115 |
for i, word in enumerate(reference_words):
|
| 116 |
try:
|
| 117 |
if word.lower() == transcribed_words[i].lower():
|
|
|
|
| 118 |
html_output += f'<span style="color: green;">{word}</span> ' # Correct words in green
|
| 119 |
elif difflib.get_close_matches(word, [transcribed_words[i]]):
|
|
|
|
| 120 |
html_output += f'<span style="color: yellow;">{word}</span> ' # Close matches in yellow
|
| 121 |
else:
|
| 122 |
-
|
| 123 |
-
html_output += f'<span style="color: red;">{word}</span> '
|
| 124 |
# Create pronunciation audio for the incorrect word
|
| 125 |
audio_file_path = create_pronunciation_audio(word)
|
| 126 |
incorrect_words_audios.append((word, audio_file_path))
|
| 127 |
except IndexError:
|
| 128 |
# Word in reference that was not transcribed
|
|
|
|
| 129 |
html_output += f'<span style="color: red;">{word}</span> '
|
| 130 |
|
| 131 |
# Provide audio for incorrect words
|
|
@@ -139,7 +142,8 @@ def compare_texts(reference_text, transcribed_text):
|
|
| 139 |
html_output += f'{word}: '
|
| 140 |
html_output += f'<audio controls><source src="{audio_src}" type="audio/mpeg">Your browser does not support the audio tag.</audio>{suggestion_text}<br>'
|
| 141 |
|
| 142 |
-
|
|
|
|
| 143 |
|
| 144 |
# Step 4: Text-to-Speech Function
|
| 145 |
def text_to_speech(paragraph):
|
|
|
|
| 20 |
files = {'files': open(local_filename, 'rb')}
|
| 21 |
|
| 22 |
try:
|
| 23 |
+
response = requests.post(upload_url, files=files, timeout=30)
|
| 24 |
|
| 25 |
if response.status_code == 200:
|
| 26 |
result = response.json()
|
|
|
|
| 41 |
|
| 42 |
recognizer = sr.Recognizer()
|
| 43 |
|
|
|
|
| 44 |
if not os.path.isfile(audio):
|
| 45 |
return "Audio file not found."
|
| 46 |
|
|
|
|
| 90 |
reference_words = reference_text.split()
|
| 91 |
transcribed_words = transcribed_text.split()
|
| 92 |
incorrect_words_audios = [] # Store audio paths for incorrect words
|
| 93 |
+
word_score_list = [] # To store each word's score
|
| 94 |
|
| 95 |
sm = difflib.SequenceMatcher(None, reference_text, transcribed_text)
|
| 96 |
similarity_score = round(sm.ratio() * 100, 2)
|
|
|
|
| 108 |
|
| 109 |
html_output += f"<strong>Quality Score:</strong> {similarity_score}%<br>"
|
| 110 |
html_output += f"<strong>Transcribed Text:</strong> {transcribed_text}<br>"
|
| 111 |
+
html_output += f"<strong>IPA Transcription:</strong> {ipa_transcription(reference_text)}<br>"
|
| 112 |
html_output += "<strong>Word Score List:</strong><br>"
|
| 113 |
|
| 114 |
# Generate colored word score list
|
| 115 |
for i, word in enumerate(reference_words):
|
| 116 |
try:
|
| 117 |
if word.lower() == transcribed_words[i].lower():
|
| 118 |
+
word_score_list.append({"quality_score": 100, "word": word})
|
| 119 |
html_output += f'<span style="color: green;">{word}</span> ' # Correct words in green
|
| 120 |
elif difflib.get_close_matches(word, [transcribed_words[i]]):
|
| 121 |
+
word_score_list.append({"quality_score": 80, "word": word}) # Close matches
|
| 122 |
html_output += f'<span style="color: yellow;">{word}</span> ' # Close matches in yellow
|
| 123 |
else:
|
| 124 |
+
word_score_list.append({"quality_score": 0, "word": word})
|
| 125 |
+
html_output += f'<span style="color: red;">{word}</span> ' # Incorrect words in red
|
| 126 |
# Create pronunciation audio for the incorrect word
|
| 127 |
audio_file_path = create_pronunciation_audio(word)
|
| 128 |
incorrect_words_audios.append((word, audio_file_path))
|
| 129 |
except IndexError:
|
| 130 |
# Word in reference that was not transcribed
|
| 131 |
+
word_score_list.append({"quality_score": 0, "word": word})
|
| 132 |
html_output += f'<span style="color: red;">{word}</span> '
|
| 133 |
|
| 134 |
# Provide audio for incorrect words
|
|
|
|
| 142 |
html_output += f'{word}: '
|
| 143 |
html_output += f'<audio controls><source src="{audio_src}" type="audio/mpeg">Your browser does not support the audio tag.</audio>{suggestion_text}<br>'
|
| 144 |
|
| 145 |
+
# Return structured data
|
| 146 |
+
return [html_output, word_score_list]
|
| 147 |
|
| 148 |
# Step 4: Text-to-Speech Function
|
| 149 |
def text_to_speech(paragraph):
|