Al1Abdullah commited on
Commit
9ae891a
·
verified ·
1 Parent(s): 3e9e923

Create src/audio_processor.py

Browse files
Files changed (1) hide show
  1. src/audio_processor.py +38 -0
src/audio_processor.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import speech_recognition as sr
2
+ from gtts import gTTS
3
+ import tempfile
4
+ from pydub import AudioSegment
5
+
6
+ class AudioProcessor:
7
+ def __init__(self):
8
+ self.recognizer = sr.Recognizer()
9
+
10
+ def audio_to_text(self, audio_file):
11
+ """Process an uploaded audio file and convert it to text."""
12
+ try:
13
+ # Convert audio file to WAV format
14
+ audio = AudioSegment.from_file(audio_file)
15
+ wav_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
16
+ audio.export(wav_file.name, format="wav")
17
+ print(f"Converted audio to WAV: {wav_file.name}") # Debug statement
18
+
19
+ with sr.AudioFile(wav_file.name) as source:
20
+ audio = self.recognizer.record(source)
21
+ try:
22
+ text = self.recognizer.recognize_google(audio)
23
+ except sr.UnknownValueError:
24
+ text = "Could not understand audio"
25
+ except sr.RequestError:
26
+ text = "Could not request results"
27
+ print(f"Recognized text: {text}") # Debug statement
28
+ return text
29
+ except Exception as e:
30
+ print(f"Error processing audio file: {e}") # Debug statement
31
+ return f"Error processing audio file: {e}"
32
+
33
+ def text_to_speech(self, text):
34
+ """Convert text to speech using gTTS and save as a .mp3 file."""
35
+ tts = gTTS(text)
36
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
37
+ tts.save(temp_audio.name)
38
+ return temp_audio.name