Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,17 +5,18 @@ import os
|
|
5 |
import json
|
6 |
import random
|
7 |
import gradio as gr
|
8 |
-
from sklearn.ensemble import IsolationForest, RandomForestClassifier
|
9 |
-
from sklearn.model_selection import train_test_split
|
10 |
from sklearn.preprocessing import OneHotEncoder
|
11 |
from sklearn.neural_network import MLPClassifier
|
12 |
-
from sklearn.metrics import accuracy_score
|
13 |
from deap import base, creator, tools, algorithms
|
14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
15 |
import torch
|
16 |
import torch.nn as nn
|
17 |
import torch.optim as optim
|
18 |
from torch.utils.data import DataLoader, TensorDataset
|
|
|
|
|
19 |
|
20 |
warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')
|
21 |
|
@@ -37,12 +38,12 @@ data = {
|
|
37 |
df = pd.DataFrame(data)
|
38 |
|
39 |
# Encoding the contexts using One-Hot Encoding
|
40 |
-
encoder = OneHotEncoder(handle_unknown='ignore')
|
41 |
-
contexts_encoded = encoder.fit_transform(df[['context']])
|
42 |
|
43 |
# Encoding emotions
|
44 |
-
emotions_target = df['emotion'].astype('category').cat.codes
|
45 |
-
emotion_classes = df['emotion'].astype('category').cat.categories
|
46 |
|
47 |
# Advanced Neural Network with PyTorch
|
48 |
class AdvancedNN(nn.Module):
|
@@ -64,44 +65,34 @@ class AdvancedNN(nn.Module):
|
|
64 |
|
65 |
# Train Advanced Neural Network
|
66 |
X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
|
67 |
-
|
68 |
-
# Convert to dense array if it's a sparse matrix, otherwise leave as is
|
69 |
-
X_train = X_train.toarray() if hasattr(X_train, 'toarray') else X_train
|
70 |
-
X_test = X_test.toarray() if hasattr(X_test, 'toarray') else X_test
|
71 |
-
|
72 |
-
# Ensure y_train and y_test are numpy arrays
|
73 |
-
y_train = y_train.to_numpy() if hasattr(y_train, 'to_numpy') else np.array(y_train)
|
74 |
-
y_test = y_test.to_numpy() if hasattr(y_test, 'to_numpy') else np.array(y_test)
|
75 |
-
|
76 |
input_size = X_train.shape[1]
|
77 |
hidden_size = 64
|
78 |
num_classes = len(emotion_classes)
|
79 |
|
80 |
-
|
|
|
81 |
criterion = nn.CrossEntropyLoss()
|
82 |
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
83 |
|
84 |
-
train_dataset = TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train))
|
85 |
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
rf_model.fit(X_train, y_train)
|
99 |
-
gb_model.fit(X_train, y_train)
|
100 |
|
101 |
# Isolation Forest Anomaly Detection Model
|
102 |
-
|
103 |
-
isolation_forest
|
104 |
-
isolation_forest.fit(historical_data)
|
105 |
|
106 |
# Enhanced Emotional States
|
107 |
emotions = {
|
@@ -144,7 +135,8 @@ def save_historical_data(historical_data, file_path=emotion_history_file):
|
|
144 |
with open(file_path, 'w') as file:
|
145 |
json.dump(historical_data, file)
|
146 |
|
147 |
-
emotion_history = load_historical_data
|
|
|
148 |
def update_emotion(emotion, percentage, intensity):
|
149 |
emotions['ideal_state']['percentage'] -= percentage
|
150 |
emotions[emotion]['percentage'] += percentage
|
@@ -157,16 +149,16 @@ def update_emotion(emotion, percentage, intensity):
|
|
157 |
def normalize_context(context):
|
158 |
return context.lower().strip()
|
159 |
|
160 |
-
#
|
161 |
-
def
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
max(intensities) - min(intensities))
|
169 |
|
|
|
170 |
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0, -1.0))
|
171 |
creator.create("Individual", list, fitness=creator.FitnessMulti)
|
172 |
|
@@ -180,7 +172,9 @@ def evolve_emotions():
|
|
180 |
n=1)
|
181 |
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
|
182 |
|
183 |
-
|
|
|
|
|
184 |
toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=0, up=120, eta=20.0)
|
185 |
toolbox.register("mutate", tools.mutPolynomialBounded, low=0, up=120, eta=20.0, indpb=0.1)
|
186 |
toolbox.register("select", tools.selNSGA2)
|
@@ -190,6 +184,8 @@ def evolve_emotions():
|
|
190 |
algorithms.eaMuPlusLambda(population, toolbox, mu=100, lambda_=100,
|
191 |
cxpb=0.7, mutpb=0.3, ngen=50, verbose=False)
|
192 |
|
|
|
|
|
193 |
best_individual = tools.selBest(population, k=1)[0]
|
194 |
|
195 |
for idx, emotion in enumerate(emotions.keys()):
|
@@ -202,24 +198,25 @@ def evolve_emotions():
|
|
202 |
# Initialize the pre-trained language model (BLOOM-1b7)
|
203 |
model_name = 'bigscience/bloom-1b7'
|
204 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
205 |
-
lm_model = AutoModelForCausalLM.from_pretrained(model_name)
|
206 |
|
207 |
def generate_text(prompt, max_length=150):
|
208 |
-
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
219 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
220 |
return generated_text
|
221 |
|
222 |
-
sentiment_pipeline = pipeline("sentiment-analysis", model=model_name, tokenizer=tokenizer)
|
223 |
|
224 |
def get_sentiment(text):
|
225 |
result = sentiment_pipeline(text)[0]
|
@@ -227,29 +224,28 @@ def get_sentiment(text):
|
|
227 |
|
228 |
def get_emotional_response(context):
|
229 |
context = normalize_context(context)
|
230 |
-
context_encoded = encoder.transform([[context]])
|
231 |
|
232 |
-
#
|
233 |
-
|
234 |
-
|
|
|
235 |
|
236 |
-
#
|
237 |
rf_prediction = rf_model.predict(context_encoded)[0]
|
238 |
-
gb_prediction = gb_model.predict(context_encoded)[0]
|
239 |
|
240 |
# Weighted ensemble
|
241 |
-
ensemble_prediction = (0.
|
242 |
predicted_emotion = emotion_classes[int(round(ensemble_prediction))]
|
243 |
|
244 |
# Anomaly detection
|
245 |
-
anomaly_score = isolation_forest.decision_function(
|
246 |
is_anomaly = anomaly_score < 0
|
247 |
|
248 |
# Calculate emotion intensity based on model confidence
|
249 |
nn_proba = torch.softmax(nn_output, dim=1).max().item()
|
250 |
rf_proba = rf_model.predict_proba(context_encoded).max()
|
251 |
-
|
252 |
-
intensity = (nn_proba + rf_proba + gb_proba) / 3 * 10 # Scale to 0-10
|
253 |
|
254 |
update_emotion(predicted_emotion, 20, intensity)
|
255 |
evolve_emotions()
|
|
|
5 |
import json
|
6 |
import random
|
7 |
import gradio as gr
|
8 |
+
from sklearn.ensemble import IsolationForest, RandomForestClassifier
|
9 |
+
from sklearn.model_selection import train_test_split
|
10 |
from sklearn.preprocessing import OneHotEncoder
|
11 |
from sklearn.neural_network import MLPClassifier
|
|
|
12 |
from deap import base, creator, tools, algorithms
|
13 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
14 |
import torch
|
15 |
import torch.nn as nn
|
16 |
import torch.optim as optim
|
17 |
from torch.utils.data import DataLoader, TensorDataset
|
18 |
+
import multiprocessing as mp
|
19 |
+
from joblib import Parallel, delayed
|
20 |
|
21 |
warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')
|
22 |
|
|
|
38 |
df = pd.DataFrame(data)
|
39 |
|
40 |
# Encoding the contexts using One-Hot Encoding
|
41 |
+
encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
|
42 |
+
contexts_encoded = encoder.fit_transform(df[['context']])
|
43 |
|
44 |
# Encoding emotions
|
45 |
+
emotions_target = df['emotion'].astype('category').cat.codes.values
|
46 |
+
emotion_classes = df['emotion'].astype('category').cat.categories.values
|
47 |
|
48 |
# Advanced Neural Network with PyTorch
|
49 |
class AdvancedNN(nn.Module):
|
|
|
65 |
|
66 |
# Train Advanced Neural Network
|
67 |
X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
input_size = X_train.shape[1]
|
69 |
hidden_size = 64
|
70 |
num_classes = len(emotion_classes)
|
71 |
|
72 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
73 |
+
model = AdvancedNN(input_size, hidden_size, num_classes).to(device)
|
74 |
criterion = nn.CrossEntropyLoss()
|
75 |
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
76 |
|
77 |
+
train_dataset = TensorDataset(torch.FloatTensor(X_train).to(device), torch.LongTensor(y_train).to(device))
|
78 |
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
|
79 |
|
80 |
+
num_epochs = 100
|
81 |
+
for epoch in range(num_epochs):
|
82 |
+
for batch_X, batch_y in train_loader:
|
83 |
+
outputs = model(batch_X)
|
84 |
+
loss = criterion(outputs, batch_y)
|
85 |
+
optimizer.zero_grad()
|
86 |
+
loss.backward()
|
87 |
+
optimizer.step()
|
88 |
+
|
89 |
+
# Ensemble with Random Forest
|
90 |
+
rf_model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
|
91 |
rf_model.fit(X_train, y_train)
|
|
|
92 |
|
93 |
# Isolation Forest Anomaly Detection Model
|
94 |
+
isolation_forest = IsolationForest(contamination=0.1, random_state=42, n_jobs=-1)
|
95 |
+
isolation_forest.fit(X_train)
|
|
|
96 |
|
97 |
# Enhanced Emotional States
|
98 |
emotions = {
|
|
|
135 |
with open(file_path, 'w') as file:
|
136 |
json.dump(historical_data, file)
|
137 |
|
138 |
+
emotion_history = load_historical_data()
|
139 |
+
|
140 |
def update_emotion(emotion, percentage, intensity):
|
141 |
emotions['ideal_state']['percentage'] -= percentage
|
142 |
emotions[emotion]['percentage'] += percentage
|
|
|
149 |
def normalize_context(context):
|
150 |
return context.lower().strip()
|
151 |
|
152 |
+
# Parallel genetic algorithm for emotion evolution
|
153 |
+
def parallel_evaluate(individual):
|
154 |
+
ideal_state = individual[-1]
|
155 |
+
other_emotions = individual[:-1]
|
156 |
+
intensities = individual[-21:-1]
|
157 |
+
return (abs(ideal_state - 100),
|
158 |
+
sum(other_emotions),
|
159 |
+
max(intensities) - min(intensities))
|
|
|
160 |
|
161 |
+
def evolve_emotions():
|
162 |
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0, -1.0))
|
163 |
creator.create("Individual", list, fitness=creator.FitnessMulti)
|
164 |
|
|
|
172 |
n=1)
|
173 |
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
|
174 |
|
175 |
+
pool = mp.Pool()
|
176 |
+
toolbox.register("map", pool.map)
|
177 |
+
toolbox.register("evaluate", parallel_evaluate)
|
178 |
toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=0, up=120, eta=20.0)
|
179 |
toolbox.register("mutate", tools.mutPolynomialBounded, low=0, up=120, eta=20.0, indpb=0.1)
|
180 |
toolbox.register("select", tools.selNSGA2)
|
|
|
184 |
algorithms.eaMuPlusLambda(population, toolbox, mu=100, lambda_=100,
|
185 |
cxpb=0.7, mutpb=0.3, ngen=50, verbose=False)
|
186 |
|
187 |
+
pool.close()
|
188 |
+
|
189 |
best_individual = tools.selBest(population, k=1)[0]
|
190 |
|
191 |
for idx, emotion in enumerate(emotions.keys()):
|
|
|
198 |
# Initialize the pre-trained language model (BLOOM-1b7)
|
199 |
model_name = 'bigscience/bloom-1b7'
|
200 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
201 |
+
lm_model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
202 |
|
203 |
def generate_text(prompt, max_length=150):
|
204 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
|
205 |
+
with torch.no_grad():
|
206 |
+
output = lm_model.generate(
|
207 |
+
input_ids,
|
208 |
+
max_length=max_length,
|
209 |
+
num_return_sequences=1,
|
210 |
+
no_repeat_ngram_size=2,
|
211 |
+
do_sample=True,
|
212 |
+
top_k=50,
|
213 |
+
top_p=0.95,
|
214 |
+
temperature=0.7
|
215 |
+
)
|
216 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
217 |
return generated_text
|
218 |
|
219 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model=model_name, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
220 |
|
221 |
def get_sentiment(text):
|
222 |
result = sentiment_pipeline(text)[0]
|
|
|
224 |
|
225 |
def get_emotional_response(context):
|
226 |
context = normalize_context(context)
|
227 |
+
context_encoded = encoder.transform([[context]])
|
228 |
|
229 |
+
# Neural network prediction
|
230 |
+
with torch.no_grad():
|
231 |
+
nn_output = model(torch.FloatTensor(context_encoded).to(device))
|
232 |
+
nn_prediction = nn_output.argmax(1).item()
|
233 |
|
234 |
+
# Random Forest prediction
|
235 |
rf_prediction = rf_model.predict(context_encoded)[0]
|
|
|
236 |
|
237 |
# Weighted ensemble
|
238 |
+
ensemble_prediction = (0.6 * nn_prediction + 0.4 * rf_prediction)
|
239 |
predicted_emotion = emotion_classes[int(round(ensemble_prediction))]
|
240 |
|
241 |
# Anomaly detection
|
242 |
+
anomaly_score = isolation_forest.decision_function(context_encoded)
|
243 |
is_anomaly = anomaly_score < 0
|
244 |
|
245 |
# Calculate emotion intensity based on model confidence
|
246 |
nn_proba = torch.softmax(nn_output, dim=1).max().item()
|
247 |
rf_proba = rf_model.predict_proba(context_encoded).max()
|
248 |
+
intensity = (nn_proba + rf_proba) / 2 * 10 # Scale to 0-10
|
|
|
249 |
|
250 |
update_emotion(predicted_emotion, 20, intensity)
|
251 |
evolve_emotions()
|