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