Sephfox commited on
Commit
ff6946f
·
verified ·
1 Parent(s): 1a498d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -137
app.py CHANGED
@@ -1,140 +1,4 @@
1
- import warnings
2
- import numpy as np
3
- import pandas as pd
4
- import os
5
- import json
6
- import random
7
- import gradio as gr
8
- import torch
9
- import torch.nn as nn
10
- import torch.optim as optim
11
- from torch.utils.data import DataLoader, IterableDataset
12
- from sklearn.ensemble import IsolationForest, RandomForestClassifier
13
- from sklearn.model_selection import train_test_split
14
- from sklearn.preprocessing import OneHotEncoder
15
- from sklearn.neural_network import MLPClassifier
16
- from deap import base, creator, tools, algorithms
17
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, AutoModelForSequenceClassification
18
- import gc
19
- import multiprocessing as mp
20
- from joblib import Parallel, delayed
21
-
22
- warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hub.file_download')
23
-
24
- # Initialize Example Emotions Dataset
25
- data = {
26
- 'context': [
27
- 'I am happy', 'I am sad', 'I am angry', 'I am excited', 'I am calm',
28
- 'I am feeling joyful', 'I am grieving', 'I am feeling peaceful', 'I am frustrated',
29
- 'I am determined', 'I feel resentment', 'I am feeling glorious', 'I am motivated',
30
- 'I am surprised', 'I am fearful', 'I am trusting', 'I feel disgust', 'I am optimistic',
31
- 'I am pessimistic', 'I feel bored', 'I am envious'
32
- ],
33
- 'emotion': [
34
- 'joy', 'sadness', 'anger', 'joy', 'calmness', 'joy', 'grief', 'calmness', 'anger',
35
- 'determination', 'resentment', 'glory', 'motivation', 'surprise', 'fear', 'trust',
36
- 'disgust', 'optimism', 'pessimism', 'boredom', 'envy'
37
- ]
38
- }
39
- df = pd.DataFrame(data)
40
-
41
- # Encoding the contexts using One-Hot Encoding (memory-efficient)
42
- encoder = OneHotEncoder(handle_unknown='ignore', sparse=True)
43
- contexts_encoded = encoder.fit_transform(df[['context']])
44
-
45
- # Encoding emotions
46
- emotions_target = pd.Categorical(df['emotion']).codes
47
- emotion_classes = pd.Categorical(df['emotion']).categories
48
-
49
- # Memory-efficient Neural Network with PyTorch
50
- class MemoryEfficientNN(nn.Module):
51
- def __init__(self, input_size, hidden_size, num_classes):
52
- super(MemoryEfficientNN, self).__init__()
53
- self.layers = nn.Sequential(
54
- nn.Embedding(input_size, hidden_size),
55
- nn.ReLU(),
56
- nn.Dropout(0.2),
57
- nn.Linear(hidden_size, hidden_size),
58
- nn.ReLU(),
59
- nn.Dropout(0.2),
60
- nn.Linear(hidden_size, num_classes)
61
- )
62
-
63
- def forward(self, x):
64
- return self.layers(x.long())
65
-
66
- # Memory-efficient dataset
67
- class MemoryEfficientDataset(IterableDataset):
68
- def __init__(self, X, y, batch_size):
69
- self.X = X
70
- self.y = torch.LongTensor(y) # Convert labels to long tensors
71
- self.batch_size = batch_size
72
-
73
- def __iter__(self):
74
- for i in range(0, len(self.y), self.batch_size):
75
- X_batch = self.X[i:i+self.batch_size].toarray()
76
- y_batch = self.y[i:i+self.batch_size]
77
- yield torch.FloatTensor(X_batch), y_batch
78
-
79
- # Train Memory-Efficient Neural Network
80
- X_train, X_test, y_train, y_test = train_test_split(contexts_encoded, emotions_target, test_size=0.2, random_state=42)
81
- input_size = X_train.shape[1]
82
- hidden_size = 64
83
- num_classes = len(emotion_classes)
84
-
85
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
86
- model = MemoryEfficientNN(input_size, hidden_size, num_classes).to(device)
87
- criterion = nn.CrossEntropyLoss()
88
- optimizer = optim.Adam(model.parameters(), lr=0.001)
89
-
90
- train_dataset = MemoryEfficientDataset(X_train, y_train, batch_size=32)
91
- train_loader = DataLoader(train_dataset, batch_size=None, num_workers=4, pin_memory=True)
92
-
93
- num_epochs = 100
94
- for epoch in range(num_epochs):
95
- for batch_X, batch_y in train_loader:
96
- batch_X, batch_y = batch_X.to(device, non_blocking=True), batch_y.to(device, non_blocking=True)
97
- outputs = model(batch_X)
98
- loss = criterion(outputs, batch_y)
99
- optimizer.zero_grad()
100
- loss.backward()
101
- optimizer.step()
102
- gc.collect() # Garbage collection after each epoch
103
-
104
- # Ensemble with Random Forest (memory-efficient)
105
- rf_model = RandomForestClassifier(n_estimators=50, random_state=42, n_jobs=-1)
106
- 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
- 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},
115
- 'pleasure': {'percentage': 10, 'motivation': 'selfish', 'intensity': 0},
116
- 'sadness': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
117
- 'grief': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
118
- 'anger': {'percentage': 10, 'motivation': 'traumatic or strong', 'intensity': 0},
119
- 'calmness': {'percentage': 10, 'motivation': 'neutral', 'intensity': 0},
120
- 'determination': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
121
- 'resentment': {'percentage': 10, 'motivation': 'negative', 'intensity': 0},
122
- 'glory': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
123
- 'motivation': {'percentage': 10, 'motivation': 'positive', 'intensity': 0},
124
- 'ideal_state': {'percentage': 100, 'motivation': 'balanced', 'intensity': 0},
125
- 'fear': {'percentage': 10, 'motivation': 'defensive', 'intensity': 0},
126
- 'surprise': {'percentage': 10, 'motivation': 'unexpected', 'intensity': 0},
127
- 'anticipation': {'percentage': 10, 'motivation': 'predictive', 'intensity': 0},
128
- 'trust': {'percentage': 10, 'motivation': 'reliable', 'intensity': 0},
129
- 'disgust': {'percentage': 10, 'motivation': 'repulsive', '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
- }
135
- total_percentage = 200
136
- default_percentage = total_percentage / len(emotions)
137
- for emotion in emotion_history_file = 'emotion_history.json'
138
 
139
  def load_historical_data(file_path=emotion_history_file):
140
  if os.path.exists(file_path):
 
1
+ emotion_history_file = 'emotion_history.json'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def load_historical_data(file_path=emotion_history_file):
4
  if os.path.exists(file_path):