-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcode_copy.txt
More file actions
144 lines (111 loc) · 5.26 KB
/
Copy pathcode_copy.txt
File metadata and controls
144 lines (111 loc) · 5.26 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import openai
import pyttsx3
import speech_recognition as sr
import random
# Set OpenAI API key
openai.api_key = "sk-CDUDYs3rDIq1n4L9iS2AG3B2bkFJzWEvKLw8ys9ARv19D3DO"
model_id = 'gpt-3.5-turbo'
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Change speech rate
engine.setProperty('rate', 180)
# Get the avaiable voice
voices = engine.getProperty('voices')
# Choose a voice based on the voice id
engine.setProperty('voice', voices[0].id)
# Counter just for interacting purposes
interaction_counter = 0
def transcribe_audio_to_text(filename):
recognizer = sr.Recognizer()
with sr.AudioFile(filename) as source:
audio = recognizer.record(source)
try:
return recognizer.recognize_google(audio)
except:
print("")
#print('Skipping unknown error')
def ChatGPT_conversation(conversation):
response = openai.ChatCompletion.create(
model=model_id,
messages=conversation
)
api_usage = response['usage']
print('Total token consumed: {0}'.format(api_usage['total_tokens']))
conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
return conversation
def speak_text(text):
engine.say(text)
engine.runAndWait()
# Starting conversation
conversation = []
conversation.append({'role': 'user', 'content': 'chat with me as you be Friday AI from Iron Man, please make a one sentence phrase introducing yourself without saying something that sounds like this chat its already started'})
conversation = ChatGPT_conversation(conversation)
print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))
speak_text(conversation[-1]['content'].strip())
def activate_assistant():
starting_chat_phrases = ["Yes sir, how may I assist you?",
"Yes, What can I do for you?",
"How can I help you, sir?",
"Friday at your service, what do you need?",
"Friday here, how can I help you today?",
"Yes, what can I do for you today?",
"Yes sir, what's on your mind?",
"Friday ready to assist, what can I do for you?",
"At your command, sir. How may I help you today?",
"Yes, sir. How may I be of assistance to you right now?",
"Yes boss, I'm here to help. What do you need from me?",
"Yes, I'm listening. What can I do for you, sir?",
"How can I assist you today, sir?",
"Friday here, ready and eager to help. What can I do for you?",
"Yes, sir. How can I make your day easier?",
"Yes boss, what's the plan? How can I assist you today?",
"Yes, I'm here and ready to assist. What's on your mind, sir?"]
continued_chat_phrases = ["yes", "yes, sir", "yes, boss", "I'm all ears"]
random_chat = ""
if(interaction_counter == 1):
random_chat = random.choice(starting_chat_phrases)
else:
random_chat = random.choice(continued_chat_phrases)
return random_chat
def append_to_log(text):
with open("chat_log.txt", "a") as f:
f.write(text + "\n")
while True:
#wait for users to say "Friday"
print("Say 'Friday' to start...")
recognizer = sr.Recognizer()
with sr.Microphone() as source:
audio = recognizer.listen(source)
try:
transcription = recognizer.recognize_google(audio)
if "friday" in transcription.lower():
interaction_counter += 1
# Record audio
filename = "input.wav"
readyToWork = activate_assistant()
speak_text(readyToWork)
print(readyToWork)
recognizer = sr.Recognizer()
with sr.Microphone() as source:
source.pause_threshold = 1
audio = recognizer.listen(source, phrase_time_limit=None, timeout=None)
with open(filename, "wb") as f:
f.write(audio.get_wav_data())
# Transcribe audio to text
text = transcribe_audio_to_text(filename)
if text:
print(f"You said: {text}")
append_to_log(f"You: {text}\n")
# Generate response using chatGPT
print(f"Friday says: {conversation}")
prompt = text
conversation.append({'role': 'user', 'content': prompt})
conversation = ChatGPT_conversation(conversation)
print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))
append_to_log(f"Friday: {conversation[-1]['content'].strip()}\n")
# Read response using text-to-speech
speak_text(conversation[-1]['content'].strip())
# In future maybe a conversation.clear to decrease input tokens as the conversation evolves ...
except Exception as e:
continue
#print("An error occurred: {}".format(e))