-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (42 loc) · 1.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import speech_recognition as sr
from gtts import gTTS
from functions import query
import sys
def process_speech():
"""
Process speech input by:
- Creating a speech recognizer
- Recording audio from a microphone
- Recognizing speech using Google's speech recognition
- Sending the detected speech to the Falcon-7B-Instruct model for generating a response
- Printing the generated text
- Converting the generated text to speech and saving it as an audio file
- Playing the generated audio
"""
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
detected_speech: str = recognizer.recognize_google(audio)
print("Detected speech:", detected_speech)
response = query(detected_speech)
tts = gTTS(text=response, lang="en", tld="co.in")
tts.save("audio.mp3")
platforms = {
"win32": "start audio.mp3",
"darwin": "open audio.mp3",
"linux": "xdg-open audio.mp3",
}
platform = sys.platform
os.system(platforms[platform])
except sr.UnknownValueError:
print("No speech detected")
except Exception as e:
print("An error occurred:", e)
if __name__ == "__main__":
try:
process_speech()
except KeyboardInterrupt:
pass