Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,30 +14,30 @@ if not os.path.exists('audio'):
|
|
14 |
# Step 1: Transcribe the audio file
|
15 |
def transcribe_audio(audio):
|
16 |
if audio is None:
|
17 |
-
return "No audio file provided."
|
18 |
|
19 |
recognizer = sr.Recognizer()
|
|
|
|
|
|
|
|
|
|
|
20 |
audio_format = audio.split('.')[-1].lower()
|
21 |
|
22 |
-
# Convert to WAV if the audio is not in a supported format
|
23 |
if audio_format != 'wav':
|
24 |
try:
|
25 |
-
# Load the audio file with pydub
|
26 |
audio_segment = AudioSegment.from_file(audio)
|
27 |
wav_path = audio.replace(audio_format, 'wav')
|
28 |
-
audio_segment.export(wav_path, format='wav')
|
29 |
-
audio = wav_path
|
30 |
except Exception as e:
|
31 |
return f"Error converting audio: {e}"
|
32 |
|
33 |
-
# Convert audio into recognizable format for the Recognizer
|
34 |
audio_file = sr.AudioFile(audio)
|
35 |
-
|
36 |
with audio_file as source:
|
37 |
audio_data = recognizer.record(source)
|
38 |
|
39 |
try:
|
40 |
-
# Recognize the audio using Google Web Speech API
|
41 |
transcription = recognizer.recognize_google(audio_data)
|
42 |
return transcription
|
43 |
except sr.UnknownValueError:
|
@@ -45,38 +45,48 @@ def transcribe_audio(audio):
|
|
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
|
49 |
def upfilepath(local_filename):
|
50 |
-
|
51 |
-
# URL để upload tệp âm thanh
|
52 |
upload_url = "https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id=yw08d344te"
|
53 |
-
# Dữ liệu tệp cần upload
|
54 |
files = {'files': open(local_filename, 'rb')}
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
def create_pronunciation_audio(word):
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Step 3: Compare the transcribed text with the input paragraph
|
82 |
def compare_texts(reference_text, transcribed_text):
|
|
|
14 |
# Step 1: Transcribe the audio file
|
15 |
def transcribe_audio(audio):
|
16 |
if audio is None:
|
17 |
+
return "No audio file provided."
|
18 |
|
19 |
recognizer = sr.Recognizer()
|
20 |
+
|
21 |
+
# Check if the file exists
|
22 |
+
if not os.path.isfile(audio):
|
23 |
+
return "Audio file not found."
|
24 |
+
|
25 |
audio_format = audio.split('.')[-1].lower()
|
26 |
|
|
|
27 |
if audio_format != 'wav':
|
28 |
try:
|
|
|
29 |
audio_segment = AudioSegment.from_file(audio)
|
30 |
wav_path = audio.replace(audio_format, 'wav')
|
31 |
+
audio_segment.export(wav_path, format='wav')
|
32 |
+
audio = wav_path
|
33 |
except Exception as e:
|
34 |
return f"Error converting audio: {e}"
|
35 |
|
|
|
36 |
audio_file = sr.AudioFile(audio)
|
|
|
37 |
with audio_file as source:
|
38 |
audio_data = recognizer.record(source)
|
39 |
|
40 |
try:
|
|
|
41 |
transcription = recognizer.recognize_google(audio_data)
|
42 |
return transcription
|
43 |
except sr.UnknownValueError:
|
|
|
45 |
except sr.RequestError as e:
|
46 |
return f"Error with Google Speech Recognition service: {e}"
|
47 |
|
48 |
+
|
49 |
# Step 2: Create pronunciation audio for incorrect words
|
50 |
def upfilepath(local_filename):
|
|
|
|
|
51 |
upload_url = "https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id=yw08d344te"
|
|
|
52 |
files = {'files': open(local_filename, 'rb')}
|
53 |
+
|
54 |
+
try:
|
55 |
+
response = requests.post(upload_url, files=files, timeout=30) # Set timeout (e.g., 30 seconds)
|
56 |
+
|
57 |
+
if response.status_code == 200:
|
58 |
+
print("Upload thành công!")
|
59 |
+
result = response.json()
|
60 |
+
extracted_path = result[0]
|
61 |
+
print(extracted_path)
|
62 |
+
return extracted_path
|
63 |
+
else:
|
64 |
+
print(f"Lỗi: {response.status_code}")
|
65 |
+
print(response.text)
|
66 |
+
return None
|
67 |
+
|
68 |
+
except requests.exceptions.Timeout:
|
69 |
+
return "Request timed out. Please try again."
|
70 |
+
except Exception as e:
|
71 |
+
return f"An error occurred: {e}"
|
72 |
+
|
73 |
|
74 |
def create_pronunciation_audio(word):
|
75 |
+
retries = 3 # Retry up to 3 times
|
76 |
+
for attempt in range(retries):
|
77 |
+
try:
|
78 |
+
tts = gTTS(word)
|
79 |
+
audio_file_path = f"audio/{word}.mp3"
|
80 |
+
tts.save(audio_file_path)
|
81 |
+
word_audio = upfilepath(audio_file_path)
|
82 |
+
if word_audio:
|
83 |
+
return f"https://mr2along-speech-recognize.hf.space/gradio_api/file={word_audio}"
|
84 |
+
except Exception as e:
|
85 |
+
if attempt < retries - 1:
|
86 |
+
time.sleep(2 ** attempt) # Exponential backoff
|
87 |
+
else:
|
88 |
+
return f"Failed to create pronunciation audio: {e}"
|
89 |
+
|
90 |
|
91 |
# Step 3: Compare the transcribed text with the input paragraph
|
92 |
def compare_texts(reference_text, transcribed_text):
|