Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -76,8 +76,10 @@ emotions = {
|
|
76 |
'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
|
77 |
'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
|
78 |
'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
|
79 |
-
'envy': {'percentage': 10, 'motivation': 'jealous', 'intensity': 0}
|
|
|
80 |
}
|
|
|
81 |
total_percentage = 200
|
82 |
emotion_history_file = 'emotion_history.json'
|
83 |
|
@@ -127,6 +129,7 @@ def evolve_emotions():
|
|
127 |
(toolbox.attr_float,) * (len(emotions) - 1) +
|
128 |
(toolbox.attr_intensity,) * len(emotions) +
|
129 |
(lambda: 100,), n=1)
|
|
|
130 |
toolbox.register("mate", tools.cxTwoPoint)
|
131 |
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
|
132 |
toolbox.register("select", tools.selNSGA2)
|
@@ -151,8 +154,19 @@ def predict_emotion(context):
|
|
151 |
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer, top_k=None)
|
152 |
predictions = emotion_prediction_pipeline(context)
|
153 |
emotion_scores = {prediction['label']: prediction['score'] for prediction in predictions[0]}
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
def generate_text(prompt, emotion=None, max_length=100):
|
158 |
finetuned_lm_tokenizer, finetuned_lm_model = get_finetuned_lm_model()
|
@@ -191,28 +205,91 @@ def generate_text(prompt, emotion=None, max_length=100):
|
|
191 |
generated_text = finetuned_lm_tokenizer.decode(output[0], skip_special_tokens=True)
|
192 |
return generated_text
|
193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
def respond_to_user(user_input, chat_history):
|
195 |
# Predict the emotion from the user input
|
196 |
-
|
|
|
|
|
|
|
|
|
197 |
|
198 |
# Update the emotional state
|
199 |
-
update_emotion(
|
200 |
|
201 |
-
#
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
# Update chat history
|
205 |
chat_history.append((user_input, response))
|
206 |
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
|
209 |
# Gradio interface
|
210 |
iface = gr.Interface(
|
211 |
-
fn=
|
212 |
-
inputs=[
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
description="Chat with an AI that understands and responds to emotions.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
)
|
217 |
|
218 |
if __name__ == "__main__":
|
|
|
76 |
'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
|
77 |
'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
|
78 |
'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
|
79 |
+
'envy': {'percentage': 10, 'motivation': 'jealous', 'intensity': 0},
|
80 |
+
'neutral': {'percentage': 10, 'motivation': 'balanced', 'intensity': 0}
|
81 |
}
|
82 |
+
|
83 |
total_percentage = 200
|
84 |
emotion_history_file = 'emotion_history.json'
|
85 |
|
|
|
129 |
(toolbox.attr_float,) * (len(emotions) - 1) +
|
130 |
(toolbox.attr_intensity,) * len(emotions) +
|
131 |
(lambda: 100,), n=1)
|
132 |
+
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
|
133 |
toolbox.register("mate", tools.cxTwoPoint)
|
134 |
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
|
135 |
toolbox.register("select", tools.selNSGA2)
|
|
|
154 |
emotion_prediction_pipeline = pipeline('text-classification', model=emotion_prediction_model, tokenizer=emotion_prediction_tokenizer, top_k=None)
|
155 |
predictions = emotion_prediction_pipeline(context)
|
156 |
emotion_scores = {prediction['label']: prediction['score'] for prediction in predictions[0]}
|
157 |
+
predicted_label = max(emotion_scores, key=emotion_scores.get)
|
158 |
+
|
159 |
+
# Map the predicted label to our emotion categories
|
160 |
+
emotion_mapping = {
|
161 |
+
'LABEL_0': 'joy',
|
162 |
+
'LABEL_1': 'sadness',
|
163 |
+
'LABEL_2': 'anger',
|
164 |
+
'LABEL_3': 'fear',
|
165 |
+
'LABEL_4': 'surprise',
|
166 |
+
'LABEL_5': 'disgust'
|
167 |
+
}
|
168 |
+
|
169 |
+
return emotion_mapping.get(predicted_label, 'neutral')
|
170 |
|
171 |
def generate_text(prompt, emotion=None, max_length=100):
|
172 |
finetuned_lm_tokenizer, finetuned_lm_model = get_finetuned_lm_model()
|
|
|
205 |
generated_text = finetuned_lm_tokenizer.decode(output[0], skip_special_tokens=True)
|
206 |
return generated_text
|
207 |
|
208 |
+
def update_emotion_history(emotion, intensity):
|
209 |
+
global emotion_history
|
210 |
+
emotion_history.append({
|
211 |
+
'emotion': emotion,
|
212 |
+
'intensity': intensity,
|
213 |
+
'timestamp': pd.Timestamp.now().isoformat()
|
214 |
+
})
|
215 |
+
save_historical_data(emotion_history)
|
216 |
+
|
217 |
+
def get_dominant_emotion():
|
218 |
+
return max(emotions, key=lambda x: emotions[x]['percentage'] if x != 'ideal_state' else 0)
|
219 |
+
|
220 |
+
def get_emotion_summary():
|
221 |
+
summary = []
|
222 |
+
for emotion, data in emotions.items():
|
223 |
+
if emotion != 'ideal_state':
|
224 |
+
summary.append(f"{emotion.capitalize()}: {data['percentage']:.1f}% (Intensity: {data['intensity']:.1f})")
|
225 |
+
return "\n".join(summary)
|
226 |
+
|
227 |
+
def reset_emotions():
|
228 |
+
global emotions
|
229 |
+
for emotion in emotions:
|
230 |
+
if emotion != 'ideal_state':
|
231 |
+
emotions[emotion]['percentage'] = 10
|
232 |
+
emotions[emotion]['intensity'] = 0
|
233 |
+
emotions['ideal_state']['percentage'] = 100
|
234 |
+
|
235 |
def respond_to_user(user_input, chat_history):
|
236 |
# Predict the emotion from the user input
|
237 |
+
predicted_emotion = predict_emotion(user_input)
|
238 |
+
|
239 |
+
# Ensure the predicted emotion exists in our emotions dictionary
|
240 |
+
if predicted_emotion not in emotions:
|
241 |
+
predicted_emotion = 'neutral'
|
242 |
|
243 |
# Update the emotional state
|
244 |
+
update_emotion(predicted_emotion, 5, random.uniform(0, 10))
|
245 |
|
246 |
+
# Get the current dominant emotion
|
247 |
+
dominant_emotion = get_dominant_emotion()
|
248 |
+
|
249 |
+
# Generate a response considering the dominant emotion
|
250 |
+
response = generate_text(user_input, dominant_emotion)
|
251 |
+
|
252 |
+
# Update emotion history
|
253 |
+
update_emotion_history(predicted_emotion, emotions[predicted_emotion]['intensity'])
|
254 |
|
255 |
# Update chat history
|
256 |
chat_history.append((user_input, response))
|
257 |
|
258 |
+
# Evolve emotions periodically (e.g., every 10 interactions)
|
259 |
+
if len(chat_history) % 10 == 0:
|
260 |
+
evolve_emotions()
|
261 |
+
|
262 |
+
return response, chat_history, get_emotion_summary()
|
263 |
+
|
264 |
+
def chat_interface(user_input, history):
|
265 |
+
response, updated_history, emotion_summary = respond_to_user(user_input, history)
|
266 |
+
return response, updated_history, emotion_summary
|
267 |
|
268 |
# Gradio interface
|
269 |
iface = gr.Interface(
|
270 |
+
fn=chat_interface,
|
271 |
+
inputs=[
|
272 |
+
gr.Textbox(lines=2, placeholder="Type your message here..."),
|
273 |
+
gr.State([])
|
274 |
+
],
|
275 |
+
outputs=[
|
276 |
+
gr.Textbox(label="AI Response"),
|
277 |
+
gr.State(),
|
278 |
+
gr.Textbox(label="Current Emotional State", lines=10)
|
279 |
+
],
|
280 |
+
title="Emotion-Aware AI Chatbot",
|
281 |
description="Chat with an AI that understands and responds to emotions.",
|
282 |
+
allow_flagging="never",
|
283 |
+
theme="default"
|
284 |
+
)
|
285 |
+
|
286 |
+
# Add a button to reset emotions
|
287 |
+
reset_button = gr.Button("Reset Emotions")
|
288 |
+
reset_button.click(
|
289 |
+
fn=reset_emotions,
|
290 |
+
inputs=None,
|
291 |
+
outputs=gr.Textbox(label="Current Emotional State", lines=10),
|
292 |
+
api_name="reset_emotions"
|
293 |
)
|
294 |
|
295 |
if __name__ == "__main__":
|