Sephfox commited on
Commit
ce3343c
·
verified ·
1 Parent(s): 8b2be8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -35
app.py CHANGED
@@ -30,11 +30,12 @@ nltk.download('words', quiet=True)
30
  # Initialize Example Dataset (For Emotion Prediction)
31
  data = {
32
  'context': [
33
- 'I am happy', 'I am sad', 'I am angry', 'I am excited', 'I am calm',
34
- 'I am feeling joyful', 'I am grieving', 'I am feeling peaceful', 'I am frustrated',
35
- 'I am determined', 'I feel resentment', 'I am feeling glorious', 'I am motivated',
36
- 'I am surprised', 'I am fearful', 'I am trusting', 'I feel disgust', 'I am optimistic',
37
- 'I am pessimistic', 'I feel bored', 'I am envious'
 
38
  ],
39
  'emotion': [
40
  'joy', 'sadness', 'anger', 'joy', 'calmness', 'joy', 'grief', 'calmness', 'anger',
@@ -69,13 +70,13 @@ response_tokenizer.pad_token = response_tokenizer.eos_token
69
 
70
  # Enhanced Emotional States
71
  emotions = {
72
- 'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
73
- 'sadness': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
74
- 'anger': {'percentage': 10, 'motivation': 'traumatic or strong', 'intensity': 0},
75
- 'fear': {'percentage': 10, 'motivation': 'defensive', 'intensity': 0},
76
- 'love': {'percentage': 10, 'motivation': 'affectionate', 'intensity': 0},
77
- 'surprise': {'percentage': 10, 'motivation': 'unexpected', 'intensity': 0},
78
- 'neutral': {'percentage': 40, 'motivation': 'balanced', 'intensity': 0},
79
  }
80
 
81
  total_percentage = 100
@@ -108,7 +109,6 @@ def update_emotion(emotion, percentage, intensity):
108
  def normalize_context(context):
109
  return context.lower().strip()
110
 
111
- # Create FitnessMulti and Individual outside of evolve_emotions
112
  creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -0.5, -0.2))
113
  creator.create("Individual", list, fitness=creator.FitnessMulti)
114
 
@@ -147,7 +147,7 @@ def evolve_emotions():
147
  data['percentage'] = emotion_values[i]
148
  data['intensity'] = intensities[i]
149
 
150
- # Normalize percentages
151
  total = sum(e['percentage'] for e in emotions.values())
152
  for e in emotions:
153
  emotions[e]['percentage'] = (emotions[e]['percentage'] / total) * 100
@@ -175,7 +175,7 @@ def feature_transformations():
175
 
176
  def generate_response(input_text, ai_emotion, conversation_history):
177
  # Prepare a prompt based on the current emotion and input
178
- prompt = f"You are an AI assistant currently feeling {ai_emotion}. Your response should reflect this emotion. Human: {input_text}\nAI:"
179
 
180
  # Add conversation history to the prompt
181
  for entry in conversation_history[-100:]: # Use last 100 entries for context
@@ -247,7 +247,7 @@ def visualize_emotions():
247
 
248
  plt.figure(figsize=(12, 6))
249
  sns.barplot(x='Emotion', y='Percentage', data=emotions_df)
250
- plt.title('Current Emotional State')
251
  plt.xticks(rotation=45, ha='right')
252
  plt.tight_layout()
253
  plt.savefig('emotional_state.png')
@@ -290,40 +290,37 @@ def interactive_interface(input_text):
290
 
291
  analysis_result = {
292
  'predicted_user_emotion': predicted_emotion,
293
- 'ai_emotion': ai_emotion,
294
  'sentiment_scores': sentiment_scores,
295
  'entities': entities,
296
  'text_complexity': text_complexity,
297
- 'response': response,
 
 
298
  'emotion_visualization': emotion_visualization
299
  }
300
 
301
  return analysis_result
302
  except Exception as e:
303
  print(f"Error: {e}")
304
- return {
305
- 'predicted_user_emotion': 'unknown',
306
- 'ai_emotion': 'neutral',
307
- 'sentiment_scores': {'compound': 0, 'neg': 0, 'neu': 1, 'pos': 0},
308
- 'entities': [],
309
- 'text_complexity': {'word_count': 0, 'sentence_count': 0, 'average_sentence_length': 0, 'polarity': 0, 'subjectivity': 0},
310
- 'response': "I'm sorry, but I encountered an error and was unable to generate a response.",
311
- 'emotion_visualization': 'emotional_state.png'
312
- }
313
 
314
- # Create a Gradio interface
315
- gr.Interface(
316
  fn=interactive_interface,
317
- inputs=gr.Textbox(label="Your Message"),
318
  outputs=[
319
  gr.Textbox(label="Predicted User Emotion"),
320
- gr.Textbox(label="AI Emotion"),
321
  gr.Textbox(label="Sentiment Scores"),
322
  gr.Textbox(label="Extracted Entities"),
323
  gr.Textbox(label="Text Complexity"),
324
- gr.Textbox(label="AI Response"),
 
 
325
  gr.Image(label="Emotional State Visualization")
326
  ],
327
- title="Emotion-Aware AI Assistant",
328
- description="Interact with an AI assistant that responds based on its emotional state.",
329
- ).launch()
 
 
 
 
30
  # Initialize Example Dataset (For Emotion Prediction)
31
  data = {
32
  'context': [
33
+ 'I am overjoyed', 'I am deeply saddened', 'I am seething with rage', 'I am exhilarated', 'I am tranquil',
34
+ 'I am brimming with joy', 'I am grieving profoundly', 'I am at peace', 'I am frustrated beyond measure',
35
+ 'I am determined to succeed', 'I feel resentment burning within me', 'I am feeling glorious and triumphant',
36
+ 'I am motivated and inspired', 'I am utterly surprised', 'I am gripped by fear', 'I am trusting and open',
37
+ 'I feel a sense of disgust', 'I am optimistic and hopeful', 'I am pessimistic and gloomy', 'I feel bored and listless',
38
+ 'I am envious and jealous'
39
  ],
40
  'emotion': [
41
  'joy', 'sadness', 'anger', 'joy', 'calmness', 'joy', 'grief', 'calmness', 'anger',
 
70
 
71
  # Enhanced Emotional States
72
  emotions = {
73
+ 'joy': {'percentage': 10, 'motivation': 'positive and uplifting', 'intensity': 0},
74
+ 'sadness': {'percentage': 10, 'motivation': 'reflective and introspective', 'intensity': 0},
75
+ 'anger': {'percentage': 10, 'motivation': 'passionate and driven', 'intensity': 0},
76
+ 'fear': {'percentage': 10, 'motivation': 'cautious and protective', 'intensity': 0},
77
+ 'love': {'percentage': 10, 'motivation': 'affectionate and caring', 'intensity': 0},
78
+ 'surprise': {'percentage': 10, 'motivation': 'curious and intrigued', 'intensity': 0},
79
+ 'neutral': {'percentage': 40, 'motivation': 'balanced and composed', 'intensity': 0},
80
  }
81
 
82
  total_percentage = 100
 
109
  def normalize_context(context):
110
  return context.lower().strip()
111
 
 
112
  creator.create("FitnessMulti", base.Fitness, weights=(-1.0, -0.5, -0.2))
113
  creator.create("Individual", list, fitness=creator.FitnessMulti)
114
 
 
147
  data['percentage'] = emotion_values[i]
148
  data['intensity'] = intensities[i]
149
 
150
+ # Normalize percentages
151
  total = sum(e['percentage'] for e in emotions.values())
152
  for e in emotions:
153
  emotions[e]['percentage'] = (emotions[e]['percentage'] / total) * 100
 
175
 
176
  def generate_response(input_text, ai_emotion, conversation_history):
177
  # Prepare a prompt based on the current emotion and input
178
+ prompt = f"As an AI assistant, I am currently feeling {ai_emotion}. My response will reflect this emotional state. Human: {input_text}\nAI:"
179
 
180
  # Add conversation history to the prompt
181
  for entry in conversation_history[-100:]: # Use last 100 entries for context
 
247
 
248
  plt.figure(figsize=(12, 6))
249
  sns.barplot(x='Emotion', y='Percentage', data=emotions_df)
250
+ plt.title('Current Emotional State of the AI')
251
  plt.xticks(rotation=45, ha='right')
252
  plt.tight_layout()
253
  plt.savefig('emotional_state.png')
 
290
 
291
  analysis_result = {
292
  'predicted_user_emotion': predicted_emotion,
 
293
  'sentiment_scores': sentiment_scores,
294
  'entities': entities,
295
  'text_complexity': text_complexity,
296
+ 'ai_emotion': ai_emotion,
297
+ 'ai_emotion_percentage': emotions[ai_emotion]['percentage'],
298
+ 'ai_emotion_intensity': emotions[ai_emotion]['intensity'],
299
  'emotion_visualization': emotion_visualization
300
  }
301
 
302
  return analysis_result
303
  except Exception as e:
304
  print(f"Error: {e}")
305
+ return {'error': str(e)}
 
 
 
 
 
 
 
 
306
 
307
+ # Define the Gradio interface
308
+ demo = gr.Interface(
309
  fn=interactive_interface,
310
+ inputs=gr.Textbox(label="Enter your message"),
311
  outputs=[
312
  gr.Textbox(label="Predicted User Emotion"),
 
313
  gr.Textbox(label="Sentiment Scores"),
314
  gr.Textbox(label="Extracted Entities"),
315
  gr.Textbox(label="Text Complexity"),
316
+ gr.Textbox(label="AI Emotion"),
317
+ gr.Textbox(label="AI Emotion Percentage"),
318
+ gr.Textbox(label="AI Emotion Intensity"),
319
  gr.Image(label="Emotional State Visualization")
320
  ],
321
+ title="Emotional AI Assistant",
322
+ description="Interact with an AI assistant that adapts its responses based on its emotional state."
323
+ )
324
+
325
+ # Launch the Gradio interface
326
+ demo.launch()