Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -107,6 +107,8 @@ rf_model.fit(X_train, y_train)
|
|
107 |
|
108 |
# Isolation Forest Anomaly Detection Model (memory-efficient)
|
109 |
isolation_forest = IsolationForest(contamination=0.1, random_state=42, n_jobs=-1, max_samples='auto')
|
|
|
|
|
110 |
# Enhanced Emotional States
|
111 |
emotions = {
|
112 |
'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
|
@@ -128,10 +130,8 @@ emotions = {
|
|
128 |
'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
|
129 |
'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
|
130 |
'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
|
131 |
-
'envy': {'percentage': 10, 'motivation':
|
132 |
-
|
133 |
-
|
134 |
-
total_percentage = 200
|
135 |
default_percentage = total_percentage / len(emotions)
|
136 |
for emotion in emotions:
|
137 |
emotions[emotion]['percentage'] = default_percentage
|
@@ -208,12 +208,15 @@ def evolve_emotions():
|
|
208 |
else:
|
209 |
emotions[emotion]['percentage'] = best_individual[-1]
|
210 |
|
211 |
-
#
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
|
215 |
|
216 |
def generate_text(prompt, max_length=100):
|
|
|
217 |
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(lm_model.device)
|
218 |
with torch.no_grad():
|
219 |
output = lm_model.generate(
|
@@ -229,7 +232,7 @@ def generate_text(prompt, max_length=100):
|
|
229 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
230 |
return generated_text
|
231 |
|
232 |
-
sentiment_pipeline = pipeline("sentiment-analysis", model=
|
233 |
|
234 |
def get_sentiment(text):
|
235 |
result = sentiment_pipeline(text)[0]
|
@@ -249,46 +252,38 @@ def get_emotional_response(context):
|
|
249 |
|
250 |
# Weighted ensemble
|
251 |
ensemble_prediction = (0.6 * nn_prediction + 0.4 * rf_prediction)
|
252 |
-
predicted_emotion = emotion_classes[int(
|
253 |
-
|
254 |
-
#
|
255 |
anomaly_score = isolation_forest.decision_function(context_encoded.toarray())
|
256 |
-
is_anomaly =
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
update_emotion(predicted_emotion, 20, intensity)
|
264 |
-
evolve_emotions()
|
265 |
-
|
266 |
-
emotion_history.append(emotions.copy())
|
267 |
-
save_historical_data(emotion_history)
|
268 |
-
|
269 |
-
response = f"Predicted Emotion: {predicted_emotion}\n"
|
270 |
-
response += f"Emotion Details: {emotions[predicted_emotion]}\n"
|
271 |
-
response += f"Anomaly Detected: {'Yes' if is_anomaly else 'No'}\n"
|
272 |
-
response += f"Emotion Intensity: {intensity:.2f}/10\n"
|
273 |
-
response += f"Current Emotional State: {json.dumps(emotions, indent=2)}"
|
274 |
-
|
275 |
-
return response
|
276 |
|
|
|
277 |
def process_input(input_text):
|
278 |
emotional_response = get_emotional_response(input_text)
|
279 |
-
|
280 |
-
generated_text = generate_text(
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
283 |
|
284 |
-
# Gradio Interface
|
285 |
iface = gr.Interface(
|
286 |
fn=process_input,
|
287 |
inputs="text",
|
288 |
-
outputs=
|
289 |
-
|
290 |
-
|
|
|
|
|
|
|
|
|
291 |
)
|
292 |
|
293 |
-
|
294 |
-
iface.launch()
|
|
|
107 |
|
108 |
# Isolation Forest Anomaly Detection Model (memory-efficient)
|
109 |
isolation_forest = IsolationForest(contamination=0.1, random_state=42, n_jobs=-1, max_samples='auto')
|
110 |
+
isolation_forest.fit(X_train) # Fit the model before using it
|
111 |
+
|
112 |
# Enhanced Emotional States
|
113 |
emotions = {
|
114 |
'joy': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
|
|
|
130 |
'optimism': {'percentage': 10, 'motivation': 'hopeful', 'intensity': 0},
|
131 |
'pessimism': {'percentage': 10, 'motivation': 'doubtful', 'intensity': 0},
|
132 |
'boredom': {'percentage': 10, 'motivation': 'indifferent', 'intensity': 0},
|
133 |
+
'envy': {'percentage': 10, 'motivation': 'jealous' , 'intensity': 0},
|
134 |
+
total_percentage = 200
|
|
|
|
|
135 |
default_percentage = total_percentage / len(emotions)
|
136 |
for emotion in emotions:
|
137 |
emotions[emotion]['percentage'] = default_percentage
|
|
|
208 |
else:
|
209 |
emotions[emotion]['percentage'] = best_individual[-1]
|
210 |
|
211 |
+
# Lazy loading for the language model
|
212 |
+
def get_language_model():
|
213 |
+
model_name = 'bigscience/bloom-1b7'
|
214 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
215 |
+
lm_model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", low_cpu_mem_usage=True)
|
216 |
+
return tokenizer, lm_model
|
217 |
|
218 |
def generate_text(prompt, max_length=100):
|
219 |
+
tokenizer, lm_model = get_language_model()
|
220 |
input_ids = tokenizer.encode(prompt, return_tensors='pt').to(lm_model.device)
|
221 |
with torch.no_grad():
|
222 |
output = lm_model.generate(
|
|
|
232 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
233 |
return generated_text
|
234 |
|
235 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model='distilbert-base-uncased-finetuned-sst-2-english', device_map="auto")
|
236 |
|
237 |
def get_sentiment(text):
|
238 |
result = sentiment_pipeline(text)[0]
|
|
|
252 |
|
253 |
# Weighted ensemble
|
254 |
ensemble_prediction = (0.6 * nn_prediction + 0.4 * rf_prediction)
|
255 |
+
predicted_emotion = emotion_classes[int(ensemble_prediction)]
|
256 |
+
|
257 |
+
# Isolation Forest anomaly detection
|
258 |
anomaly_score = isolation_forest.decision_function(context_encoded.toarray())
|
259 |
+
is_anomaly = isolation_forest.predict(context_encoded.toarray())[0] == -1
|
260 |
+
|
261 |
+
if is_anomaly:
|
262 |
+
return "Anomaly detected in the emotional response."
|
263 |
+
|
264 |
+
return f"Predicted Emotion: {predicted_emotion}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
+
# Gradio interface
|
267 |
def process_input(input_text):
|
268 |
emotional_response = get_emotional_response(input_text)
|
269 |
+
sentiment_response = get_sentiment(input_text)
|
270 |
+
generated_text = generate_text(input_text)
|
271 |
+
return {
|
272 |
+
"Emotional Response": emotional_response,
|
273 |
+
"Sentiment Response": sentiment_response,
|
274 |
+
"Generated Text": generated_text
|
275 |
+
}
|
276 |
|
|
|
277 |
iface = gr.Interface(
|
278 |
fn=process_input,
|
279 |
inputs="text",
|
280 |
+
outputs=[
|
281 |
+
gr.outputs.Textbox(label="Emotional Response"),
|
282 |
+
gr.outputs.Textbox(label="Sentiment Response"),
|
283 |
+
gr.outputs.Textbox(label="Generated Text")
|
284 |
+
],
|
285 |
+
title="Emotion and Sentiment Analysis",
|
286 |
+
description="Analyze emotions and sentiments from text input."
|
287 |
)
|
288 |
|
289 |
+
iface.launch()
|
|