Update app.py
Browse files
app.py
CHANGED
@@ -5,27 +5,32 @@ from pydub import AudioSegment
|
|
5 |
import io
|
6 |
import tempfile
|
7 |
import speech_recognition as sr
|
|
|
8 |
|
9 |
def transcribe_audio(audio):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Perform speech recognition
|
20 |
-
recognizer = sr.Recognizer()
|
21 |
-
with sr.AudioFile(temp_audio_path) as source:
|
22 |
-
audio_data = recognizer.record(source)
|
23 |
-
text = recognizer.recognize_google(audio_data)
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def process_audio(audio, api_token):
|
31 |
if not api_token:
|
@@ -39,6 +44,8 @@ def process_audio(audio, api_token):
|
|
39 |
|
40 |
# Transcribe the input audio
|
41 |
transcription = transcribe_audio(audio)
|
|
|
|
|
42 |
|
43 |
try:
|
44 |
# Process the transcription with the API
|
@@ -79,7 +86,7 @@ def process_audio(audio, api_token):
|
|
79 |
return response_text, temp_audio_path
|
80 |
|
81 |
except Exception as e:
|
82 |
-
return f"An error occurred: {str(e)}", None
|
83 |
|
84 |
# Create the Gradio interface
|
85 |
iface = gr.Interface(
|
|
|
5 |
import io
|
6 |
import tempfile
|
7 |
import speech_recognition as sr
|
8 |
+
import os
|
9 |
|
10 |
def transcribe_audio(audio):
|
11 |
+
try:
|
12 |
+
# Convert the audio to wav format
|
13 |
+
audio = AudioSegment.from_file(audio)
|
14 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
15 |
+
|
16 |
+
# Save as wav file
|
17 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
|
18 |
+
audio.export(temp_audio.name, format="wav")
|
19 |
+
temp_audio_path = temp_audio.name
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Perform speech recognition
|
22 |
+
recognizer = sr.Recognizer()
|
23 |
+
with sr.AudioFile(temp_audio_path) as source:
|
24 |
+
audio_data = recognizer.record(source)
|
25 |
+
text = recognizer.recognize_google(audio_data)
|
26 |
|
27 |
+
return text
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error in transcription: {str(e)}"
|
30 |
+
finally:
|
31 |
+
# Clean up the temporary file
|
32 |
+
if 'temp_audio_path' in locals():
|
33 |
+
os.unlink(temp_audio_path)
|
34 |
|
35 |
def process_audio(audio, api_token):
|
36 |
if not api_token:
|
|
|
44 |
|
45 |
# Transcribe the input audio
|
46 |
transcription = transcribe_audio(audio)
|
47 |
+
if transcription.startswith("Error in transcription:"):
|
48 |
+
return transcription, None
|
49 |
|
50 |
try:
|
51 |
# Process the transcription with the API
|
|
|
86 |
return response_text, temp_audio_path
|
87 |
|
88 |
except Exception as e:
|
89 |
+
return f"An error occurred during API processing: {str(e)}", None
|
90 |
|
91 |
# Create the Gradio interface
|
92 |
iface = gr.Interface(
|