-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalktogeminiAI1.py
107 lines (88 loc) · 3.17 KB
/
talktogeminiAI1.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import google.generativeai as gai
from dotenv import load_dotenv
import os
import tempfile
import pygame
from gtts import gTTS
import speech_recognition as sr
# load environment variables
load_dotenv()
#define google api_key in environment variables
gai.configure(api_key=os.environ["Google_API_KEY"])
# define model
model = gai.GenerativeModel('gemini-pro')
#define chat condition
chat = model.start_chat()
#chats with gemini generative ai setting the prompt
def chat_lm(prompt):
response = model.generate_content(
contents=prompt,
stop=None,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=0.7,
)
model.generate_content(prompt, stop=None, callbacks=None)
#speaks the text using gtts
def speak(text):
tts = gTTS(text=text, lang='en')
temp = tempfile.NamedTemporaryFile(delete=False)
temp.close()
tts.save(temp.name) # Corrected line
#pygame setting to record to tempfile and play voice input
pygame.mixer.init()
pygame.mixer.music.load(temp.name)
pygame.mixer.music.play()
print(text)
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.mixer.quit()
#define input microphone and speech recognition
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening...")
speak("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
text = ''
try:
text = r.recognize_google(audio)
# Error handling
except sr.RequestError as re:
print(re)
print("Sorry, I encountered an error. Please try again.")
except sr.UnknownValueError as uve:
print(uve)
print("Sorry, I couldn't understand. Please try again.")
except sr.WaitTimeoutError as wte:
print(wte)
print("Sorry, the operation timed out. Please try again.")
text = text.lower()
return text
#main loop
if __name__ == "__main__":
while True:
human_input = listen()# define human input equal audio source
prompt=human_input #define prompt equals human input
print(prompt)#print input
speak( "User said: " + human_input)# voice playback input
#error handling if human_input is not defined, then continue loop
if not human_input:
print("I didn't catch that. Could you please repeat?")
speak("I didn't catch that. Could you please repeat?")
continue
# if human_input is in quit, exit, stop, bye, goodbye, then break loop
if human_input.lower() in [ "quit", "exit", "stop", "bye", "goodbye"]:
break
#define response equal to model.generate_content(human_input)
response = model.generate_content(human_input)
#define response_text equal to response.text
response_text = response.text
#speak text response
speak(response_text)
#print text response
print(response_text)