-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory9.py
More file actions
172 lines (146 loc) · 7 KB
/
memory9.py
File metadata and controls
172 lines (146 loc) · 7 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from gtts import gTTS
import os
import pygame
import json
from openai import OpenAI
pygame.init()
pygame.mixer.init
client = OpenAI()
class UserProfileManager:
def __init__(self, file_path):
self.file_path = file_path
self.profiles = self.load_profiles()
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
def text_to_speech(self, text):
tts = gTTS(text=text, lang='en')
output_file_path = "output.wav"
tts.save(output_file_path) # Save as WAV
print(f"Text-to-speech audio saved as '{output_file_path}'.")
self.outputsound = pygame.mixer.Sound("output.wav")
pygame.mixer.Sound.play(self.outputsound)
def play_wav(self, output):
pygame.mixer.music.load(output)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
def load_profiles(self):
try:
with open(self.file_path, 'r') as file:
return json.load(file)
except FileNotFoundError:
return {}
def save_profiles(self):
with open(self.file_path, 'w') as file:
json.dump(self.profiles, file, indent=2)
def create_profile(self, name, age, interests):
if name not in self.profiles:
self.profiles[name] = {'age': age, 'interests': interests}
print(f"Profile created for {name}")
self.save_profiles()
# Perform TTS and save as WAV
self.text_to_speech(f"Profile created for {name}")
else:
print(f"Name '{name}' already exists. Please choose a different name.")
self.text_to_speech(f"Name '{name}' already exists. Please choose a different name.")
def activate_profile(self, name):
if name in self.profiles:
profile = self.profiles[name]
self.name = name
self.age = profile['age']
self.interests = profile['interests']
print(f"Name: {name}, Age: {self.age}, Interests: {self.interests}")
self.text_to_speech(f"Activating profile for {name}. Age: {self.age}, Interests: {self.interests}")
else:
print(f"Name '{name}' not found. Please enter a valid Name.")
self.text_to_speech(f"Name '{name}' not found. Please enter a valid Name.")
def edit_profile(self, name, field, new_value):
if name in self.profiles:
if field in self.profiles[name]:
self.profiles[name][field] = new_value
print(f"Profile updated for {name}")
self.save_profiles()
self.text_to_speech(f"Profile updated for {name}")
# Perform TTS and save as WAV
self.text_to_speech(f"Profile updated for {name}")
else:
print(f"Field '{field}' not found in the profile.")
self.text_to_speech(f"Field '{field}' not found in the profile.")
else:
print(f"Name '{name}' not found. Please enter a valid Name.")
self.text_to_speech(f"Name '{name}' not found. Please enter a valid Name.")
def view_profiles(self):
print("User Profiles:")
for name, profile in self.profiles.items():
print(f"Name: {name}, Age: {profile['age']}, Interests: {profile['interests']}")
self.text_to_speech(f"Name: {name}, Age: {profile['age']}, Interests: {profile['interests']}")
def chat(self, user_input):
conversation_history = [{"role": "system", "content": f"You are an assistant talking to {self.name} who is {self.age} years old. They enjoy {self.interests}."}]
# Append user input to conversation history
conversation_history.append({"role": "user", "content": user_input})
# Call the OpenAI API with the conversation history
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=conversation_history,
)
# Append the bot's response to the conversation history
response = completion.choices[0].message.content
conversation_history.append({"role": "assistant", "content": response})
return response
def chat_with_gpt(self):
while True:
user_input = input("You: ")
if user_input.lower() == "quit":
break
response = self.chat(user_input)
print("ChatGPT: ", response)
# Example usage
file_path = "user_profiles.json"
user_manager = UserProfileManager(file_path)
first_time = True
activate = False
while True:
print("\n1. Create Profile\n2. Activate Profile\n3. Edit Profile\n4. View Profiles\n5. Activate Chat\n6. Exit")
if first_time:
UserProfileManager.text_to_speech(UserProfileManager, f"1- create profile. 2- activate profile. 3- edit profile. 4- view profiles. 5- activate chat. 6- exit. Enter your choice:")
first_time = False
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter name: ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter name:")
age = input("Enter age: ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter Age:")
interests = input("Enter interests:")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter interests:")
user_manager.create_profile(name, age, interests)
elif choice == '2':
activated = True
name = input("Enter name to activate: ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter name to activate:")
user_manager.activate_profile(name)
elif choice == '3':
name = input("Enter name: ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter name:")
field = input("Enter field to edit (age or interests): ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter field to edit, (age or interests)")
new_value = input(f"Enter new value for {field}: ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter new value for {field}:")
user_manager.edit_profile(name, field, new_value)
elif choice == '4':
user_manager.view_profiles()
elif choice == '5':
if not activate:
activate = True
name = input("Enter name to activate: ")
UserProfileManager.text_to_speech(UserProfileManager, f"Enter name to activate:")
user_manager.activate_profile(name)
else:
user_manager.chat_with_gpt()
elif choice == '6':
print("Exiting the program. Goodbye!")
UserProfileManager.text_to_speech(UserProfileManager, f"Exiting the program. Goodbye!")
import time
time.sleep(1.5)
break
else:
print("Invalid choice. Please enter a valid option.")
UserProfileManager.text_to_speech(UserProfileManager, f"Invalid choice. Please enter a valid option.")