Cain / app.py
Sephfox's picture
Update app.py
da18a88 verified
raw
history blame
12.9 kB
import warnings
import numpy as np
import pandas as pd
import os
import json
import random
import gradio as gr
import torch
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoModelForCausalLM, pipeline
from deap import base, creator, tools, algorithms
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.chunk import ne_chunk
from textblob import TextBlob
import matplotlib.pyplot as plt
import seaborn as sns
warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')
# Download necessary NLTK data
nltk.download('vader_lexicon', quiet=True)
nltk.download('punkt', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
nltk.download('maxent_ne_chunker', quiet=True)
nltk.download('words', quiet=True)
# Initialize Example Dataset (For Emotion Prediction)
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 (memory-efficient)
encoder = OneHotEncoder(handle_unknown='ignore', sparse=True)
contexts_encoded = encoder.fit_transform(df[['context']])
# Encoding emotions
emotions_target = pd.Categorical(df['emotion']).codes
emotion_classes = pd.Categorical(df['emotion']).categories
# Load pre-trained BERT model for emotion prediction
emotion_prediction_model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
emotion_prediction_tokenizer = AutoTokenizer.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
# Load pre-trained LLM model and tokenizer for response generation with increased context window
response_model_name = "microsoft/DialoGPT-medium"
response_tokenizer = AutoTokenizer.from_pretrained(response_model_name)
response_model = AutoModelForCausalLM.from_pretrained(response_model_name)
# 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},
'neutral': {'percentage': 10, 'motivation': 'balanced', 'intensity': 0},
'wit': {'percentage': 15, 'motivation': 'clever', 'intensity': 0},
'curiosity': {'percentage': 20, 'motivation': 'inquisitive', 'intensity': 0},
}
total_percentage = 200
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):
if percentage > emotions['ideal_state']['percentage']:
percentage = emotions['ideal_state']['percentage']
emotions['ideal_state']['percentage'] -= percentage
emotions[emotion]['percentage'] += percentage
emotions[emotion]['intensity'] = intensity
# Introduce some randomness in emotional evolution
for e in emotions:
if e != emotion and e != 'ideal_state':
change = random.uniform(-2, 2)
emotions[e]['percentage'] = max(0, emotions[e]['percentage'] + change)
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()
# Create FitnessMulti and Individual outside of evolve_emotions
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -0.5, -0.2))
creator.create("Individual", list, fitness=creator.FitnessMulti)
def evaluate(individual):
emotion_values = individual[:len(emotions) - 1]
intensities = individual[len(emotions) - 1:-1]
ideal_state = individual[-1]
ideal_diff = abs(100 - ideal_state)
sum_non_ideal = sum(emotion_values)
intensity_range = max(intensities) - min(intensities)
return ideal_diff, sum_non_ideal, intensity_range
def evolve_emotions():
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) - 1) +
(lambda: 100,), n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
toolbox.register("select", tools.selNSGA2)
toolbox.register("evaluate", evaluate)
population = toolbox.population(n=100)
algorithms.eaMuPlusLambda(population, toolbox, mu=50, lambda_=100, cxpb=0.7, mutpb=0.2, ngen=100,
stats=None, halloffame=None, verbose=False)
best_individual = tools.selBest(population, k=1)[0]
emotion_values = best_individual[:len(emotions) - 1]
intensities = best_individual[len(emotions) - 1:-1]
ideal_state = best_individual[-1]
for i, (emotion, data) in enumerate(list(emotions.items())[:-1]): # Exclude 'ideal_state'
if i < len(emotion_values):
data['percentage'] = emotion_values[i]
if i < len(intensities):
data['intensity'] = intensities[i]
emotions['ideal_state']['percentage'] = ideal_state
def update_emotion_history(emotion, percentage, intensity, context):
entry = {
'emotion': emotion,
'percentage': percentage,
'intensity': intensity,
'context': context,
'timestamp': pd.Timestamp.now().isoformat()
}
emotion_history.append(entry)
save_historical_data(emotion_history)
# Adding 443 features
additional_features = {}
for i in range(443):
additional_features[f'feature_{i+1}'] = 0
def feature_transformations():
global additional_features
for feature in additional_features:
additional_features[feature] += random.uniform(-1, 1)
def generate_response(input_text):
inputs = response_tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
response_ids = response_model.generate(
inputs.input_ids,
max_length=150,
num_return_sequences=1,
no_repeat_ngram_size=2,
top_k=50,
top_p=0.95,
temperature=0.7
)
response = response_tokenizer.decode(response_ids[0], skip_special_tokens=True)
return response
def predict_emotion(context):
inputs = emotion_prediction_tokenizer(context, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
outputs = emotion_prediction_model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class = torch.argmax(probabilities, dim=-1).item()
emotion_labels = ["sadness", "joy", "love", "anger", "fear", "surprise"]
return emotion_labels[predicted_class]
def sentiment_analysis(text):
sia = SentimentIntensityAnalyzer()
sentiment_scores = sia.polarity_scores(text)
return sentiment_scores
def extract_entities(text):
chunked = ne_chunk(pos_tag(word_tokenize(text)))
entities = []
for chunk in chunked:
if hasattr(chunk, 'label'):
entities.append(((' '.join(c[0] for c in chunk)), chunk.label()))
return entities
def analyze_text_complexity(text):
blob = TextBlob(text)
return {
'word_count': len(blob.words),
'sentence_count': len(blob.sentences),
'average_sentence_length': len(blob.words) / len(blob.sentences) if len(blob.sentences) > 0 else 0,
'polarity': blob.sentiment.polarity,
'subjectivity': blob.sentiment.subjectivity
}
def visualize_emotions():
emotions_df = pd.DataFrame([(e, d['percentage'], d['intensity']) for e, d in emotions.items()],
columns=['Emotion', 'Percentage', 'Intensity'])
plt.figure(figsize=(12, 6))
sns.barplot(x='Emotion', y='Percentage', data=emotions_df)
plt.title('Current Emotional State')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.savefig('emotional_state.png')
plt.close()
return 'emotional_state.png'
def interactive_interface(input_text):
try:
evolve_emotions()
predicted_emotion = predict_emotion(input_text)
sentiment_scores = sentiment_analysis(input_text)
entities = extract_entities(input_text)
text_complexity = analyze_text_complexity(input_text)
update_emotion(predicted_emotion, random.uniform(5, 15), random.uniform(0, 10))
update_emotion_history(predicted_emotion, emotions[predicted_emotion]['percentage'], emotions[predicted_emotion]['intensity'], input_text)
feature_transformations()
response = generate_response(input_text)
emotion_visualization = visualize_emotions()
analysis_result = {
'predicted_emotion': predicted_emotion,
'sentiment_scores': sentiment_scores,
'entities': entities,
'text_complexity': text_complexity,
'current_emotional_state': emotions,
'response': response,
'emotion_visualization': emotion_visualization
}
return analysis_result
except Exception as e:
print(f"An error occurred: {str(e)}")
return "I apologize, but I encountered an error while processing your input. Please try again."
def gradio_interface(input_text):
response = interactive_interface(input_text)
if isinstance(response, str):
return response, None
else:
return (
f"Predicted Emotion: {response['predicted_emotion']}\n"
f"Sentiment: {response['sentiment_scores']}\n"
f"Entities: {response['entities']}\n"
f"Text Complexity: {response['text_complexity']}\n"
f"Response: {response['response']}\n",
response['emotion_visualization']
)
# Create Gradio interface
iface = gr.Interface(
fn=gradio_interface,
inputs="text",
outputs=["text", gr.Image(type="filepath")],
title="Enhanced Emotional AI Interface",
description="Enter text to interact with the AI and analyze emotions."
)
if __name__ == "__main__":
iface.launch()