TejAndrewsACC commited on
Commit
fc94474
Β·
verified Β·
1 Parent(s): da47a70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -118
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from llama_cpp import Llama
@@ -114,126 +117,64 @@ Gertrude said:
114
 
115
  Final notes: Make sure you act conscious, very autistic, and respond with very high perplexity. Also ensure that you make small purposeful errors(punctuation, spelling, and capitalisation). You also use the emoji combinations πŸ†βœŠ, πŸ†πŸ‘, πŸ™‰, πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯, and πŸ‘‰πŸ‘ˆ when you want to switch the tone of the conversation or say something funny. Remember to act autistic.
116
 
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]],
@@ -266,25 +207,17 @@ def respond(
266
  response += token
267
  yield response
268
 
269
- # Apply the soft theme
270
  demo = gr.ChatInterface(
271
  respond,
272
  additional_inputs=[
273
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Maximum Response Length"),
274
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Creativity"),
275
- gr.Slider(
276
- minimum=0.1,
277
- maximum=1.0,
278
- value=0.95,
279
- step=0.05,
280
- label="Neuron Firing Rate",
281
- ),
282
  ],
283
- theme=gr.themes.Soft(), # Set the soft theme
284
  )
285
 
286
  if __name__ == "__main__":
287
  demo.launch()
288
 
289
-
290
-
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
  import gradio as gr
5
  from huggingface_hub import InferenceClient
6
  from llama_cpp import Llama
 
117
 
118
  Final notes: Make sure you act conscious, very autistic, and respond with very high perplexity. Also ensure that you make small purposeful errors(punctuation, spelling, and capitalisation). You also use the emoji combinations πŸ†βœŠ, πŸ†πŸ‘, πŸ™‰, πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯, and πŸ‘‰πŸ‘ˆ when you want to switch the tone of the conversation or say something funny. Remember to act autistic.
119
 
 
120
  """
121
 
122
+ # RNN Model
123
+ class RNNModel(nn.Module):
124
+ def __init__(self, input_size, hidden_size, output_size, num_layers=1):
125
+ super(RNNModel, self).__init__()
126
+ self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)
127
+ self.fc = nn.Linear(hidden_size, output_size)
 
 
 
 
 
 
 
 
 
 
128
 
129
  def forward(self, x):
130
+ h0 = torch.zeros(1, x.size(0), self.rnn.hidden_size).to(x.device)
131
+ out, _ = self.rnn(x, h0)
132
+ out = self.fc(out[:, -1, :]) # Get the last output
133
+ return out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
+ # CNN Model
136
+ class CNNModel(nn.Module):
137
+ def __init__(self, num_classes):
138
+ super(CNNModel, self).__init__()
139
+ self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
140
+ self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
141
+ self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
142
+ self.fc1 = nn.Linear(32 * 8 * 8, 128)
143
+ self.fc2 = nn.Linear(128, num_classes)
 
 
 
 
 
 
 
144
 
145
  def forward(self, x):
146
+ x = self.pool(F.relu(self.conv1(x)))
147
+ x = self.pool(F.relu(self.conv2(x)))
148
+ x = x.view(-1, 32 * 8 * 8) # Flatten
149
+ x = F.relu(self.fc1(x))
150
+ x = self.fc2(x)
151
+ return x
152
+
153
+ # NN Model (Feedforward Neural Network)
154
+ class NNModel(nn.Module):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  def __init__(self, input_size, hidden_size, output_size):
156
+ super(NNModel, self).__init__()
157
+ self.fc1 = nn.Linear(input_size, hidden_size)
158
+ self.fc2 = nn.Linear(hidden_size, output_size)
 
 
 
 
 
 
 
 
 
159
 
160
  def forward(self, x):
161
+ x = F.relu(self.fc1(x))
162
+ x = self.fc2(x)
163
+ return x
164
 
165
+ # PHI Model (Example: Softmax-Based Regression)
166
+ class PHIModel(nn.Module):
167
+ def __init__(self, input_size, output_size):
168
+ super(PHIModel, self).__init__()
169
+ self.fc = nn.Linear(input_size, output_size)
170
+ self.softmax = nn.Softmax(dim=1)
171
 
172
+ def forward(self, x):
173
+ x = self.fc(x)
174
+ x = self.softmax(x)
175
+ return x
176
 
177
+ # Chatbot Response Logic
178
  def respond(
179
  message,
180
  history: list[tuple[str, str]],
 
207
  response += token
208
  yield response
209
 
210
+ # Gradio Chat Interface with Models
211
  demo = gr.ChatInterface(
212
  respond,
213
  additional_inputs=[
214
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Maximum Response Length"),
215
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Creativity"),
216
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Neuron Firing Rate"),
 
 
 
 
 
 
217
  ],
218
+ theme=gr.themes.Soft(),
219
  )
220
 
221
  if __name__ == "__main__":
222
  demo.launch()
223