mr2along commited on
Commit
6ac5a2f
1 Parent(s): 145903a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -34
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." # Handle the case when no audio is uploaded
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') # Convert to WAV
29
- audio = wav_path # Update audio path to the converted file
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
- # Gửi yêu cầu POST
57
- response = requests.post(upload_url, files=files)
58
-
59
- # Kiểm tra kết quả trả về từ server
60
- if response.status_code == 200:
61
- print("Upload thành công!")
62
- result=response.json()
63
- extracted_path = result[0]
64
- print(extracted_path) # In kết quả nếu server trả về dưới dạng JSON
65
- return extracted_path
66
- else:
67
- print(f"Lỗi: {response.status_code}")
68
- print(response.text) # In thông báo lỗi từ server
 
 
 
 
 
 
69
 
70
  def create_pronunciation_audio(word):
71
- time.sleep(1) # Chờ 5 giây
72
- tts = gTTS(word)
73
- main_url="https://mr2along-speech-recognize.hf.space/gradio_api/file="
74
-
75
- audio_file_path = f"audio/{word}.mp3" # Save the audio to a file
76
- tts.save(audio_file_path)
77
- word_audio=upfilepath(audio_file_path)
78
- #print(f"Lỗi: {word_audio}")
79
- return f"{main_url}{word_audio}" # Return the file path of the saved audio
 
 
 
 
 
 
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):