Spaces:
Sleeping
Sleeping
Update agents/voice_agent.py
Browse files- agents/voice_agent.py +23 -12
agents/voice_agent.py
CHANGED
@@ -1,17 +1,28 @@
|
|
|
|
|
|
1 |
import speech_recognition as sr
|
2 |
-
import
|
|
|
3 |
|
4 |
-
|
|
|
|
|
5 |
recognizer = sr.Recognizer()
|
6 |
-
with sr.
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
def text_to_speech(text):
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
import speech_recognition as sr
|
4 |
+
from gtts import gTTS
|
5 |
+
from datetime import datetime
|
6 |
|
7 |
+
|
8 |
+
def speech_to_text(audio_file):
|
9 |
+
"""Convert uploaded audio file to text using SpeechRecognition + Google API"""
|
10 |
recognizer = sr.Recognizer()
|
11 |
+
with sr.AudioFile(audio_file) as source:
|
12 |
+
audio = recognizer.record(source)
|
13 |
+
try:
|
14 |
+
return recognizer.recognize_google(audio)
|
15 |
+
except sr.UnknownValueError:
|
16 |
+
return "Could not understand audio."
|
17 |
+
except sr.RequestError:
|
18 |
+
return "Speech recognition service unavailable."
|
19 |
+
|
20 |
|
21 |
def text_to_speech(text):
|
22 |
+
"""Convert text to speech and return audio file path"""
|
23 |
+
os.makedirs("temp_audio", exist_ok=True)
|
24 |
+
filename = f"tts_{datetime.now().strftime('%Y%m%d%H%M%S')}.mp3"
|
25 |
+
filepath = os.path.join("temp_audio", filename)
|
26 |
+
tts = gTTS(text)
|
27 |
+
tts.save(filepath)
|
28 |
+
return filepath
|