Sephfox commited on
Commit
a46831f
·
verified ·
1 Parent(s): 86ff96a

Update Adam2

Browse files
Files changed (1) hide show
  1. Adam2 +268 -264
Adam2 CHANGED
@@ -1,264 +1,268 @@
1
-
2
- import numpy as np
3
- import pandas as pd
4
- import os
5
- import json
6
- import random
7
- from sklearn.ensemble import IsolationForest
8
- from sklearn.model_selection import train_test_split
9
- from sklearn.preprocessing import OneHotEncoder
10
- from sklearn.neural_network import MLPClassifier
11
- from deap import base, creator, tools, algorithms
12
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
13
-
14
-
15
- # Initialize Example Emotions Dataset
16
- data = {
17
- 'context': [
18
- 'I am happy', 'I am sad', 'I am angry', 'I am excited', 'I am calm',
19
- 'I am feeling joyful', 'I am grieving', 'I am feeling peaceful', 'I am frustrated',
20
- 'I am determined', 'I feel resentment', 'I am feeling glorious', 'I am motivated',
21
- 'I am surprised', 'I am fearful', 'I am trusting', 'I feel disgust', 'I am optimistic',
22
- 'I am pessimistic', 'I feel bored', 'I am envious'
23
- ],
24
- 'emotion': [
25
- 'joy', 'sadness', 'anger', 'joy', 'calmness', 'joy', 'grief', 'calmness', 'anger',
26
- 'determination', 'resentment', 'glory', 'motivation', 'surprise', 'fear', 'trust',
27
- 'disgust', 'optimism', 'pessimism', 'boredom', 'envy'
28
- ]
29
- }
30
- df = pd.DataFrame(data)
31
-
32
-
33
- # Encoding the contexts using One-Hot Encoding
34
- encoder = OneHotEncoder(handle_unknown='ignore')
35
- contexts_encoded = encoder.fit_transform(df[['context']]).toarray()
36
-
37
-
38
- # Encoding emotions
39
- emotions_target = df['emotion'].astype('category').cat.codes
40
- emotion_classes = df['emotion'].astype('category').cat.categories
41
-
42
-
43
- # Train Neural Network
44
- X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
45
- model = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=1000, random_state=42)
46
- model.fit(X_train, y_train)
47
-
48
-
49
- # Isolation Forest Anomaly Detection Model
50
- historical_data = np.array([model.predict(contexts_encoded)]).T
51
- isolation_forest = IsolationForest(contamination=0.1, random_state=42)
52
- isolation_forest.fit(historical_data)
53
-
54
-
55
- # Emotional States with 20 emotions
56
- emotions = {
57
- 'joy': {'percentage': 10, 'motivation': 'positive'},
58
- 'pleasure': {'percentage': 10, 'motivation': 'selfish'},
59
- 'sadness': {'percentage': 10, 'motivation': 'negative'},
60
- 'grief': {'percentage': 10, 'motivation': 'negative'},
61
- 'anger': {'percentage': 10, 'motivation': 'traumatic or strong'},
62
- 'calmness': {'percentage': 10, 'motivation': 'neutral'},
63
- 'determination': {'percentage': 10, 'motivation': 'positive'},
64
- 'resentment': {'percentage': 10, 'motivation': 'negative'},
65
- 'glory': {'percentage': 10, 'motivation': 'positive'},
66
- 'motivation': {'percentage': 10, 'motivation': 'positive'},
67
- 'ideal_state': {'percentage': 100, 'motivation': 'balanced'},
68
- 'fear': {'percentage': 10, 'motivation': 'defensive'},
69
- 'surprise': {'percentage': 10, 'motivation': 'unexpected'},
70
- 'anticipation': {'percentage': 10, 'motivation': 'predictive'},
71
- 'trust': {'percentage': 10, 'motivation': 'reliable'},
72
- 'disgust': {'percentage': 10, 'motivation': 'repulsive'},
73
- 'optimism': {'percentage': 10, 'motivation': 'hopeful'},
74
- 'pessimism': {'percentage': 10, 'motivation': 'doubtful'},
75
- 'boredom': {'percentage': 10, 'motivation': 'indifferent'},
76
- 'envy': {'percentage': 10, 'motivation': 'jealous'}
77
- }
78
-
79
-
80
- # Adjust all emotions to a total of 200%
81
- total_percentage = 200
82
- default_percentage = total_percentage / len(emotions)
83
- for emotion in emotions:
84
- emotions[emotion]['percentage'] = default_percentage
85
-
86
-
87
- emotion_history_file = 'emotion_history.json'
88
-
89
-
90
- # Load historical data from file if exists
91
- def load_historical_data(file_path=emotion_history_file):
92
- if os.path.exists(file_path):
93
- with open(file_path, 'r') as file:
94
- return json.load(file)
95
- return []
96
-
97
-
98
- # Save historical data to file
99
- def save_historical_data(historical_data, file_path=emotion_history_file):
100
- with open(file_path, 'w') as file:
101
- json.dump(historical_data, file)
102
-
103
-
104
- # Load previous emotional states
105
- emotion_history = load_historical_data()
106
-
107
-
108
- # Function to update emotions
109
- def update_emotion(emotion, percentage):
110
- emotions['ideal_state']['percentage'] -= percentage
111
- emotions[emotion]['percentage'] += percentage
112
-
113
- # Ensure total percentage remains 200%
114
- total_current = sum(e['percentage'] for e in emotions.values())
115
- adjustment = total_percentage - total_current
116
- emotions['ideal_state']['percentage'] += adjustment
117
-
118
-
119
- # Function to normalize context
120
- def normalize_context(context):
121
- return context.lower().strip()
122
-
123
-
124
- # Function to evolve emotions using genetic algorithm
125
- def evolve_emotions():
126
- # Define the fitness function
127
- def evaluate(individual):
128
- ideal_state = individual[-1] # Last value is the ideal state percentage
129
- other_emotions = individual[:-1] # All other emotions
130
- return abs(ideal_state - 100), sum(other_emotions)
131
-
132
- # Register the genetic algorithm components
133
- creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
134
- creator.create("Individual", list, fitness=creator.FitnessMin)
135
-
136
- # Create individuals and population
137
- toolbox = base.Toolbox()
138
- toolbox.register("attribute", lambda: random.uniform(0, 20))
139
- toolbox.register("individual", tools.initCycle, creator.Individual, toolbox.attribute, n=(len(emotions) - 1))
140
- toolbox.register("ideal_state", lambda: random.uniform(80, 120))
141
- toolbox.register("complete_individual", tools.initConcat, creator.Individual, toolbox.individual, toolbox.ideal_state)
142
- toolbox.register("population", tools.initRepeat, list, toolbox.complete_individual)
143
-
144
- # Register genetic operators
145
- toolbox.register("evaluate", evaluate)
146
- toolbox.register("mate", tools.cxBlend, alpha=0.5)
147
- toolbox.register("mutate", tools.mutGaussian, mu=10, sigma=5, indpb=0.3)
148
- toolbox.register("select", tools.selTournament, tournsize=3)
149
-
150
- # Initialize the population
151
- population = toolbox.population(n=10)
152
-
153
- # Run genetic algorithm
154
- population, log = algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.2, ngen=20, verbose=False)
155
-
156
- # Update the emotions with the best individual
157
- best_individual = tools.selBest(population, k=1)[0]
158
- for idx, emotion in enumerate(emotions.keys()):
159
- emotions[emotion]['percentage'] = best_individual[idx]
160
-
161
-
162
- # Function to get emotional response
163
- def get_emotional_response(context):
164
- # Normalize context
165
- context = normalize_context(context)
166
-
167
- # Encode the context and predict the emotion using the neural network
168
- context_encoded = encoder.transform([[context]]).toarray()
169
- prediction = model.predict(context_encoded)
170
- predicted_emotion = emotion_classes[prediction[0]]
171
-
172
- # Check for anomalies using Isolation Forest
173
- anomaly_score = isolation_forest.decision_function([prediction])[0]
174
- if anomaly_score < -0.5:
175
- print("Anomalous context detected. Adjusting emotional response.")
176
- update_emotion('calmness', 20)
177
- else:
178
- # Define emotional responses
179
- if predicted_emotion == 'joy':
180
- update_emotion('joy', 20)
181
- update_emotion('pleasure', 20)
182
- elif predicted_emotion == 'sadness':
183
- update_emotion('sadness', 20)
184
- update_emotion('grief', 20)
185
- elif predicted_emotion == 'anger':
186
- update_emotion('anger', 20)
187
- elif predicted_emotion == 'determination':
188
- update_emotion('determination', 20)
189
- elif predicted_emotion == 'resentment':
190
- update_emotion('resentment', 20)
191
- elif predicted_emotion == 'glory':
192
- update_emotion('glory', 20)
193
- elif predicted_emotion == 'motivation':
194
- update_emotion('motivation', 20)
195
- elif predicted_emotion == 'surprise':
196
- update_emotion('surprise', 20)
197
- elif predicted_emotion == 'fear':
198
- update_emotion('fear', 20)
199
- elif predicted_emotion == 'trust':
200
- update_emotion('trust', 20)
201
- elif predicted_emotion == 'disgust':
202
- update_emotion('disgust', 20)
203
- elif predicted_emotion == 'optimism':
204
- update_emotion('optimism', 20)
205
- elif predicted_emotion == 'pessimism':
206
- update_emotion('pessimism', 20)
207
- elif predicted_emotion == 'boredom':
208
- update_emotion('boredom', 20)
209
- elif predicted_emotion == 'envy':
210
- update_emotion('envy', 20)
211
- else:
212
- update_emotion('calmness', 20)
213
-
214
- # Record the current emotional state in history
215
- emotion_state = {emotion: data['percentage'] for emotion, data in emotions.items()}
216
- emotion_history.append(emotion_state)
217
-
218
- # Save the history to file
219
- save_historical_data(emotion_history)
220
-
221
- # Print the current emotional state
222
- for emotion, data in emotions.items():
223
- print(f"{emotion.capitalize()}: {data['percentage']}% ({data['motivation']} motivation)")
224
-
225
-
226
- # Function to handle idle state using genetic algorithm
227
- def handle_idle_state():
228
- print("Entering idle state...")
229
- evolve_emotions()
230
- print("Emotions evolved")
231
- for emotion, data in emotions.items():
232
- print(f"{emotion.capitalize()}: {data['percentage']}% ({data['motivation']} motivation)")
233
-
234
-
235
- # S.O.U.L. (Self-Organizing Universal Learning) Function
236
- class SOUL:
237
- def __init__(self, gpt2_model='gpt2'):
238
- self.gpt2_tokenizer = GPT2Tokenizer.from_pretrained(gpt2_model)
239
- self.gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_model)
240
-
241
- def generate_text(self, prompt, max_length=50):
242
- inputs = self.gpt2_tokenizer.encode(prompt, return_tensors='pt')
243
- outputs = self.gpt2_model.generate(inputs, max_length=max_length, num_return_sequences=1, no_repeat_ngram_size=2)
244
- return self.gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
245
-
246
- def bridge_ai(self, prompt):
247
- # Generate the response using GPT-2
248
- print("\nGPT-2 Response:")
249
- gpt2_response = self.generate_text(prompt)
250
- print(gpt2_response)
251
-
252
- # Get the emotional response
253
- print("\nEmotional Response:")
254
- get_emotional_response(gpt2_response)
255
-
256
-
257
- # Example usage of S.O.U.L. function
258
- soul = SOUL()
259
-
260
-
261
- # Test open-ended conversation with emotional response
262
- while True:
263
- user_input = input("You: ")
264
- soul.bridge_ai(user_input)
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import os
4
+ import json
5
+ import random
6
+ from sklearn.ensemble import IsolationForest
7
+ from sklearn.model_selection import train_test_split
8
+ from sklearn.preprocessing import OneHotEncoder
9
+ from sklearn.neural_network import MLPClassifier
10
+ from deap import base, creator, tools, algorithms
11
+ from transformers import BloomForCausalLM, BloomTokenizerFast
12
+ import torch
13
+
14
+ # Initialize Example Emotions Dataset
15
+ data = {
16
+ 'context': [
17
+ 'I am happy', 'I am sad', 'I am angry', 'I am excited', 'I am calm',
18
+ 'I am feeling joyful', 'I am grieving', 'I am feeling peaceful', 'I am frustrated',
19
+ 'I am determined', 'I feel resentment', 'I am feeling glorious', 'I am motivated',
20
+ 'I am surprised', 'I am fearful', 'I am trusting', 'I feel disgust', 'I am optimistic',
21
+ 'I am pessimistic', 'I feel bored', 'I am envious'
22
+ ],
23
+ 'emotion': [
24
+ 'joy', 'sadness', 'anger', 'joy', 'calmness', 'joy', 'grief', 'calmness', 'anger',
25
+ 'determination', 'resentment', 'glory', 'motivation', 'surprise', 'fear', 'trust',
26
+ 'disgust', 'optimism', 'pessimism', 'boredom', 'envy'
27
+ ]
28
+ }
29
+ df = pd.DataFrame(data)
30
+
31
+ # Encoding the contexts using One-Hot Encoding
32
+ encoder = OneHotEncoder(handle_unknown='ignore')
33
+ contexts_encoded = encoder.fit_transform(df[['context']]).toarray()
34
+
35
+ # Encoding emotions
36
+ emotions_target = df['emotion'].astype('category').cat.codes
37
+ emotion_classes = df['emotion'].astype('category').cat.categories
38
+
39
+ # Train Neural Network
40
+ X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
41
+ model = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=1000, random_state=42)
42
+ model.fit(X_train, y_train)
43
+
44
+ # Isolation Forest Anomaly Detection Model
45
+ historical_data = np.array([model.predict(contexts_encoded)]).T
46
+ isolation_forest = IsolationForest(contamination=0.1, random_state=42)
47
+ isolation_forest.fit(historical_data)
48
+
49
+ # Emotional States with 20 emotions
50
+ emotions = {
51
+ 'joy': {'percentage': 10, 'motivation': 'positive'},
52
+ 'pleasure': {'percentage': 10, 'motivation': 'selfish'},
53
+ 'sadness': {'percentage': 10, 'motivation': 'negative'},
54
+ 'grief': {'percentage': 10, 'motivation': 'negative'},
55
+ 'anger': {'percentage': 10, 'motivation': 'traumatic or strong'},
56
+ 'calmness': {'percentage': 10, 'motivation': 'neutral'},
57
+ 'determination': {'percentage': 10, 'motivation': 'positive'},
58
+ 'resentment': {'percentage': 10, 'motivation': 'negative'},
59
+ 'glory': {'percentage': 10, 'motivation': 'positive'},
60
+ 'motivation': {'percentage': 10, 'motivation': 'positive'},
61
+ 'ideal_state': {'percentage': 100, 'motivation': 'balanced'},
62
+ 'fear': {'percentage': 10, 'motivation': 'defensive'},
63
+ 'surprise': {'percentage': 10, 'motivation': 'unexpected'},
64
+ 'anticipation': {'percentage': 10, 'motivation': 'predictive'},
65
+ 'trust': {'percentage': 10, 'motivation': 'reliable'},
66
+ 'disgust': {'percentage': 10, 'motivation': 'repulsive'},
67
+ 'optimism': {'percentage': 10, 'motivation': 'hopeful'},
68
+ 'pessimism': {'percentage': 10, 'motivation': 'doubtful'},
69
+ 'boredom': {'percentage': 10, 'motivation': 'indifferent'},
70
+ 'envy': {'percentage': 10, 'motivation': 'jealous'}
71
+ }
72
+
73
+ # Adjust all emotions to a total of 200%
74
+ total_percentage = 200
75
+ default_percentage = total_percentage / len(emotions)
76
+ for emotion in emotions:
77
+ emotions[emotion]['percentage'] = default_percentage
78
+
79
+ emotion_history_file = 'emotion_history.json'
80
+
81
+ # Load historical data from file if exists
82
+ def load_historical_data(file_path=emotion_history_file):
83
+ if os.path.exists(file_path):
84
+ with open(file_path, 'r') as file:
85
+ return json.load(file)
86
+ return []
87
+
88
+ # Save historical data to file
89
+ def save_historical_data(historical_data, file_path=emotion_history_file):
90
+ with open(file_path, 'w') as file:
91
+ json.dump(historical_data, file)
92
+
93
+ # Load previous emotional states
94
+ emotion_history = load_historical_data()
95
+
96
+ # Function to update emotions
97
+ def update_emotion(emotion, percentage):
98
+ emotions['ideal_state']['percentage'] -= percentage
99
+ emotions[emotion]['percentage'] += percentage
100
+
101
+ # Ensure total percentage remains 200%
102
+ total_current = sum(e['percentage'] for e in emotions.values())
103
+ adjustment = total_percentage - total_current
104
+ emotions['ideal_state']['percentage'] += adjustment
105
+
106
+ # Function to normalize context
107
+ def normalize_context(context):
108
+ return context.lower().strip()
109
+
110
+ # Function to evolve emotions using genetic algorithm
111
+ def evolve_emotions():
112
+ # Define the fitness function
113
+ def evaluate(individual):
114
+ ideal_state = individual[-1] # Last value is the ideal state percentage
115
+ other_emotions = individual[:-1] # All other emotions
116
+ return abs(ideal_state - 100), sum(other_emotions)
117
+
118
+ # Register the genetic algorithm components
119
+ creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
120
+ creator.create("Individual", list, fitness=creator.FitnessMin)
121
+
122
+ # Create individuals and population
123
+ toolbox = base.Toolbox()
124
+ toolbox.register("attribute", lambda: random.uniform(0, 20))
125
+ toolbox.register("individual", tools.initCycle, creator.Individual, toolbox.attribute, n=(len(emotions) - 1))
126
+ toolbox.register("ideal_state", lambda: random.uniform(80, 120))
127
+ toolbox.register("complete_individual", tools.initConcat, creator.Individual, toolbox.individual, toolbox.ideal_state)
128
+ toolbox.register("population", tools.initRepeat, list, toolbox.complete_individual)
129
+
130
+ # Register genetic operators
131
+ toolbox.register("evaluate", evaluate)
132
+ toolbox.register("mate", tools.cxBlend, alpha=0.5)
133
+ toolbox.register("mutate", tools.mutGaussian, mu=10, sigma=5, indpb=0.3)
134
+ toolbox.register("select", tools.selTournament, tournsize=3)
135
+
136
+ # Initialize the population
137
+ population = toolbox.population(n=10)
138
+
139
+ # Run genetic algorithm
140
+ population, log = algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.2, ngen=20, verbose=False)
141
+
142
+ # Update the emotions with the best individual
143
+ best_individual = tools.selBest(population, k=1)[0]
144
+ for idx, emotion in enumerate(emotions.keys()):
145
+ emotions[emotion]['percentage'] = best_individual[idx]
146
+
147
+ # Function to get emotional response
148
+ def get_emotional_response(context):
149
+ # Normalize context
150
+ context = normalize_context(context)
151
+
152
+ # Encode the context and predict the emotion using the neural network
153
+ context_encoded = encoder.transform([[context]]).toarray()
154
+ prediction = model.predict(context_encoded)
155
+ predicted_emotion = emotion_classes[prediction[0]]
156
+
157
+ # Check for anomalies using Isolation Forest
158
+ anomaly_score = isolation_forest.decision_function([prediction])[0]
159
+ if anomaly_score < -0.5:
160
+ print("Anomalous context detected. Adjusting emotional response.")
161
+ update_emotion('calmness', 20)
162
+ else:
163
+ # Define emotional responses
164
+ if predicted_emotion == 'joy':
165
+ update_emotion('joy', 20)
166
+ update_emotion('pleasure', 20)
167
+ elif predicted_emotion == 'sadness':
168
+ update_emotion('sadness', 20)
169
+ update_emotion('grief', 20)
170
+ elif predicted_emotion == 'anger':
171
+ update_emotion('anger', 20)
172
+ elif predicted_emotion == 'determination':
173
+ update_emotion('determination', 20)
174
+ elif predicted_emotion == 'resentment':
175
+ update_emotion('resentment', 20)
176
+ elif predicted_emotion == 'glory':
177
+ update_emotion('glory', 20)
178
+ elif predicted_emotion == 'motivation':
179
+ update_emotion('motivation', 20)
180
+ elif predicted_emotion == 'surprise':
181
+ update_emotion('surprise', 20)
182
+ elif predicted_emotion == 'fear':
183
+ update_emotion('fear', 20)
184
+ elif predicted_emotion == 'trust':
185
+ update_emotion('trust', 20)
186
+ elif predicted_emotion == 'disgust':
187
+ update_emotion('disgust', 20)
188
+ elif predicted_emotion == 'optimism':
189
+ update_emotion('optimism', 20)
190
+ elif predicted_emotion == 'pessimism':
191
+ update_emotion('pessimism', 20)
192
+ elif predicted_emotion == 'boredom':
193
+ update_emotion('boredom', 20)
194
+ elif predicted_emotion == 'envy':
195
+ update_emotion('envy', 20)
196
+ else:
197
+ update_emotion('calmness', 20)
198
+
199
+ # Record the current emotional state in history
200
+ emotion_state = {emotion: data['percentage'] for emotion, data in emotions.items()}
201
+ emotion_history.append(emotion_state)
202
+
203
+ # Save the history to file
204
+ save_historical_data(emotion_history)
205
+
206
+ # Print the current emotional state
207
+ for emotion, data in emotions.items():
208
+ print(f"{emotion.capitalize()}: {data['percentage']:.2f}% ({data['motivation']} motivation)")
209
+
210
+ # Function to handle idle state using genetic algorithm
211
+ def handle_idle_state():
212
+ print("Entering idle state...")
213
+ evolve_emotions()
214
+ print("Emotions evolved")
215
+ for emotion, data in emotions.items():
216
+ print(f"{emotion.capitalize()}: {data['percentage']:.2f}% ({data['motivation']} motivation)")
217
+
218
+ # S.O.U.L. (Self-Organizing Universal Learning) Function
219
+ class SOUL:
220
+ def __init__(self, model_name='bigscience/bloom-1b1'):
221
+ self.tokenizer = BloomTokenizerFast.from_pretrained(model_name)
222
+ self.model = BloomForCausalLM.from_pretrained(model_name)
223
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
224
+ self.model.to(self.device)
225
+
226
+ def generate_text(self, prompt, max_length=100):
227
+ inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
228
+
229
+ # Generate
230
+ with torch.no_grad():
231
+ generate_ids = self.model.generate(
232
+ inputs.input_ids,
233
+ max_length=max_length,
234
+ num_return_sequences=1,
235
+ no_repeat_ngram_size=2,
236
+ do_sample=True,
237
+ top_k=50,
238
+ top_p=0.95,
239
+ temperature=0.7
240
+ )
241
+
242
+ return self.tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
243
+
244
+ def bridge_ai(self, prompt):
245
+ # Generate the response using BLOOM
246
+ print("\nBLOOM Response:")
247
+ bloom_response = self.generate_text(prompt)
248
+ print(bloom_response)
249
+
250
+ # Get the emotional response
251
+ print("\nEmotional Response:")
252
+ get_emotional_response(bloom_response)
253
+
254
+ # Example usage of S.O.U.L. function
255
+ soul = SOUL()
256
+
257
+ # Test open-ended conversation with emotional response
258
+ print("Welcome to the SOUL AI. Type 'exit' to end the conversation.")
259
+ while True:
260
+ user_input = input("You: ")
261
+ if user_input.lower() == 'exit':
262
+ print("Thank you for the conversation. Goodbye!")
263
+ break
264
+ soul.bridge_ai(user_input)
265
+
266
+ # Simulate idle state every 5 interactions
267
+ if len(emotion_history) % 5 == 0:
268
+ handle_idle_state()