Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import speech_recognition as sr
|
| 4 |
import difflib
|
| 5 |
import gradio as gr
|
|
@@ -46,44 +45,15 @@ def transcribe_audio(audio):
|
|
| 46 |
except sr.RequestError as e:
|
| 47 |
return f"Error with Google Speech Recognition service: {e}"
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def upfilepath(local_filename):
|
| 52 |
-
upload_url = "https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id=yw08d344te"
|
| 53 |
-
files = {'files': open(local_filename, 'rb')}
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
extracted_path = result[0]
|
| 61 |
-
return extracted_path
|
| 62 |
-
else:
|
| 63 |
-
return None
|
| 64 |
-
|
| 65 |
-
except requests.exceptions.Timeout:
|
| 66 |
-
return "Request timed out. Please try again."
|
| 67 |
except Exception as e:
|
| 68 |
-
return f"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
def create_pronunciation_audio(word):
|
| 72 |
-
retries = 3 # Retry up to 3 times
|
| 73 |
-
for attempt in range(retries):
|
| 74 |
-
try:
|
| 75 |
-
tts = gTTS(word)
|
| 76 |
-
audio_file_path = f"audio/{word}.mp3"
|
| 77 |
-
tts.save(audio_file_path)
|
| 78 |
-
word_audio = upfilepath(audio_file_path)
|
| 79 |
-
if word_audio:
|
| 80 |
-
return f"https://mr2along-speech-recognize.hf.space/gradio_api/file={word_audio}"
|
| 81 |
-
except Exception as e:
|
| 82 |
-
if attempt < retries - 1:
|
| 83 |
-
time.sleep(2 ** attempt) # Exponential backoff
|
| 84 |
-
else:
|
| 85 |
-
return f"Failed to create pronunciation audio: {e}"
|
| 86 |
-
|
| 87 |
|
| 88 |
# Step 3: Compare the transcribed text with the input paragraph
|
| 89 |
def compare_texts(reference_text, transcribed_text):
|
|
@@ -133,7 +103,7 @@ def compare_texts(reference_text, transcribed_text):
|
|
| 133 |
suggestion = difflib.get_close_matches(word, reference_words, n=1)
|
| 134 |
suggestion_text = f" (Did you mean: <em>{suggestion[0]}</em>?)" if suggestion else ""
|
| 135 |
html_output += f'{word}: '
|
| 136 |
-
html_output += f'<audio controls><source src="{audio}" type="audio/
|
| 137 |
|
| 138 |
return [html_output, [audio for _, audio in incorrect_words_audios]]
|
| 139 |
|
|
@@ -145,7 +115,7 @@ def text_to_speech(paragraph):
|
|
| 145 |
tts = gTTS(paragraph)
|
| 146 |
audio_file_path = "audio/paragraph.mp3" # Save the audio to a file
|
| 147 |
tts.save(audio_file_path)
|
| 148 |
-
return audio_file_path # Return the file path
|
| 149 |
|
| 150 |
# Gradio Interface Function
|
| 151 |
def gradio_function(paragraph, audio):
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import speech_recognition as sr
|
| 3 |
import difflib
|
| 4 |
import gradio as gr
|
|
|
|
| 45 |
except sr.RequestError as e:
|
| 46 |
return f"Error with Google Speech Recognition service: {e}"
|
| 47 |
|
| 48 |
+
# Step 2: Create pronunciation audio for incorrect words (locally)
|
| 49 |
+
def create_pronunciation_audio(word):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
+
tts = gTTS(word)
|
| 52 |
+
audio_file_path = f"audio/{word}.mp3"
|
| 53 |
+
tts.save(audio_file_path)
|
| 54 |
+
return audio_file_path # Return the local path instead of uploading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
+
return f"Failed to create pronunciation audio: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Step 3: Compare the transcribed text with the input paragraph
|
| 59 |
def compare_texts(reference_text, transcribed_text):
|
|
|
|
| 103 |
suggestion = difflib.get_close_matches(word, reference_words, n=1)
|
| 104 |
suggestion_text = f" (Did you mean: <em>{suggestion[0]}</em>?)" if suggestion else ""
|
| 105 |
html_output += f'{word}: '
|
| 106 |
+
html_output += f'<audio controls><source src="{audio}" type="audio/mpeg">Your browser does not support the audio tag.</audio>{suggestion_text}<br>'
|
| 107 |
|
| 108 |
return [html_output, [audio for _, audio in incorrect_words_audios]]
|
| 109 |
|
|
|
|
| 115 |
tts = gTTS(paragraph)
|
| 116 |
audio_file_path = "audio/paragraph.mp3" # Save the audio to a file
|
| 117 |
tts.save(audio_file_path)
|
| 118 |
+
return audio_file_path # Return the file path
|
| 119 |
|
| 120 |
# Gradio Interface Function
|
| 121 |
def gradio_function(paragraph, audio):
|