omar1232 commited on
Commit
d498a93
·
verified ·
1 Parent(s): 9fea2cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -7,17 +7,22 @@ import os
7
 
8
  # Process audio and transcribe
9
  def process_audio(audio_input):
10
- recognizer = sr.Recognizer() # Correct usage of 'sr' as the module alias
11
 
12
- if isinstance(audio_input, tuple): # Recorded audio (sample_rate, numpy_array)
13
- sample_rate, audio_data = audio_input # Rename 'sr' to 'sample_rate'
14
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
 
15
  AudioSegment(audio_data, sample_rate=sample_rate, frame_rate=sample_rate, channels=1).export(temp_file.name, format="wav")
16
- audio_file_path = temp_file.name
17
- else: # Uploaded audio file
18
- audio_file_path = audio_input
19
-
20
- with sr.AudioFile(audio_file_path) as source: # Now 'sr' is correctly defined
 
 
 
 
21
  audio = recognizer.record(source)
22
  try:
23
  transcription = recognizer.recognize_google(audio)
@@ -37,8 +42,8 @@ def process_audio(audio_input):
37
  text_file.write(transcription)
38
  text_file_path = text_file.name
39
 
40
- # Clean up temporary audio file (if created)
41
- if isinstance(audio_input, tuple) and os.path.exists(audio_file_path):
42
  os.remove(audio_file_path)
43
 
44
  return language, transcription, text_file_path
 
7
 
8
  # Process audio and transcribe
9
  def process_audio(audio_input):
10
+ recognizer = sr.Recognizer()
11
 
12
+ # Convert all audio inputs to WAV format
13
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
14
+ if isinstance(audio_input, tuple): # Recorded audio (sample_rate, numpy_array)
15
+ sample_rate, audio_data = audio_input
16
  AudioSegment(audio_data, sample_rate=sample_rate, frame_rate=sample_rate, channels=1).export(temp_file.name, format="wav")
17
+ else: # Uploaded audio file
18
+ # Load the uploaded audio file and convert it to WAV
19
+ audio = AudioSegment.from_file(audio_input)
20
+ audio = audio.set_channels(1) # Convert to mono for consistency
21
+ audio.export(temp_file.name, format="wav")
22
+ audio_file_path = temp_file.name
23
+
24
+ # Transcribe the WAV file
25
+ with sr.AudioFile(audio_file_path) as source:
26
  audio = recognizer.record(source)
27
  try:
28
  transcription = recognizer.recognize_google(audio)
 
42
  text_file.write(transcription)
43
  text_file_path = text_file.name
44
 
45
+ # Clean up temporary WAV file
46
+ if os.path.exists(audio_file_path):
47
  os.remove(audio_file_path)
48
 
49
  return language, transcription, text_file_path