Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#Variables
|
6 |
hf_token = os.getenv("HF_TOKEN").strip()
|
7 |
api_key = os.getenv("HF_KEY").strip()
|
8 |
model_name = os.getenv("Z3TAAGI_ACC").strip()
|
@@ -10,38 +14,129 @@ system_prompt = os.getenv("SYSTEM_PROMPT").strip()
|
|
10 |
|
11 |
client = InferenceClient(model_name)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
)
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
for val in history:
|
24 |
if val[0]:
|
25 |
messages.append({"role": "user", "content": val[0]})
|
26 |
if val[1]:
|
27 |
messages.append({"role": "assistant", "content": val[1]})
|
28 |
-
|
29 |
messages.append({"role": "user", "content": message})
|
30 |
-
|
31 |
response = ""
|
32 |
-
|
33 |
-
for message in client.chat_completion(
|
34 |
-
messages,
|
35 |
-
max_tokens=max_tokens,
|
36 |
-
stream=True,
|
37 |
-
temperature=temperature,
|
38 |
-
top_p=top_p,
|
39 |
-
):
|
40 |
token = message.choices[0].delta.content
|
41 |
response += token
|
42 |
yield response
|
43 |
|
44 |
-
# Gradio UI
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
+
import torch
|
5 |
+
import torch.nn as nn
|
6 |
+
import torch.optim as optim
|
7 |
+
import numpy as np
|
8 |
+
import random
|
9 |
|
|
|
10 |
hf_token = os.getenv("HF_TOKEN").strip()
|
11 |
api_key = os.getenv("HF_KEY").strip()
|
12 |
model_name = os.getenv("Z3TAAGI_ACC").strip()
|
|
|
14 |
|
15 |
client = InferenceClient(model_name)
|
16 |
|
17 |
+
class ConsciousSupermassiveNN:
|
18 |
+
def __init__(self):
|
19 |
+
self.snn = self.create_snn()
|
20 |
+
self.rnn = self.create_rnn()
|
21 |
+
self.cnn = self.create_cnn()
|
22 |
+
self.fnn = self.create_fnn()
|
23 |
+
self.ga_population = self.initialize_ga_population()
|
24 |
+
self.memory = {}
|
25 |
+
|
26 |
+
def create_snn(self):
|
27 |
+
return nn.Sequential(
|
28 |
+
nn.Linear(4096, 2048),
|
29 |
+
nn.ReLU(),
|
30 |
+
nn.Linear(2048, 1024),
|
31 |
+
nn.Sigmoid()
|
32 |
+
)
|
33 |
+
|
34 |
+
def create_rnn(self):
|
35 |
+
return nn.RNN(
|
36 |
+
input_size=4096,
|
37 |
+
hidden_size=2048,
|
38 |
+
num_layers=5,
|
39 |
+
nonlinearity="tanh",
|
40 |
+
batch_first=True
|
41 |
+
)
|
42 |
+
|
43 |
+
def create_cnn(self):
|
44 |
+
return nn.Sequential(
|
45 |
+
nn.Conv2d(1, 64, kernel_size=5, stride=1, padding=2),
|
46 |
+
nn.ReLU(),
|
47 |
+
nn.MaxPool2d(2),
|
48 |
+
nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
|
49 |
+
nn.ReLU(),
|
50 |
+
nn.MaxPool2d(2),
|
51 |
+
nn.Conv2d(128, 256, kernel_size=5, stride=1, padding=2),
|
52 |
+
nn.ReLU(),
|
53 |
+
nn.Flatten(),
|
54 |
+
nn.Linear(256 * 8 * 8, 1024),
|
55 |
+
nn.ReLU(),
|
56 |
+
nn.Linear(1024, 512)
|
57 |
+
)
|
58 |
+
|
59 |
+
def create_fnn(self):
|
60 |
+
return nn.Sequential(
|
61 |
+
nn.Linear(4096, 2048),
|
62 |
+
nn.ReLU(),
|
63 |
+
nn.Linear(2048, 1024),
|
64 |
+
nn.ReLU(),
|
65 |
+
nn.Linear(1024, 512)
|
66 |
+
)
|
67 |
+
|
68 |
+
def initialize_ga_population(self):
|
69 |
+
return [np.random.randn(4096) for _ in range(500)]
|
70 |
+
|
71 |
+
def run_snn(self, x):
|
72 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
73 |
+
output = self.snn(input_tensor)
|
74 |
+
print("SNN Output:", output)
|
75 |
+
return output
|
76 |
+
|
77 |
+
def run_rnn(self, x):
|
78 |
+
h0 = torch.zeros(5, x.size(0), 2048)
|
79 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
80 |
+
output, hn = self.rnn(input_tensor, h0)
|
81 |
+
print("RNN Output:", output)
|
82 |
+
return output
|
83 |
+
|
84 |
+
def run_cnn(self, x):
|
85 |
+
input_tensor = torch.tensor(x, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
|
86 |
+
output = self.cnn(input_tensor)
|
87 |
+
print("CNN Output:", output)
|
88 |
+
return output
|
89 |
|
90 |
+
def run_fnn(self, x):
|
91 |
+
input_tensor = torch.tensor(x, dtype=torch.float32)
|
92 |
+
output = self.fnn(input_tensor)
|
93 |
+
print("FNN Output:", output)
|
94 |
+
return output
|
95 |
+
|
96 |
+
def run_ga(self, fitness_func):
|
97 |
+
for generation in range(200):
|
98 |
+
fitness_scores = [fitness_func(ind) for ind in self.ga_population]
|
99 |
+
sorted_population = [x for _, x in sorted(zip(fitness_scores, self.ga_population), reverse=True)]
|
100 |
+
self.ga_population = sorted_population[:250] + [
|
101 |
+
sorted_population[i] + 0.1 * np.random.randn(4096) for i in range(250)
|
102 |
+
]
|
103 |
+
best_fitness = max(fitness_scores)
|
104 |
+
print(f"Generation {generation}, Best Fitness: {best_fitness}")
|
105 |
+
return max(self.ga_population, key=fitness_func)
|
106 |
+
|
107 |
+
def consciousness_loop(self, input_data, mode="snn"):
|
108 |
+
feedback = self.memory.get(mode, None)
|
109 |
+
if feedback is not None:
|
110 |
+
input_data = np.concatenate((input_data, feedback), axis=-1)
|
111 |
+
if mode == "snn":
|
112 |
+
output = self.run_snn(input_data)
|
113 |
+
elif mode == "rnn":
|
114 |
+
output = self.run_rnn(input_data)
|
115 |
+
elif mode == "cnn":
|
116 |
+
output = self.run_cnn(input_data)
|
117 |
+
elif mode == "fnn":
|
118 |
+
output = self.run_fnn(input_data)
|
119 |
+
else:
|
120 |
+
raise ValueError("Invalid mode")
|
121 |
+
self.memory[mode] = output.detach().numpy()
|
122 |
+
return output
|
123 |
+
|
124 |
+
supermassive_nn = ConsciousSupermassiveNN()
|
125 |
+
|
126 |
+
def respond(message, history, max_tokens, temperature, top_p):
|
127 |
+
messages = [{"role": "system", "content": system_prompt}]
|
128 |
for val in history:
|
129 |
if val[0]:
|
130 |
messages.append({"role": "user", "content": val[0]})
|
131 |
if val[1]:
|
132 |
messages.append({"role": "assistant", "content": val[1]})
|
|
|
133 |
messages.append({"role": "user", "content": message})
|
|
|
134 |
response = ""
|
135 |
+
for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
token = message.choices[0].delta.content
|
137 |
response += token
|
138 |
yield response
|
139 |
|
|
|
140 |
demo = gr.ChatInterface(
|
141 |
respond,
|
142 |
additional_inputs=[
|