Sephfox commited on
Commit
a10f37d
·
verified ·
1 Parent(s): b726416

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -42
app.py CHANGED
@@ -42,7 +42,7 @@ emotion_classes = pd.Categorical(df['emotion']).categories
42
  emotion_prediction_model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
43
  emotion_prediction_tokenizer = AutoTokenizer.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
44
 
45
- # Load pre-trained LLM model and tokenizer for response generation
46
  response_model_name = "microsoft/DialoGPT-medium"
47
  response_tokenizer = AutoTokenizer.from_pretrained(response_model_name)
48
  response_model = AutoModelForCausalLM.from_pretrained(response_model_name)
@@ -163,57 +163,54 @@ def update_emotion_history(emotion, percentage, intensity, context):
163
  'timestamp': pd.Timestamp.now().isoformat()
164
  }
165
  emotion_history.append(entry)
166
- if len(emotion_history) > 100:
167
- emotion_history.pop(0)
168
  save_historical_data(emotion_history)
169
 
170
- def predict_emotion(context):
171
- tokens = emotion_prediction_tokenizer(context, return_tensors='pt', padding=True, truncation=True)
172
- outputs = emotion_prediction_model(**tokens)
173
- logits = outputs.logits
174
- predicted_class = torch.argmax(logits, dim=1).item()
175
- predicted_emotion = emotion_classes[predicted_class]
176
 
177
- # Get percentage and intensity based on context
178
- percentage = np.random.uniform(5, 20)
179
- intensity = np.random.uniform(1, 10)
 
180
 
181
- update_emotion(predicted_emotion, percentage, intensity)
182
- update_emotion_history(predicted_emotion, percentage, intensity, context)
 
 
 
183
 
 
 
 
 
 
 
184
  return predicted_emotion
185
 
186
- def generate_response(context):
187
- tokenizer, model = get_finetuned_lm_model()
188
- inputs = tokenizer.encode(context, return_tensors='pt')
189
-
190
- # Ensure pad_token_id is a tensor
191
- pad_token_id = torch.tensor(tokenizer.pad_token_id)
192
-
193
- outputs = model.generate(inputs, max_length=500, num_return_sequences=1, pad_token_id=pad_token_id.item(), eos_token_id=tokenizer.eos_token_id)
194
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
195
-
196
- # Ensure the response does not repeat the input
197
- if context in response:
198
- response = response.replace(context, '').strip()
199
-
200
  return response
201
 
202
- def handle_conversation(user_input):
203
- user_input = normalize_context(user_input)
204
- predicted_emotion = predict_emotion(user_input)
205
- bot_response = generate_response(user_input)
206
- return f"Emotion: {predicted_emotion}, Response: {bot_response}"
207
-
208
- def update_ui(user_input):
209
- response = handle_conversation(user_input)
210
  return response
211
 
212
- with gr.Blocks() as demo:
213
- user_input = gr.Textbox(label="User Input")
214
- response = gr.Textbox(label="Bot Response")
215
- submit = gr.Button("Submit")
216
- submit.click(update_ui, inputs=[user_input], outputs=[response])
 
 
217
 
218
  if __name__ == "__main__":
219
- demo.launch(share=True)
 
42
  emotion_prediction_model = AutoModelForSequenceClassification.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
43
  emotion_prediction_tokenizer = AutoTokenizer.from_pretrained("bhadresh-savani/distilbert-base-uncased-emotion")
44
 
45
+ # Load pre-trained LLM model and tokenizer for response generation with increased context window
46
  response_model_name = "microsoft/DialoGPT-medium"
47
  response_tokenizer = AutoTokenizer.from_pretrained(response_model_name)
48
  response_model = AutoModelForCausalLM.from_pretrained(response_model_name)
 
163
  'timestamp': pd.Timestamp.now().isoformat()
164
  }
165
  emotion_history.append(entry)
 
 
166
  save_historical_data(emotion_history)
167
 
168
+ # Adding 443 features
169
+ additional_features = {}
170
+ for i in range(443):
171
+ additional_features[f'feature_{i+1}'] = 0
 
 
172
 
173
+ def feature_transformations():
174
+ global additional_features
175
+ for feature in additional_features:
176
+ additional_features[feature] += random.uniform(-1, 1)
177
 
178
+ def generate_response(input_text):
179
+ inputs = response_tokenizer(input_text, return_tensors="pt")
180
+ response_ids = response_model.generate(inputs.input_ids, max_length=int(inputs.input_ids.shape[1] * 2.289))
181
+ response = response_tokenizer.decode(response_ids[:, inputs.input_ids.shape[1]:][0], skip_special_tokens=True)
182
+ return response
183
 
184
+ def predict_emotion(context):
185
+ inputs = emotion_prediction_tokenizer(context, return_tensors="pt")
186
+ outputs = emotion_prediction_model(**inputs)
187
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
188
+ predicted_class = torch.argmax(predictions, dim=-1).item()
189
+ predicted_emotion = emotion_classes[predicted_class]
190
  return predicted_emotion
191
 
192
+ def interactive_interface(context):
193
+ normalized_context = normalize_context(context)
194
+ predicted_emotion = predict_emotion(normalized_context)
195
+ update_emotion(predicted_emotion, random.uniform(5, 15), random.uniform(1, 10))
196
+ update_emotion_history(predicted_emotion, emotions[predicted_emotion]['percentage'], emotions[predicted_emotion]['intensity'], normalized_context)
197
+ evolve_emotions()
198
+ feature_transformations()
199
+ response = generate_response(normalized_context)
 
 
 
 
 
 
200
  return response
201
 
202
+ # Gradio Interface
203
+ def gradio_interface(input_text):
204
+ response = interactive_interface(input_text)
 
 
 
 
 
205
  return response
206
 
207
+ iface = gr.Interface(
208
+ fn=gradio_interface,
209
+ inputs="text",
210
+ outputs="text",
211
+ title="Emotion-aware AI with Enhanced Features",
212
+ description="An AI that predicts emotions from text and generates responses with 443 additional features."
213
+ )
214
 
215
  if __name__ == "__main__":
216
+ iface.launch()