File size: 11,516 Bytes
52b8a27
a403423
 
 
 
 
4b172ec
e6344ec
 
a403423
 
e6344ec
a403423
e6344ec
a403423
e6344ec
 
 
4b172ec
52b8a27
 
a403423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6344ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a403423
e6344ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a403423
 
e6344ec
a403423
 
 
e6344ec
a403423
e6344ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a403423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6344ec
 
a403423
 
e6344ec
a403423
 
 
 
 
 
 
 
e6344ec
a403423
 
e6344ec
 
 
 
 
 
a403423
e6344ec
 
4b172ec
a403423
e6344ec
 
 
 
 
 
 
 
4b172ec
a403423
e6344ec
 
 
4b172ec
e6344ec
 
 
 
4b172ec
a403423
e6344ec
a403423
e6344ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a403423
 
 
 
e6344ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a403423
e6344ec
 
 
 
 
bc4f40b
e6344ec
a403423
4b172ec
6ea58c0
 
a403423
e6344ec
 
 
 
 
 
 
a403423
e6344ec
 
 
 
 
 
a403423
e6344ec
 
 
 
 
 
 
 
a403423
e6344ec
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import warnings
import numpy as np
import pandas as pd
import os
import json
import random
import gradio as gr
from sklearn.ensemble import IsolationForest, RandomForestClassifier, GradientBoostingClassifier
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import OneHotEncoder
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from deap import base, creator, tools, algorithms
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset

warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')

# Initialize Example Emotions Dataset
data = {
    'context': [
        'I am happy', 'I am sad', 'I am angry', 'I am excited', 'I am calm',
        'I am feeling joyful', 'I am grieving', 'I am feeling peaceful', 'I am frustrated',
        'I am determined', 'I feel resentment', 'I am feeling glorious', 'I am motivated',
        'I am surprised', 'I am fearful', 'I am trusting', 'I feel disgust', 'I am optimistic',
        'I am pessimistic', 'I feel bored', 'I am envious'
    ],
    'emotion': [
        'joy', 'sadness', 'anger', 'joy', 'calmness', 'joy', 'grief', 'calmness', 'anger',
        'determination', 'resentment', 'glory', 'motivation', 'surprise', 'fear', 'trust',
        'disgust', 'optimism', 'pessimism', 'boredom', 'envy'
    ]
}
df = pd.DataFrame(data)

# Encoding the contexts using One-Hot Encoding
encoder = OneHotEncoder(handle_unknown='ignore')
contexts_encoded = encoder.fit_transform(df[['context']]).toarray()

# Encoding emotions
emotions_target = df['emotion'].astype('category').cat.codes
emotion_classes = df['emotion'].astype('category').cat.categories

# Advanced Neural Network with PyTorch
class AdvancedNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(AdvancedNN, self).__init__()
        self.layer1 = nn.Linear(input_size, hidden_size)
        self.layer2 = nn.Linear(hidden_size, hidden_size)
        self.layer3 = nn.Linear(hidden_size, num_classes)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(0.2)
        
    def forward(self, x):
        x = self.relu(self.layer1(x))
        x = self.dropout(x)
        x = self.relu(self.layer2(x))
        x = self.dropout(x)
        x = self.layer3(x)
        return x

# Train Advanced Neural Network
X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
input_size = X_train.shape[1]
hidden_size = 64
num_classes = len(emotion_classes)

model = AdvancedNN(input_size, hidden_size, num_classes)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

train_dataset = TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train))
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)

num_epochs = 100
for epoch in range(num_epochs):
    for batch_X, batch_y in train_loader:
        outputs = model(batch_X)
        loss = criterion(outputs, batch_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

# Ensemble with Random Forest and Gradient Boosting
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
gb_model = GradientBoostingClassifier(n_estimators=100, random_state=42)

rf_model.fit(X_train, y_train)
gb_model.fit(X_train, y_train)

# Isolation Forest Anomaly Detection Model
historical_data = np.array([model(torch.FloatTensor(contexts_encoded)).argmax(1).numpy()]).T
isolation_forest = IsolationForest(contamination=0.1, random_state=42)
isolation_forest.fit(historical_data)

# Enhanced Emotional States
emotions = {
    'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
    'pleasure': {'percentage': 10, 'motivation': 'selfish', 'intensity': 0},
    'sadness': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
    'grief': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
    'anger': {'percentage': 10, 'motivation': 'traumatic or strong', 'intensity': 0},
    'calmness': {'percentage': 10, 'motivation': 'neutral', 'intensity': 0},
    'determination': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
    'resentment': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
    'glory': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
    'motivation': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
    'ideal_state': {'percentage': 100, 'motivation': 'balanced', 'intensity': 0},
    'fear': {'percentage': 10, 'motivation': 'defensive', 'intensity': 0},
    'surprise': {'percentage': 10, 'motivation': 'unexpected', 'intensity': 0},
    'anticipation': {'percentage': 10, 'motivation': 'predictive', 'intensity': 0},
    'trust': {'percentage': 10, 'motivation': 'reliable', 'intensity': 0},
    'disgust': {'percentage': 10, 'motivation': 'repulsive', 'intensity': 0},
    'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
    'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
    'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
    'envy': {'percentage': 10, 'motivation': 'jealous', 'intensity': 0}
}

total_percentage = 200
default_percentage = total_percentage / len(emotions)
for emotion in emotions:
    emotions[emotion]['percentage'] = default_percentage

emotion_history_file = 'emotion_history.json'

def load_historical_data(file_path=emotion_history_file):
    if os.path.exists(file_path):
        with open(file_path, 'r') as file:
            return json.load(file)
    return []

def save_historical_data(historical_data, file_path=emotion_history_file):
    with open(file_path, 'w') as file:
        json.dump(historical_data, file)

emotion_history = load_historical_data
def update_emotion(emotion, percentage, intensity):
    emotions['ideal_state']['percentage'] -= percentage
    emotions[emotion]['percentage'] += percentage
    emotions[emotion]['intensity'] = intensity

    total_current = sum(e['percentage'] for e in emotions.values())
    adjustment = total_percentage - total_current
    emotions['ideal_state']['percentage'] += adjustment

def normalize_context(context):
    return context.lower().strip()

# Advanced Genetic Algorithm for Emotion Evolution
def evolve_emotions():
    def evaluate(individual):
        ideal_state = individual[-1]
        other_emotions = individual[:-1]
        intensities = individual[-21:-1]
        return (abs(ideal_state - 100), 
                sum(other_emotions), 
                max(intensities) - min(intensities))

    creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -1.0, -1.0))
    creator.create("Individual", list, fitness=creator.FitnessMulti)

    toolbox = base.Toolbox()
    toolbox.register("attr_float", random.uniform, 0, 20)
    toolbox.register("attr_intensity", random.uniform, 0, 10)
    toolbox.register("individual", tools.initCycle, creator.Individual,
                     (toolbox.attr_float,) * (len(emotions) - 1) + 
                     (toolbox.attr_intensity,) * len(emotions) +
                     (lambda: random.uniform(80, 120),),
                     n=1)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    toolbox.register("evaluate", evaluate)
    toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=0, up=120, eta=20.0)
    toolbox.register("mutate", tools.mutPolynomialBounded, low=0, up=120, eta=20.0, indpb=0.1)
    toolbox.register("select", tools.selNSGA2)

    population = toolbox.population(n=100)
    
    algorithms.eaMuPlusLambda(population, toolbox, mu=100, lambda_=100, 
                              cxpb=0.7, mutpb=0.3, ngen=50, verbose=False)

    best_individual = tools.selBest(population, k=1)[0]
    
    for idx, emotion in enumerate(emotions.keys()):
        if idx < len(emotions) - 1:
            emotions[emotion]['percentage'] = best_individual[idx]
            emotions[emotion]['intensity'] = best_individual[idx + len(emotions) - 1]
        else:
            emotions[emotion]['percentage'] = best_individual[-1]

# Initialize the pre-trained language model (BLOOM-1b7)
model_name = 'bigscience/bloom-1b7'
tokenizer = AutoTokenizer.from_pretrained(model_name)
lm_model = AutoModelForCausalLM.from_pretrained(model_name)

def generate_text(prompt, max_length=150):
    input_ids = tokenizer.encode(prompt, return_tensors='pt')
    output = lm_model.generate(
        input_ids, 
        max_length=max_length, 
        num_return_sequences=1, 
        no_repeat_ngram_size=2,
        do_sample=True,
        top_k=50,
        top_p=0.95,
        temperature=0.7
    )
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
    return generated_text

sentiment_pipeline = pipeline("sentiment-analysis", model=model_name, tokenizer=tokenizer)

def get_sentiment(text):
    result = sentiment_pipeline(text)[0]
    return f"Sentiment: {result['label']}, Score: {result['score']:.4f}"

def get_emotional_response(context):
    context = normalize_context(context)
    context_encoded = encoder.transform([[context]]).toarray()
    
    # Advanced NN prediction
    nn_output = model(torch.FloatTensor(context_encoded))
    nn_prediction = nn_output.argmax(1).item()
    
    # Ensemble predictions
    rf_prediction = rf_model.predict(context_encoded)[0]
    gb_prediction = gb_model.predict(context_encoded)[0]
    
    # Weighted ensemble
    ensemble_prediction = (0.4 * nn_prediction + 0.3 * rf_prediction + 0.3 * gb_prediction)
    predicted_emotion = emotion_classes[int(round(ensemble_prediction))]

    # Anomaly detection
    anomaly_score = isolation_forest.decision_function(np.array([[nn_prediction]]))
    is_anomaly = anomaly_score < 0

    # Calculate emotion intensity based on model confidence
    nn_proba = torch.softmax(nn_output, dim=1).max().item()
    rf_proba = rf_model.predict_proba(context_encoded).max()
    gb_proba = gb_model.predict_proba(context_encoded).max()
    intensity = (nn_proba + rf_proba + gb_proba) / 3 * 10  # Scale to 0-10

    update_emotion(predicted_emotion, 20, intensity)
    evolve_emotions()

    emotion_history.append(emotions.copy())
    save_historical_data(emotion_history)

    response = f"Predicted Emotion: {predicted_emotion}\n"
    response += f"Emotion Details: {emotions[predicted_emotion]}\n"
    response += f"Anomaly Detected: {'Yes' if is_anomaly else 'No'}\n"
    response += f"Emotion Intensity: {intensity:.2f}/10\n"
    response += f"Current Emotional State: {json.dumps(emotions, indent=2)}"

    return response

def process_input(input_text):
    emotional_response = get_emotional_response(input_text)
    sentiment = get_sentiment(input_text)
    generated_text = generate_text(f"Based on the emotion analysis: {emotional_response}\nGenerate a response: {input_text}")
    
    return f"{emotional_response}\n\nSentiment Analysis: {sentiment}\n\nGenerated Response: {generated_text}"

# Gradio Interface
iface = gr.Interface(
    fn=process_input,
    inputs="text",
    outputs="text",
    title="Advanced Emotion Analysis and Text Generation with BLOOM-1b7",
    description="Enter a sentence for comprehensive emotion analysis, sentiment analysis, and context-aware text generation using advanced AI models."
)

if __name__ == "__main__":
    iface.launch()