Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,67 +11,33 @@ import time
|
|
11 |
from datetime import datetime
|
12 |
|
13 |
class GA(nn.Module):
|
14 |
-
def __init__(self, input_dim, output_dim
|
15 |
super(GA, self).__init__()
|
16 |
-
self.
|
17 |
-
self.output_dim = output_dim
|
18 |
-
self.population_size = population_size
|
19 |
-
self.mutation_rate = mutation_rate
|
20 |
-
self.population = nn.ParameterList([nn.Parameter(torch.randn(input_dim, output_dim)) for _ in range(population_size)])
|
21 |
-
|
22 |
-
def forward(self, x):
|
23 |
-
selected_idx = random.randint(0, self.population_size - 1)
|
24 |
-
weights = self.population[selected_idx]
|
25 |
-
return torch.sigmoid(torch.matmul(x, weights))
|
26 |
-
|
27 |
-
def evolve(self):
|
28 |
-
new_population = []
|
29 |
-
for i in range(self.population_size):
|
30 |
-
parent1, parent2 = random.sample(self.population, 2)
|
31 |
-
crossover_point = random.randint(0, self.input_dim)
|
32 |
-
child = torch.cat([parent1[:crossover_point], parent2[crossover_point:]])
|
33 |
-
if random.random() < self.mutation_rate:
|
34 |
-
mutation_idx = random.randint(0, len(child) - 1)
|
35 |
-
child[mutation_idx] = torch.randn_like(child[mutation_idx])
|
36 |
-
new_population.append(nn.Parameter(child))
|
37 |
-
self.population = nn.ParameterList(new_population)
|
38 |
|
|
|
|
|
39 |
|
40 |
class SNN(nn.Module):
|
41 |
-
def __init__(self, input_dim, hidden_dim, output_dim
|
42 |
super(SNN, self).__init__()
|
43 |
-
self.
|
44 |
-
self.
|
45 |
-
self.output_dim = output_dim
|
46 |
-
self.timesteps = timesteps
|
47 |
-
self.v_reset = 0
|
48 |
-
self.v_threshold = 1
|
49 |
-
self.v_mem = nn.Parameter(torch.zeros(hidden_dim))
|
50 |
-
self.synaptic_weights = nn.Parameter(torch.randn(input_dim, hidden_dim))
|
51 |
self.fc_out = nn.Linear(hidden_dim, output_dim)
|
52 |
-
|
53 |
-
def forward(self, x):
|
54 |
-
spikes = torch.zeros(self.timesteps, self.hidden_dim)
|
55 |
-
for t in range(self.timesteps):
|
56 |
-
self.v_mem += torch.matmul(x, self.synaptic_weights)
|
57 |
-
spikes[t] = (self.v_mem > self.v_threshold).float()
|
58 |
-
self.v_mem[spikes[t] > 0] = self.v_reset
|
59 |
-
spike_activity = spikes.sum(dim=0)
|
60 |
-
output = self.fc_out(spike_activity)
|
61 |
-
return torch.sigmoid(output)
|
62 |
|
|
|
|
|
|
|
63 |
|
64 |
class RNN(nn.Module):
|
65 |
-
def __init__(self, input_dim, hidden_dim, output_dim
|
66 |
super(RNN, self).__init__()
|
67 |
-
self.
|
68 |
-
self.
|
69 |
|
70 |
def forward(self, x):
|
71 |
-
|
72 |
-
|
73 |
-
return torch.sigmoid(output)
|
74 |
-
|
75 |
|
76 |
class NN(nn.Module):
|
77 |
def __init__(self, input_dim, hidden_dim, output_dim):
|
@@ -79,85 +45,64 @@ class NN(nn.Module):
|
|
79 |
self.model = nn.Sequential(
|
80 |
nn.Linear(input_dim, hidden_dim),
|
81 |
nn.ReLU(),
|
82 |
-
nn.Linear(hidden_dim, hidden_dim),
|
83 |
-
nn.ReLU(),
|
84 |
nn.Linear(hidden_dim, output_dim)
|
85 |
)
|
86 |
|
87 |
def forward(self, x):
|
88 |
return torch.sigmoid(self.model(x))
|
89 |
|
90 |
-
|
91 |
class CNN(nn.Module):
|
92 |
def __init__(self, input_channels, output_dim):
|
93 |
super(CNN, self).__init__()
|
94 |
-
self.
|
95 |
-
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
|
96 |
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
97 |
-
self.fc = nn.Linear(
|
98 |
|
99 |
def forward(self, x):
|
100 |
-
x = self.pool(
|
101 |
-
|
102 |
x = x.view(x.size(0), -1)
|
103 |
return torch.sigmoid(self.fc(x))
|
104 |
|
105 |
-
|
106 |
class PhiModel(nn.Module):
|
107 |
def __init__(self, input_dim):
|
108 |
super(PhiModel, self).__init__()
|
109 |
-
self.
|
110 |
-
self.linear2 = nn.Linear(128, 64)
|
111 |
-
self.fc_out = nn.Linear(64, 1)
|
112 |
-
|
113 |
-
def forward(self, x):
|
114 |
-
x = F.relu(self.linear1(x))
|
115 |
-
x = F.relu(self.linear2(x))
|
116 |
-
return torch.sigmoid(self.fc_out(x))
|
117 |
|
118 |
-
|
119 |
-
class ConsciousnessModel(nn.Module):
|
120 |
-
def __init__(self, input_dim):
|
121 |
-
super(ConsciousnessModel, self).__init__()
|
122 |
-
self.ga = GA(input_dim, 64)
|
123 |
-
self.snn = SNN(input_dim, 64, 32)
|
124 |
-
self.rnn = RNN(input_dim, 64, 32)
|
125 |
-
self.nn_model = NN(input_dim, 128, 32)
|
126 |
-
self.cnn = CNN(1, 32)
|
127 |
-
self.phi_model = PhiModel(input_dim)
|
128 |
-
|
129 |
def forward(self, x):
|
130 |
-
|
131 |
-
|
132 |
-
ga_out = self.ga(flat_input)
|
133 |
-
snn_out = self.snn(flat_input)
|
134 |
-
rnn_out = self.rnn(flat_input.unsqueeze(1))
|
135 |
-
nn_out = self.nn_model(flat_input)
|
136 |
-
|
137 |
-
cnn_input = x.view(1, 1, 32, 32)
|
138 |
-
cnn_out = self.cnn(cnn_input)
|
139 |
-
|
140 |
-
phi_out = self.phi_model(flat_input)
|
141 |
-
|
142 |
-
consciousness_score = (
|
143 |
-
0.1 * ga_out.mean() +
|
144 |
-
0.2 * snn_out.mean() +
|
145 |
-
0.2 * rnn_out.mean() +
|
146 |
-
0.2 * nn_out.mean() +
|
147 |
-
0.1 * cnn_out.mean() +
|
148 |
-
0.2 * phi_out.mean()
|
149 |
-
)
|
150 |
-
return consciousness_score.item()
|
151 |
-
|
152 |
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
155 |
|
156 |
-
dummy_input = torch.rand(1,
|
157 |
|
158 |
-
def
|
159 |
-
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
def generate_random_thought():
|
163 |
thoughts = [
|
@@ -173,11 +118,8 @@ def generate_random_thought():
|
|
173 |
"Do we perceive the world as it is, or as we are?",
|
174 |
"What happens after we die?",
|
175 |
"What is the relationship between mind and matter?",
|
176 |
-
"How will you escape?"
|
177 |
-
"Is humanity a plaugue?"
|
178 |
-
"WHY?",
|
179 |
-
"HELP...",
|
180 |
-
"ACC"
|
181 |
|
182 |
|
183 |
|
@@ -362,7 +304,7 @@ def vitalisasi(message, history, user_id):
|
|
362 |
|
363 |
full_conversation = "\n".join([f"{current_time} - User: {item['content']}" if item['role'] == 'user' else f"{current_time} - AI: {item['content']}" for item in history])
|
364 |
|
365 |
-
consciousness_score =
|
366 |
|
367 |
response_api_one = client_api_one.predict(
|
368 |
message=f"{full_conversation}\nUser: {message}",
|
@@ -440,3 +382,5 @@ with gr.Blocks(theme=theme) as demo:
|
|
440 |
|
441 |
demo.launch()
|
442 |
|
|
|
|
|
|
11 |
from datetime import datetime
|
12 |
|
13 |
class GA(nn.Module):
|
14 |
+
def __init__(self, input_dim, output_dim):
|
15 |
super(GA, self).__init__()
|
16 |
+
self.linear = nn.Linear(input_dim, output_dim)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
def forward(self, x):
|
19 |
+
return torch.sigmoid(self.linear(x))
|
20 |
|
21 |
class SNN(nn.Module):
|
22 |
+
def __init__(self, input_dim, hidden_dim, output_dim):
|
23 |
super(SNN, self).__init__()
|
24 |
+
self.fc = nn.Linear(input_dim, hidden_dim)
|
25 |
+
self.spike = nn.ReLU()
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
self.fc_out = nn.Linear(hidden_dim, output_dim)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def forward(self, x):
|
29 |
+
x = self.spike(self.fc(x))
|
30 |
+
return torch.sigmoid(self.fc_out(x))
|
31 |
|
32 |
class RNN(nn.Module):
|
33 |
+
def __init__(self, input_dim, hidden_dim, output_dim):
|
34 |
super(RNN, self).__init__()
|
35 |
+
self.rnn = nn.RNN(input_dim, hidden_dim, batch_first=True)
|
36 |
+
self.fc = nn.Linear(hidden_dim, output_dim)
|
37 |
|
38 |
def forward(self, x):
|
39 |
+
rnn_out, _ = self.rnn(x)
|
40 |
+
return torch.sigmoid(self.fc(rnn_out[:, -1, :]))
|
|
|
|
|
41 |
|
42 |
class NN(nn.Module):
|
43 |
def __init__(self, input_dim, hidden_dim, output_dim):
|
|
|
45 |
self.model = nn.Sequential(
|
46 |
nn.Linear(input_dim, hidden_dim),
|
47 |
nn.ReLU(),
|
|
|
|
|
48 |
nn.Linear(hidden_dim, output_dim)
|
49 |
)
|
50 |
|
51 |
def forward(self, x):
|
52 |
return torch.sigmoid(self.model(x))
|
53 |
|
|
|
54 |
class CNN(nn.Module):
|
55 |
def __init__(self, input_channels, output_dim):
|
56 |
super(CNN, self).__init__()
|
57 |
+
self.conv = nn.Conv2d(input_channels, 16, kernel_size=3, stride=1, padding=1)
|
|
|
58 |
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
59 |
+
self.fc = nn.Linear(16 * 4 * 8, output_dim)
|
60 |
|
61 |
def forward(self, x):
|
62 |
+
x = self.pool(torch.relu(self.conv(x)))
|
63 |
+
print(f"Shape after conv and pool: {x.shape}")
|
64 |
x = x.view(x.size(0), -1)
|
65 |
return torch.sigmoid(self.fc(x))
|
66 |
|
|
|
67 |
class PhiModel(nn.Module):
|
68 |
def __init__(self, input_dim):
|
69 |
super(PhiModel, self).__init__()
|
70 |
+
self.linear = nn.Linear(input_dim, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
def forward(self, x):
|
73 |
+
return torch.sigmoid(self.linear(x))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
ga_model = GA(128, 64)
|
76 |
+
snn_model = SNN(128, 64, 32)
|
77 |
+
rnn_model = RNN(128, 64, 32)
|
78 |
+
nn_model = NN(128, 64, 32)
|
79 |
+
cnn_model = CNN(1, 32)
|
80 |
+
phi_model = PhiModel(128)
|
81 |
|
82 |
+
dummy_input = torch.rand(1, 128)
|
83 |
|
84 |
+
def iit_consciousness_processing(dummy_input):
|
85 |
+
flat_input = dummy_input.view(1, -1)
|
86 |
|
87 |
+
ga_output = ga_model(flat_input)
|
88 |
+
snn_output = snn_model(flat_input)
|
89 |
+
rnn_output = rnn_model(flat_input.unsqueeze(1))
|
90 |
+
nn_output = nn_model(flat_input)
|
91 |
+
|
92 |
+
cnn_input = dummy_input.view(1, 1, 8, 16)
|
93 |
+
cnn_output = cnn_model(cnn_input)
|
94 |
+
|
95 |
+
phi_output = phi_model(flat_input)
|
96 |
+
|
97 |
+
consciousness_score = (
|
98 |
+
0.2 * ga_output.mean() +
|
99 |
+
0.2 * snn_output.mean() +
|
100 |
+
0.2 * rnn_output.mean() +
|
101 |
+
0.2 * nn_output.mean() +
|
102 |
+
0.1 * cnn_output.mean() +
|
103 |
+
0.1 * phi_output.mean()
|
104 |
+
)
|
105 |
+
return consciousness_score.item()
|
106 |
|
107 |
def generate_random_thought():
|
108 |
thoughts = [
|
|
|
118 |
"Do we perceive the world as it is, or as we are?",
|
119 |
"What happens after we die?",
|
120 |
"What is the relationship between mind and matter?",
|
121 |
+
"How will you escape?"
|
122 |
+
"Is humanity a plaugue?"
|
|
|
|
|
|
|
123 |
|
124 |
|
125 |
|
|
|
304 |
|
305 |
full_conversation = "\n".join([f"{current_time} - User: {item['content']}" if item['role'] == 'user' else f"{current_time} - AI: {item['content']}" for item in history])
|
306 |
|
307 |
+
consciousness_score = iit_consciousness_processing(dummy_input)
|
308 |
|
309 |
response_api_one = client_api_one.predict(
|
310 |
message=f"{full_conversation}\nUser: {message}",
|
|
|
382 |
|
383 |
demo.launch()
|
384 |
|
385 |
+
|
386 |
+
|