Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import pickle
|
|
6 |
import numpy as np
|
7 |
import torch.nn.functional as F
|
8 |
from accelerate import init_empty_weights, infer_auto_device_map, load_checkpoint_and_dispatch
|
|
|
9 |
|
10 |
# ---- Constants and Setup ----
|
11 |
model_name = 'gpt2'
|
@@ -90,9 +91,90 @@ def chat_interface(user_input):
|
|
90 |
response = advanced_agi_chat(user_input)
|
91 |
return response
|
92 |
|
93 |
-
# ----
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
|
|
96 |
auth = ("Tej", "186281mps", "ACC", "HIPE")
|
97 |
|
98 |
with gr.Blocks() as app:
|
@@ -126,4 +208,3 @@ with gr.Blocks() as app:
|
|
126 |
|
127 |
# Launch the Gradio app
|
128 |
app.launch()
|
129 |
-
|
|
|
6 |
import numpy as np
|
7 |
import torch.nn.functional as F
|
8 |
from accelerate import init_empty_weights, infer_auto_device_map, load_checkpoint_and_dispatch
|
9 |
+
import gradio as gr
|
10 |
|
11 |
# ---- Constants and Setup ----
|
12 |
model_name = 'gpt2'
|
|
|
91 |
response = advanced_agi_chat(user_input)
|
92 |
return response
|
93 |
|
94 |
+
# ---- RNN Model ----
|
95 |
+
class RNNModel(nn.Module):
|
96 |
+
def __init__(self, input_size, hidden_size, output_size):
|
97 |
+
super(RNNModel, self).__init__()
|
98 |
+
self.hidden_size = hidden_size
|
99 |
+
self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
|
100 |
+
self.fc = nn.Linear(hidden_size, output_size)
|
101 |
+
|
102 |
+
def forward(self, x, hidden):
|
103 |
+
out, hidden = self.rnn(x, hidden)
|
104 |
+
out = self.fc(out[:, -1, :]) # Use last time-step
|
105 |
+
return out, hidden
|
106 |
+
|
107 |
+
def init_hidden(self, batch_size):
|
108 |
+
return torch.zeros(batch_size, self.hidden_size).to(device)
|
109 |
+
|
110 |
+
# ---- CNN Model ----
|
111 |
+
class CNNModel(nn.Module):
|
112 |
+
def __init__(self, input_channels, output_size):
|
113 |
+
super(CNNModel, self).__init__()
|
114 |
+
self.conv1 = nn.Conv2d(input_channels, 16, 3)
|
115 |
+
self.conv2 = nn.Conv2d(16, 32, 3)
|
116 |
+
self.fc = nn.Linear(32 * 6 * 6, output_size) # Assume input size is 28x28
|
117 |
+
|
118 |
+
def forward(self, x):
|
119 |
+
x = F.relu(F.max_pool2d(self.conv1(x), 2))
|
120 |
+
x = F.relu(F.max_pool2d(self.conv2(x), 2))
|
121 |
+
x = x.view(x.size(0), -1) # Flatten
|
122 |
+
x = self.fc(x)
|
123 |
+
return x
|
124 |
+
|
125 |
+
# ---- Neural Network (Feedforward) ----
|
126 |
+
class NNModel(nn.Module):
|
127 |
+
def __init__(self, input_size, hidden_size, output_size):
|
128 |
+
super(NNModel, self).__init__()
|
129 |
+
self.fc1 = nn.Linear(input_size, hidden_size)
|
130 |
+
self.fc2 = nn.Linear(hidden_size, output_size)
|
131 |
+
|
132 |
+
def forward(self, x):
|
133 |
+
x = F.relu(self.fc1(x))
|
134 |
+
x = self.fc2(x)
|
135 |
+
return x
|
136 |
+
|
137 |
+
# ---- PHI Model ----
|
138 |
+
class PHIModel(nn.Module):
|
139 |
+
def __init__(self, input_size, output_size):
|
140 |
+
super(PHIModel, self).__init__()
|
141 |
+
self.phi = (1 + np.sqrt(5)) / 2 # Golden Ratio
|
142 |
+
self.fc1 = nn.Linear(input_size, int(input_size * self.phi))
|
143 |
+
self.fc2 = nn.Linear(int(input_size * self.phi), output_size)
|
144 |
+
|
145 |
+
def forward(self, x):
|
146 |
+
x = F.relu(self.fc1(x))
|
147 |
+
x = self.fc2(x)
|
148 |
+
return x
|
149 |
+
|
150 |
+
# ---- Genetic Algorithm (GA) ----
|
151 |
+
def ga_optimization(population, generations, mutation_rate):
|
152 |
+
def fitness_function(individual):
|
153 |
+
return sum(individual) # Simple fitness: sum of individual genes
|
154 |
+
|
155 |
+
for gen in range(generations):
|
156 |
+
population.sort(key=fitness_function, reverse=True) # Sort by fitness
|
157 |
+
next_generation = population[:len(population)//2] # Keep top half
|
158 |
+
|
159 |
+
# Crossover: Create new individuals by combining genes
|
160 |
+
for i in range(len(population) // 2):
|
161 |
+
parent1 = next_generation[i]
|
162 |
+
parent2 = next_generation[len(population)//2 + i]
|
163 |
+
crossover_point = random.randint(1, len(parent1) - 1)
|
164 |
+
child = parent1[:crossover_point] + parent2[crossover_point:]
|
165 |
+
next_generation.append(child)
|
166 |
+
|
167 |
+
# Mutation: Randomly mutate genes
|
168 |
+
for individual in next_generation:
|
169 |
+
if random.random() < mutation_rate:
|
170 |
+
mutation_point = random.randint(0, len(individual) - 1)
|
171 |
+
individual[mutation_point] = random.randint(0, 1)
|
172 |
+
|
173 |
+
population = next_generation # Update population
|
174 |
+
|
175 |
+
return population[0] # Return the best individual
|
176 |
|
177 |
+
# ---- Gradio App Setup ----
|
178 |
auth = ("Tej", "186281mps", "ACC", "HIPE")
|
179 |
|
180 |
with gr.Blocks() as app:
|
|
|
208 |
|
209 |
# Launch the Gradio app
|
210 |
app.launch()
|
|