zoya23 commited on
Commit
4738167
·
verified ·
1 Parent(s): ff96c8d

Update agents/voice_agent.py

Browse files
Files changed (1) hide show
  1. agents/voice_agent.py +23 -12
agents/voice_agent.py CHANGED
@@ -1,17 +1,28 @@
 
 
1
  import speech_recognition as sr
2
- import pyttsx3
 
3
 
4
- def speech_to_text():
 
 
5
  recognizer = sr.Recognizer()
6
- with sr.Microphone() as source:
7
- print("🎤 Listening...")
8
- audio = recognizer.listen(source)
9
- try:
10
- return recognizer.recognize_google(audio)
11
- except:
12
- return "Could not understand."
 
 
13
 
14
  def text_to_speech(text):
15
- engine = pyttsx3.init()
16
- engine.say(text)
17
- engine.runAndWait()
 
 
 
 
 
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