Spaces:
Running
Running
File size: 11,829 Bytes
5b50796 26bca4f 5b50796 3ac5a66 20e25d2 5b50796 88945ca 5b50796 aaf362d 0241af2 5b50796 33e1c48 88945ca 60ae9bc 88945ca 33e1c48 88945ca 5b50796 449f6f5 5b50796 20e25d2 5b50796 ff1fffe 5b50796 ff1fffe 5b50796 ff6946f a403423 a6dbe30 e6344ec a403423 e6344ec a403423 12380c1 60ae9bc a6dbe30 12380c1 e6344ec 4b172ec a403423 e6344ec 12380c1 33e1c48 6148fb4 ff1fffe 449f6f5 20e25d2 e6344ec 53f69b8 60ae9bc 4b172ec a403423 60ae9bc 53f69b8 aaf362d 26bca4f aaf362d efc1ae9 0241af2 ff1fffe 0241af2 ff1fffe 0241af2 ff1fffe 0241af2 aaf362d 27a0681 0f6ffbd 27a0681 33e1c48 27a0681 33e1c48 27a0681 33e1c48 12380c1 0f6ffbd 12380c1 33e1c48 12380c1 33e1c48 0f6ffbd 12380c1 33e1c48 0f6ffbd 33e1c48 b7412b9 ff1fffe 0241af2 ff1fffe 12380c1 ff1fffe 12380c1 ff1fffe 12380c1 ff1fffe ff63280 ff1fffe 12380c1 ff1fffe 12380c1 0241af2 33e1c48 0241af2 12380c1 0241af2 |
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 279 280 281 |
import warnings
import numpy as np
import pandas as pd
import os
import json
import random
import gradio as gr
import torch
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from transformers import AutoModelForSequenceClassification, AutoTokenizer, T5ForConditionalGeneration, T5Tokenizer, pipeline
from deap import base, creator, tools, algorithms
import gc
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 (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")
# Lazy loading for the fine-tuned language model (FLAN-T5)
_finetuned_lm_tokenizer = None
_finetuned_lm_model = None
def get_finetuned_lm_model():
global _finetuned_lm_tokenizer, _finetuned_lm_model
if _finetuned_lm_tokenizer is None or _finetuned_lm_model is None:
_finetuned_lm_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
_finetuned_lm_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base", device_map="auto", low_cpu_mem_usage=True)
return _finetuned_lm_tokenizer, _finetuned_lm_model
# 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}
}
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):
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()
def evaluate(individual):
emotion_values = individual[:len(emotions) - 1]
intensities = individual[-21:-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():
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -0.5, -0.2))
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: 100,), n(toolbox.attr_intensity,) * len(emotions) +
(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[-21:-1]
ideal_state = best_individual[-1]
for i, emotion in enumerate(emotions):
emotions[emotion]['percentage'] = emotion_values[i]
emotions[emotion]['intensity'] = intensities[i]
emotions['ideal_state']['percentage'] = ideal_state
def predict_emotion(context):
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer, top_k=None)
predictions = emotion_prediction_pipeline(context)
emotion_scores = {prediction['label']: prediction['score'] for prediction in predictions[0]}
predicted_emotion = max(emotion_scores, key=emotion_scores.get)
# Map the predicted emotion to our emotion categories
emotion_mapping = {
'sadness': 'sadness',
'joy': 'joy',
'love': 'pleasure',
'anger': 'anger',
'fear': 'fear',
'surprise': 'surprise'
}
return emotion_mapping.get(predicted_emotion, 'neutral')
def generate_text(prompt, chat_history, emotion=None, max_length=100):
finetuned_lm_tokenizer, finetuned_lm_model = get_finetuned_lm_model()
# Prepare the input by concatenating the chat history and the new prompt
full_prompt = "You are Adam, an AI assistant. Respond to the following conversation in a natural and engaging way, considering the emotion: " + emotion + "\n\n"
for turn in chat_history[-5:]: # Consider last 5 turns for context
full_prompt += f"Human: {turn[0]}\nAdam: {turn[1]}\n"
full_prompt += f"Human: {prompt}\nAdam:"
input_ids = finetuned_lm_tokenizer(full_prompt, return_tensors="pt", max_length=512, truncation=True).input_ids
if torch.cuda.is_available():
input_ids = input_ids.cuda()
finetuned_lm_model = finetuned_lm_model.cuda()
# Generate the response
outputs = finetuned_lm_model.generate(
input_ids,
max_length=max_length + input_ids.shape[1],
num_return_sequences=1,
no_repeat_ngram_size=2,
do_sample=True,
temperature=0.7,
top_k=50,
top_p=0.95
)
generated_text = finetuned_lm_tokenizer.decode(outputs[0], skip_special_tokens=True)
return generated_text.strip()
def update_emotion_history(emotion, intensity):
global emotion_history
emotion_history.append({
'emotion': emotion,
'intensity': intensity,
'timestamp': pd.Timestamp.now().isoformat()
})
save_historical_data(emotion_history)
def get_dominant_emotion():
return max(emotions, key=lambda x: emotions[x]['percentage'] if x != 'ideal_state' else 0)
def get_emotion_summary():
summary = []
for emotion, data in emotions.items():
if emotion != 'ideal_state':
summary.append(f"{emotion.capitalize()}: {data['percentage']:.1f}% (Intensity: {data['intensity']:.1f})")
return "\n".join(summary)
def reset_emotions():
global emotions
for emotion in emotions:
if emotion != 'ideal_state':
emotions[emotion]['percentage'] = 10
emotions[emotion]['intensity'] = 0
emotions['ideal_state']['percentage'] = 100
return get_emotion_summary()
def respond_to_user(user_input, chat_history):
# Predict the emotion from the user input
predicted_emotion = predict_emotion(user_input)
# Ensure the predicted emotion exists in our emotions dictionary
if predicted_emotion not in emotions:
predicted_emotion = 'neutral'
# Update the emotional state
update_emotion(predicted_emotion, 5, random.uniform(0, 10))
# Get the current dominant emotion
dominant_emotion = get_dominant_emotion()
# Generate a response considering the dominant emotion
response = generate_text(user_input, chat_history, dominant_emotion)
# Update emotion history
update_emotion_history(predicted_emotion, emotions[predicted_emotion]['intensity'])
# Update chat history
chat_history.append((user_input, response))
# Evolve emotions periodically (e.g., every 10 interactions)
if len(chat_history) % 10 == 0:
evolve_emotions()
return response, chat_history, get_emotion_summary()
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Adam: Emotion-Aware AI Chatbot")
gr.Markdown("Chat with Adam, an AI that understands and responds to emotions.")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Type your message here...")
clear = gr.Button("Clear")
emotion_state = gr.Textbox(label="Current Emotional State", lines=10)
reset_button = gr.Button("Reset Emotions")
def user(user_message, history):
response, updated_history, emotion_summary = respond_to_user(user_message, history)
return "", updated_history, emotion_summary
msg.submit(user, [msg, chatbot], [msg, chatbot, emotion_state])
clear.click(lambda: None, None, chatbot, queue=False)
reset_button.click(reset_emotions, None, emotion_state, queue=False)
if __name__ == "__main__":
demo.launch()
|