Sephfox commited on
Commit
5a9d9de
·
verified ·
1 Parent(s): ea92796

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -146,7 +146,8 @@ def evolve_emotions():
146
  for i, (emotion, data) in enumerate(emotions.items()):
147
  data['percentage'] = emotion_values[i]
148
  data['intensity'] = intensities[i]
149
- # Normalize percentages
 
150
  total = sum(e['percentage'] for e in emotions.values())
151
  for e in emotions:
152
  emotions[e]['percentage'] = (emotions[e]['percentage'] / total) * 100
@@ -192,16 +193,17 @@ def get_ai_emotion(input_text):
192
 
193
  def generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion_intensity):
194
  # Generate an emotion visualization based on the AI's emotional state
195
- # This could involve creating an image or a visualization using Matplotlib/Seaborn
196
- # The generated image should be saved and returned as the output
197
  emotion_visualization_path = 'emotional_state.png'
198
  try:
199
  # Generate and save the emotion visualization
200
- plt.figure(figsize=(6, 6))
201
- sns.barplot(x=['Emotion'], y=[ai_emotion_percentage], color=sns.color_palette()[emotions.index(ai_emotion)])
202
- plt.title(f'Current Emotional State: {ai_emotion.capitalize()}')
 
 
203
  plt.xlabel('Emotion')
204
  plt.ylabel('Percentage')
 
205
  plt.savefig(emotion_visualization_path)
206
  plt.close()
207
  except Exception as e:
@@ -209,13 +211,13 @@ def generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion
209
  emotion_visualization_path = None
210
  return emotion_visualization_path
211
 
212
- def generate_response(ai_emotion, conversation_history):
213
- # Prepare a prompt based on the current emotion
214
- prompt = f"As an AI assistant, I am currently feeling {ai_emotion}. My response will reflect this emotional state."
215
 
216
  # Add conversation history to the prompt
217
  for entry in conversation_history[-100:]: # Use last 100 entries for context
218
- prompt = f"My previous response: {entry['response']}\n" + prompt
219
 
220
  inputs = response_tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=8192)
221
 
@@ -252,10 +254,10 @@ def interactive_interface(input_text):
252
  text_complexity = analyze_text_complexity(input_text)
253
  ai_emotion, ai_emotion_percentage, ai_emotion_intensity = get_ai_emotion(input_text)
254
  emotion_visualization = generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion_intensity)
255
- response = generate_response(ai_emotion, conversation_history)
256
 
257
  # Update conversation history
258
- conversation_history.append({'response': response})
259
  if len(conversation_history) > max_history_length:
260
  conversation_history.pop(0)
261
 
@@ -266,10 +268,10 @@ def interactive_interface(input_text):
266
  gr.Textbox(value=str(entities), label="Extracted Entities"),
267
  gr.Textbox(value=str(text_complexity), label="Text Complexity"),
268
  gr.Textbox(value=ai_emotion, label="AI Emotion"),
269
- gr.Textbox(value=str(ai_emotion_percentage), label="AI Emotion Percentage"),
270
- gr.Textbox(value=str(ai_emotion_intensity), label="AI Emotion Intensity"),
271
  gr.Image(value=emotion_visualization, label="Emotion Visualization"),
272
- gr.Textbox(value=response, label="AI Response")
273
  )
274
 
275
  # 443 additional features
@@ -302,7 +304,7 @@ def visualize_emotions():
302
  plt.xticks(rotation=90)
303
  plt.savefig('emotion_intensities.png')
304
 
305
- return 'emotion_percentages.png', 'emotion_intensities.png'
306
 
307
  # Create the Gradio interface
308
  iface = gr.Interface(
 
146
  for i, (emotion, data) in enumerate(emotions.items()):
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
 
193
 
194
  def generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion_intensity):
195
  # Generate an emotion visualization based on the AI's emotional state
 
 
196
  emotion_visualization_path = 'emotional_state.png'
197
  try:
198
  # Generate and save the emotion visualization
199
+ plt.figure(figsize=(8, 6))
200
+ emotions_df = pd.DataFrame([(e, d['percentage'], d['intensity']) for e, d in emotions.items()],
201
+ columns=['emotion', 'percentage', 'intensity'])
202
+ sns.barplot(x='emotion', y='percentage', data=emotions_df)
203
+ plt.title(f'Current Emotional State: {ai_emotion.capitalize()} ({ai_emotion_percentage:.2f}%)')
204
  plt.xlabel('Emotion')
205
  plt.ylabel('Percentage')
206
+ plt.xticks(rotation=90)
207
  plt.savefig(emotion_visualization_path)
208
  plt.close()
209
  except Exception as e:
 
211
  emotion_visualization_path = None
212
  return emotion_visualization_path
213
 
214
+ def generate_response(input_text, ai_emotion, conversation_history):
215
+ # Prepare a prompt based on the current emotion and input
216
+ prompt = f"As an AI assistant, I am currently feeling {ai_emotion}. My response will reflect this emotional state. Human: {input_text}\nAI: "
217
 
218
  # Add conversation history to the prompt
219
  for entry in conversation_history[-100:]: # Use last 100 entries for context
220
+ prompt = f"Human: {entry['user']}\nAI: {entry['response']}\n" + prompt
221
 
222
  inputs = response_tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=8192)
223
 
 
254
  text_complexity = analyze_text_complexity(input_text)
255
  ai_emotion, ai_emotion_percentage, ai_emotion_intensity = get_ai_emotion(input_text)
256
  emotion_visualization = generate_emotion_visualization(ai_emotion, ai_emotion_percentage, ai_emotion_intensity)
257
+ response = generate_response(input_text, ai_emotion, conversation_history)
258
 
259
  # Update conversation history
260
+ conversation_history.append({'user': input_text, 'response': response})
261
  if len(conversation_history) > max_history_length:
262
  conversation_history.pop(0)
263
 
 
268
  gr.Textbox(value=str(entities), label="Extracted Entities"),
269
  gr.Textbox(value=str(text_complexity), label="Text Complexity"),
270
  gr.Textbox(value=ai_emotion, label="AI Emotion"),
271
+ gr.Textbox(value=f"{ai_emotion_percentage:.2f}%", label="AI Emotion Percentage"),
272
+ gr.Textbox(value=f"{ai_emotion_intensity:.2f}", label="AI Emotion Intensity"),
273
  gr.Image(value=emotion_visualization, label="Emotion Visualization"),
274
+ gr.Textbox(value=f"As an AI assistant, I am currently feeling {ai_emotion}. My response will reflect this emotional state.\n\n{response}", label="AI Response")
275
  )
276
 
277
  # 443 additional features
 
304
  plt.xticks(rotation=90)
305
  plt.savefig('emotion_intensities.png')
306
 
307
+ return 'emotion_percentages.png', 'emotion_intensities.png'
308
 
309
  # Create the Gradio interface
310
  iface = gr.Interface(