Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
AudioSegment(audio_data, sample_rate=sample_rate, frame_rate=sample_rate, channels=1).export(temp_file.name, format="wav")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 41 |
-
if
|
| 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
|