Spaces:
Running
Running
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 | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import OneHotEncoder | |
from sklearn.neural_network import MLPClassifier | |
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 | |
import multiprocessing as mp | |
from joblib import Parallel, delayed | |
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', sparse=False) | |
contexts_encoded = encoder.fit_transform(df[['context']]) | |
# Encoding emotions | |
emotions_target = df['emotion'].astype('category').cat.codes.values | |
emotion_classes = df['emotion'].astype('category').cat.categories.values | |
# 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) | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model = AdvancedNN(input_size, hidden_size, num_classes).to(device) | |
criterion = nn.CrossEntropyLoss() | |
optimizer = optim.Adam(model.parameters(), lr=0.001) | |
train_dataset = TensorDataset(torch.FloatTensor(X_train).to(device), torch.LongTensor(y_train).to(device)) | |
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 | |
rf_model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1) | |
rf_model.fit(X_train, y_train) | |
# Isolation Forest Anomaly Detection Model | |
isolation_forest = IsolationForest(contamination=0.1, random_state=42, n_jobs=-1) | |
isolation_forest.fit(X_train) | |
# 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() | |
# Parallel genetic algorithm for emotion evolution | |
def parallel_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)) | |
def evolve_emotions(): | |
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) | |
pool = mp.Pool() | |
toolbox.register("map", pool.map) | |
toolbox.register("evaluate", parallel_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) | |
pool.close() | |
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).to(device) | |
def generate_text(prompt, max_length=150): | |
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device) | |
with torch.no_grad(): | |
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, device=0 if torch.cuda.is_available() else -1) | |
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]]) | |
# Neural network prediction | |
with torch.no_grad(): | |
nn_output = model(torch.FloatTensor(context_encoded).to(device)) | |
nn_prediction = nn_output.argmax(1).item() | |
# Random Forest prediction | |
rf_prediction = rf_model.predict(context_encoded)[0] | |
# Weighted ensemble | |
ensemble_prediction = (0.6 * nn_prediction + 0.4 * rf_prediction) | |
predicted_emotion = emotion_classes[int(round(ensemble_prediction))] | |
# Anomaly detection | |
anomaly_score = isolation_forest.decision_function(context_encoded) | |
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() | |
intensity = (nn_proba + rf_proba) / 2 * 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() |