Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
predicted_class = torch.argmax(logits, dim=1).item()
|
175 |
-
predicted_emotion = emotion_classes[predicted_class]
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
180 |
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
return predicted_emotion
|
185 |
|
186 |
-
def
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
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 |
-
|
203 |
-
|
204 |
-
|
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 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
|
|
|
|
217 |
|
218 |
if __name__ == "__main__":
|
219 |
-
|
|
|
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()
|