Sephfox commited on
Commit
6ea58c0
·
verified ·
1 Parent(s): 582139f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -77
app.py CHANGED
@@ -9,7 +9,7 @@ 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 BloomForCausalLM, BloomTokenizerFast
13
  import torch
14
  import torch.multiprocessing as mp
15
 
@@ -195,87 +195,28 @@ def get_emotional_response(context):
195
  update_emotion('boredom', 20)
196
  elif predicted_emotion == 'envy':
197
  update_emotion('envy', 20)
198
- else:
199
- update_emotion('calmness', 20)
200
-
201
- # Record the current emotional state in history
202
- emotion_state = {emotion: data['percentage'] for emotion, data in emotions.items()}
203
- emotion_history.append(emotion_state)
204
-
205
- # Save the history to file
206
- save_historical_data(emotion_history)
207
-
208
- # Print the current emotional state
209
- response = ""
210
- for emotion, data in emotions.items():
211
- response += f"{emotion.capitalize()}: {data['percentage']:.2f}% ({data['motivation']} motivation)\n"
212
 
213
- return response
214
-
215
- # Function to handle idle state using genetic algorithm
216
- def handle_idle_state():
217
  evolve_emotions()
218
- response = "Emotions evolved\n"
219
- for emotion, data in emotions.items():
220
- response += f"{emotion.capitalize()}: {data['percentage']:.2f}% ({data['motivation']} motivation)\n"
221
- return response
222
 
223
- # S.O.U.L. (Self-Organizing Universal Learning) Function
224
- class SOUL:
225
- def __init__(self, model_name='bigscience/bloom-1b1'):
226
- self.tokenizer = BloomTokenizerFast.from_pretrained(model_name)
227
- self.model = BloomForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
228
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
229
- self.model.to(self.device)
230
-
231
- def generate_text(self, prompt, max_length=100):
232
- inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
233
-
234
- # Generate
235
- with torch.no_grad():
236
- generate_ids = self.model.generate(
237
- inputs.input_ids,
238
- max_length=max_length,
239
- num_return_sequences=1,
240
- no_repeat_ngram_size=2,
241
- do_sample=True,
242
- top_k=50,
243
- top_p=0.95,
244
- temperature=0.7
245
- )
246
-
247
- return self.tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
248
-
249
- def bridge_ai(self, prompt):
250
- # Generate the response using BLOOM
251
- bloom_response = self.generate_text(prompt)
252
 
253
- # Get the emotional response
254
- emotional_response = get_emotional_response(bloom_response)
255
-
256
- return bloom_response, emotional_response
257
 
258
- # Example usage of S.O.U.L. function
259
- soul = SOUL()
 
 
260
 
261
- def interact_with_soul(user_input):
262
- bloom_response, emotional_response = soul.bridge_ai(user_input)
263
- return bloom_response, emotional_response
 
264
 
265
- # Function to handle Gradio interface using multiprocessing
266
- def launch_gradio():
267
- iface = gr.Interface(
268
- fn=interact_with_soul,
269
- inputs="text",
270
- outputs=["text", "text"],
271
- title="S.O.U.L AI",
272
- description="Enter a prompt to interact with the S.O.U.L AI, which will generate a response and provide an emotional analysis."
273
- )
274
- iface.launch()
275
 
276
- # Use multiprocessing to utilize all CPU cores
277
- if __name__ == '__main__':
278
- mp.set_start_method('spawn')
279
- p = mp.Process(target=launch_gradio)
280
- p.start()
281
- p.join()
 
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 GPTJForCausalLM, GPT2TokenizerFast
13
  import torch
14
  import torch.multiprocessing as mp
15
 
 
195
  update_emotion('boredom', 20)
196
  elif predicted_emotion == 'envy':
197
  update_emotion('envy', 20)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
+ # Evolve emotions
 
 
 
200
  evolve_emotions()
 
 
 
 
201
 
202
+ # Save the updated emotion history
203
+ emotion_history.append(emotions.copy())
204
+ save_historical_data(emotion_history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
+ return f"Emotion: {predicted_emotion}, Emotion Details: {emotions[predicted_emotion]}"
 
 
 
207
 
208
+ # Initialize the pre-trained language model
209
+ model_name = 'EleutherAI/gpt-j-6B'
210
+ tokenizer = GPT2TokenizerFast.from_pretrained(model_name)
211
+ lm_model = GPTJForCausalLM.from_pretrained(model_name)
212
 
213
+ # Multiprocessing context setting (ensure it's set only once)
214
+ if __name__ == '__main__':
215
+ if mp.get_start_method(allow_none=True) != 'spawn':
216
+ mp.set_start_method('spawn')
217
 
218
+ # Example usage
219
+ context_input = "I am feeling very joyful today"
220
+ emotional_response = get_emotional_response(context_input)
221
+ print(emotional_response)
 
 
 
 
 
 
222