Cain / app.py
Sephfox's picture
Update app.py
c852ed8 verified
raw
history blame
13.1 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.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 overjoyed', 'I am deeply saddened', 'I am seething with rage', 'I am exhilarated', 'I am tranquil',
'I am brimming with joy', 'I am grieving profoundly', 'I am at peace', 'I am frustrated beyond measure',
'I am determined to succeed', 'I feel resentment burning within me', 'I am feeling glorious and triumphant',
'I am motivated and inspired', 'I am utterly surprised', 'I am gripped by fear', 'I am trusting and open',
'I feel a sense of disgust', 'I am optimistic and hopeful', 'I am pessimistic and gloomy', 'I feel bored and listless',
'I am envious and jealous'
],
'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)
try:
encoder = OneHotEncoder(handle_unknown='ignore', sparse_output=True)
except TypeError:
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 large language model and tokenizer for response generation with increased context window
response_model_name = "gpt2-xl"
response_tokenizer = AutoTokenizer.from_pretrained(response_model_name)
response_model = AutoModelForCausalLM.from_pretrained(response_model_name)
# Set the pad token
response_tokenizer.pad_token = response_tokenizer.eos_token
# Enhanced Emotional States
emotions = {
'joy': {'percentage': 10, 'motivation': 'positive and uplifting', 'intensity': 0},
'sadness': {'percentage': 10, 'motivation': 'reflective and introspective', 'intensity': 0},
'anger': {'percentage': 10, 'motivation': 'passionate and driven', 'intensity': 0},
'fear': {'percentage': 10, 'motivation': 'cautious and protective', 'intensity': 0},
'love': {'percentage': 10, 'motivation': 'affectionate and caring', 'intensity': 0},
'surprise': {'percentage': 10, 'motivation': 'curious and intrigued', 'intensity': 0},
'neutral': {'percentage': 40, 'motivation': 'balanced and composed', 'intensity': 0},
}
total_percentage = 100
emotion_history_file = 'emotion_history.json'
global conversation_history
conversation_history = []
max_history_length = 1000 # Increase the maximum history length
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[emotion]['percentage'] += percentage
emotions[emotion]['intensity'] = intensity
# Normalize percentages
total = sum(e['percentage'] for e in emotions.values())
for e in emotions:
emotions[e]['percentage'] = (emotions[e]['percentage'] / total) * 100
def normalize_context(context):
return context.lower().strip()
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)]
intensities = individual[len(emotions):]
total_diff = abs(100 - sum(emotion_values))
intensity_range = max(intensities) - min(intensities)
emotion_balance = max(emotion_values) - min(emotion_values)
return total_diff, intensity_range, emotion_balance
def evolve_emotions():
toolbox = base.Toolbox()
toolbox.register("attr_float", random.uniform, 0, 100)
toolbox.register("attr_intensity", random.uniform, 0, 10)
toolbox.register("individual", tools.initCycle, creator.Individual,
(toolbox.attr_float,) * len(emotions) +
(toolbox.attr_intensity,) * len(emotions), 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=50,
stats=None, halloffame=None, verbose=False)
best_individual = tools.selBest(population, k=1)[0]
emotion_values = best_individual[:len(emotions)]
intensities = best_individual[len(emotions):]
for i, (emotion, data) in enumerate(emotions.items()):
data['percentage'] = emotion_values[i]
data['intensity'] = intensities[i]
# Normalize percentages
total = sum(e['percentage'] for e in emotions.values())
for e in emotions:
emotions[e]['percentage'] = (emotions[e]['percentage'] / total) * 100
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 get_ai_emotion(input_text):
predicted_emotion = predict_emotion(input_text)
ai_emotion = predicted_emotion
ai_emotion_percentage = emotions[predicted_emotion]['percentage']
ai_emotion_intensity = emotions[predicted_emotion]['intensity']
return ai_emotion, ai_emotion_percentage, ai_emotion_intensity
def generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion_intensity):
# Generate an emotion visualization based on the AI's emotional state
# This could involve creating an image or a visualization using Matplotlib/Seaborn
# The generated image should be saved and returned as the output
emotion_visualization_path = 'emotional_state.png'
# Generate and save the emotion visualization
return emotion_visualization_path
def generate_response(input_text, ai_emotion, conversation_history):
# Prepare a prompt based on the current emotion and input
prompt = f"As an AI assistant, I am currently feeling {ai_emotion}. My response will reflect this emotional state. Human: {input_text}\nAI:"
# Add conversation history to the prompt
for entry in conversation_history[-100:]: # Use last 100 entries for context
prompt = f"Human: {entry['user']}\nAI: {entry['response']}\n" + prompt
inputs = response_tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=8192)
# Adjust generation parameters based on emotion
temperature = 0.7
if ai_emotion == 'anger':
temperature = 0.9 # More randomness for angry responses
elif ai_emotion == 'joy':
temperature = 0.5 # More focused responses for joyful state
with torch.no_grad():
response_ids = response_model.generate(
inputs.input_ids,
attention_mask=inputs.attention_mask,
max_length=8192,
num_return_sequences=1,
no_repeat_ngram_size=2,
do_sample=True,
top_k=50,
top_p=0.95,
temperature=temperature,
pad_token_id=response_tokenizer.eos_token_id
)
response = response_tokenizer.decode(response_ids[0], skip_special_tokens=True)
# Extract only the AI's response
response = response.split("AI:")[-1].strip()
return response
def interactive_interface(input_text):
# Perform your processing logic here
predicted_emotion = predict_emotion(input_text)
sentiment_scores = sentiment_analysis(input_text)
entities = extract_entities(input_text)
text_complexity = analyze_text_complexity(input_text)
ai_emotion, ai_emotion_percentage, ai_emotion_intensity = get_ai_emotion(input_text)
emotion_visualization = generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion_intensity)
response = generate_response(input_text, ai_emotion, conversation_history)
# Update conversation history
conversation_history.append({'user': input_text, 'response': response})
if len(conversation_history) > max_history_length:
conversation_history.pop(0)
# Return the expected outputs in the correct order
return (
gr.Textbox(predicted_emotion),
gr.Textbox(str(sentiment_scores)),
gr.Textbox(str(entities)),
gr.Textbox(str(text_complexity)),
gr.Textbox(ai_emotion),
gr.Textbox(str(ai_emotion_percentage)),
gr.Textbox(str(ai_emotion_intensity)),
gr.Image(emotion_visualization),
gr.Textbox(response)
)
# 443 additional 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 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('Emotion Percentages')
plt.xlabel('Emotion')
plt.ylabel('Percentage')
plt.xticks(rotation=90)
plt.savefig('emotion_percentages.png')
plt.figure(figsize=(12, 6))
sns.barplot(x='emotion', y='intensity', data=emotions_df)
plt.title('Emotion Intensities')
plt.xlabel('Emotion')
plt.ylabel('Intensity')
plt.xticks(rotation=90)
plt.savefig('emotion_intensities.png')
return 'emotion_percentages.png', 'emotion_intensities.png'
# Create the Gradio interface
iface = gr.Interface(
fn=interactive_interface,
inputs=gr.Textbox(label="Input Text"),
outputs=[
gr.Textbox(label="Predicted Emotion"),
gr.Textbox(label="Sentiment Scores"),
gr.Textbox(label="Extracted Entities"),
gr.Textbox(label="Text Complexity"),
gr.Textbox(label="AI Emotion"),
gr.Textbox(label="AI Emotion Percentage"),
gr.Textbox(label="AI Emotion Intensity"),
gr.Image(label="Emotion Visualization"),
gr.Textbox(label="AI Response")
],
title="Emotional AI Assistant",
description="An AI assistant that can analyze the emotional content of text and generate responses based on its emotional state.",
)
iface.launch()