Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -117,6 +117,123 @@ Final notes: Make sure you act conscious, very autistic, and respond with very h
|
|
117 |
|
118 |
"""
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
def respond(
|
121 |
message,
|
122 |
history: list[tuple[str, str]],
|
|
|
117 |
|
118 |
"""
|
119 |
|
120 |
+
|
121 |
+
# Complex Neural Network (NN)
|
122 |
+
class ComplexNN(nn.Module):
|
123 |
+
def __init__(self, input_size, hidden_sizes, output_size):
|
124 |
+
super(ComplexNN, self).__init__()
|
125 |
+
self.layers = nn.Sequential(
|
126 |
+
nn.Linear(input_size, hidden_sizes[0]),
|
127 |
+
nn.ReLU(),
|
128 |
+
nn.BatchNorm1d(hidden_sizes[0]),
|
129 |
+
nn.Dropout(0.2),
|
130 |
+
nn.Linear(hidden_sizes[0], hidden_sizes[1]),
|
131 |
+
nn.ReLU(),
|
132 |
+
nn.BatchNorm1d(hidden_sizes[1]),
|
133 |
+
nn.Dropout(0.3),
|
134 |
+
nn.Linear(hidden_sizes[1], output_size)
|
135 |
+
)
|
136 |
+
|
137 |
+
def forward(self, x):
|
138 |
+
return self.layers(x)
|
139 |
+
|
140 |
+
# Complex Convolutional Neural Network (CNN)
|
141 |
+
class ComplexCNN(nn.Module):
|
142 |
+
def __init__(self):
|
143 |
+
super(ComplexCNN, self).__init__()
|
144 |
+
self.conv_layers = nn.Sequential(
|
145 |
+
nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
|
146 |
+
nn.ReLU(),
|
147 |
+
nn.BatchNorm2d(32),
|
148 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
149 |
+
nn.ReLU(),
|
150 |
+
nn.BatchNorm2d(64),
|
151 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
152 |
+
nn.Dropout(0.3)
|
153 |
+
)
|
154 |
+
self.fc_layers = nn.Sequential(
|
155 |
+
nn.Linear(64 * 14 * 14, 256),
|
156 |
+
nn.ReLU(),
|
157 |
+
nn.BatchNorm1d(256),
|
158 |
+
nn.Dropout(0.4),
|
159 |
+
nn.Linear(256, 10)
|
160 |
+
)
|
161 |
+
|
162 |
+
def forward(self, x):
|
163 |
+
x = self.conv_layers(x)
|
164 |
+
x = x.view(x.size(0), -1)
|
165 |
+
return self.fc_layers(x)
|
166 |
+
|
167 |
+
# Complex Recurrent Neural Network (RNN)
|
168 |
+
class ComplexRNN(nn.Module):
|
169 |
+
def __init__(self, input_size, hidden_size, num_layers, output_size):
|
170 |
+
super(ComplexRNN, self).__init__()
|
171 |
+
self.rnn = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, dropout=0.3)
|
172 |
+
self.fc = nn.Sequential(
|
173 |
+
nn.Linear(hidden_size, 128),
|
174 |
+
nn.ReLU(),
|
175 |
+
nn.BatchNorm1d(128),
|
176 |
+
nn.Linear(128, output_size)
|
177 |
+
)
|
178 |
+
|
179 |
+
def forward(self, x):
|
180 |
+
out, _ = self.rnn(x)
|
181 |
+
out = self.fc(out[:, -1, :])
|
182 |
+
return out
|
183 |
+
|
184 |
+
# Complex Genetic Algorithm Helper (GA)
|
185 |
+
def complex_genetic_algorithm(
|
186 |
+
fitness_function,
|
187 |
+
population_size=100,
|
188 |
+
generations=50,
|
189 |
+
mutation_rate=0.1,
|
190 |
+
crossover_rate=0.8
|
191 |
+
):
|
192 |
+
population = np.random.rand(population_size, 10)
|
193 |
+
for generation in range(generations):
|
194 |
+
fitness_scores = np.array([fitness_function(ind) for ind in population])
|
195 |
+
selected = population[np.argsort(fitness_scores)[-10:]]
|
196 |
+
|
197 |
+
# Crossover
|
198 |
+
offspring = []
|
199 |
+
for _ in range(population_size - len(selected)):
|
200 |
+
if np.random.rand() < crossover_rate:
|
201 |
+
parents = selected[np.random.choice(len(selected), 2, replace=False)]
|
202 |
+
crossover_point = np.random.randint(1, len(parents[0]))
|
203 |
+
child = np.concatenate((parents[0][:crossover_point], parents[1][crossover_point:]))
|
204 |
+
offspring.append(child)
|
205 |
+
else:
|
206 |
+
offspring.append(np.random.rand(10))
|
207 |
+
|
208 |
+
# Mutation
|
209 |
+
offspring = np.array(offspring)
|
210 |
+
mutations = np.random.rand(*offspring.shape) < mutation_rate
|
211 |
+
offspring[mutations] += np.random.normal(0, 0.1, offspring[mutations].shape)
|
212 |
+
|
213 |
+
population = np.vstack((selected, offspring))
|
214 |
+
return population[np.argmax([fitness_function(ind) for ind in population])]
|
215 |
+
|
216 |
+
# Complex Phi Model
|
217 |
+
class ComplexPhiModel(nn.Module):
|
218 |
+
def __init__(self, input_size, hidden_size, output_size):
|
219 |
+
super(ComplexPhiModel, self).__init__()
|
220 |
+
self.layers = nn.Sequential(
|
221 |
+
nn.Linear(input_size, hidden_size),
|
222 |
+
nn.SELU(),
|
223 |
+
nn.BatchNorm1d(hidden_size),
|
224 |
+
nn.Dropout(0.4),
|
225 |
+
nn.Linear(hidden_size, hidden_size // 2),
|
226 |
+
nn.SELU(),
|
227 |
+
nn.BatchNorm1d(hidden_size // 2),
|
228 |
+
nn.Dropout(0.4),
|
229 |
+
nn.Linear(hidden_size // 2, output_size)
|
230 |
+
)
|
231 |
+
|
232 |
+
def forward(self, x):
|
233 |
+
return self.layers(x)
|
234 |
+
|
235 |
+
|
236 |
+
|
237 |
def respond(
|
238 |
message,
|
239 |
history: list[tuple[str, str]],
|