Spaces:
Runtime error
Runtime error
AXD
Browse files
app.py
CHANGED
|
@@ -1,151 +1,74 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
import
|
| 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 |
-
data=filtered_df(),
|
| 76 |
-
x=input.xvar(),
|
| 77 |
-
y=input.yvar(),
|
| 78 |
-
palette=palette,
|
| 79 |
-
hue="Species" if input.by_species() else None,
|
| 80 |
-
hue_order=species,
|
| 81 |
-
legend=False,
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
@output
|
| 85 |
-
@render.ui
|
| 86 |
-
def value_boxes():
|
| 87 |
-
df = filtered_df()
|
| 88 |
-
|
| 89 |
-
def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
|
| 90 |
-
return ui.value_box(
|
| 91 |
-
title,
|
| 92 |
-
count,
|
| 93 |
-
{"class_": "pt-1 pb-0"},
|
| 94 |
-
showcase=ui.fill.as_fill_item(
|
| 95 |
-
ui.tags.img(
|
| 96 |
-
{"style": "object-fit:contain;"},
|
| 97 |
-
src=showcase_img,
|
| 98 |
-
)
|
| 99 |
-
),
|
| 100 |
-
theme_color=None,
|
| 101 |
-
style=f"background-color: {bgcol};",
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
if not input.by_species():
|
| 105 |
-
return penguin_value_box(
|
| 106 |
-
"Penguins",
|
| 107 |
-
len(df.index),
|
| 108 |
-
bg_palette["default"],
|
| 109 |
-
# Artwork by @allison_horst
|
| 110 |
-
showcase_img="penguins.png",
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
value_boxes = [
|
| 114 |
-
penguin_value_box(
|
| 115 |
-
name,
|
| 116 |
-
len(df[df["Species"] == name]),
|
| 117 |
-
bg_palette[name],
|
| 118 |
-
# Artwork by @allison_horst
|
| 119 |
-
showcase_img=f"{name}.png",
|
| 120 |
-
)
|
| 121 |
-
for name in species
|
| 122 |
-
# Only include boxes for _selected_ species
|
| 123 |
-
if name in input.species()
|
| 124 |
-
]
|
| 125 |
-
|
| 126 |
-
return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# "darkorange", "purple", "cyan4"
|
| 130 |
-
colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
|
| 131 |
-
colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
|
| 132 |
-
|
| 133 |
-
palette: Dict[str, Tuple[float, float, float]] = {
|
| 134 |
-
"Adelie": colors[0],
|
| 135 |
-
"Chinstrap": colors[1],
|
| 136 |
-
"Gentoo": colors[2],
|
| 137 |
-
"default": sns.color_palette()[0], # type: ignore
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
-
bg_palette = {}
|
| 141 |
-
# Use `sns.set_style("whitegrid")` to help find approx alpha value
|
| 142 |
-
for name, col in palette.items():
|
| 143 |
-
# Adjusted n_colors until `axe` accessibility did not complain about color contrast
|
| 144 |
-
bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
app = App(
|
| 148 |
-
app_ui,
|
| 149 |
-
server,
|
| 150 |
-
static_assets=str(www_dir),
|
| 151 |
-
)
|
|
|
|
| 1 |
+
# Aseg煤rate de ejecutar esto en un entorno de Google Colab
|
| 2 |
+
|
| 3 |
+
!pip install transformers torch
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from torch import nn
|
| 7 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 8 |
+
|
| 9 |
+
class LiquidLayer(nn.Module):
|
| 10 |
+
def __init__(self, input_size, hidden_size):
|
| 11 |
+
super(LiquidLayer, self).__init__()
|
| 12 |
+
self.input_size = input_size
|
| 13 |
+
self.hidden_size = hidden_size
|
| 14 |
+
self.wx = nn.Linear(input_size, hidden_size)
|
| 15 |
+
self.wh = nn.Linear(hidden_size, hidden_size)
|
| 16 |
+
self.activation = nn.Tanh()
|
| 17 |
+
|
| 18 |
+
def forward(self, x, prev_state):
|
| 19 |
+
new_state = self.activation(self.wx(x) + self.wh(prev_state))
|
| 20 |
+
return new_state
|
| 21 |
+
|
| 22 |
+
class LiquidGPT2(nn.Module):
|
| 23 |
+
def __init__(self, gpt2_model, liquid_size):
|
| 24 |
+
super(LiquidGPT2, self).__init__()
|
| 25 |
+
self.gpt2 = gpt2_model
|
| 26 |
+
self.liquid_layer = LiquidLayer(self.gpt2.config.n_embd, liquid_size)
|
| 27 |
+
self.memory = torch.zeros(1, liquid_size)
|
| 28 |
+
|
| 29 |
+
def forward(self, input_ids, attention_mask=None):
|
| 30 |
+
# Set output_hidden_states to True to get the last hidden state
|
| 31 |
+
gpt2_output = self.gpt2(input_ids, attention_mask=attention_mask, output_hidden_states=True)
|
| 32 |
+
last_hidden_state = gpt2_output.hidden_states[-1][:, -1, :] # Access last hidden state correctly
|
| 33 |
+
liquid_output = self.liquid_layer(last_hidden_state, self.memory)
|
| 34 |
+
self.memory = liquid_output.detach()
|
| 35 |
+
return self.gpt2.lm_head(liquid_output)
|
| 36 |
+
|
| 37 |
+
# Cargar el modelo GPT-2 y el tokenizador
|
| 38 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
| 39 |
+
gpt2_model = GPT2LMHeadModel.from_pretrained('gpt2')
|
| 40 |
+
|
| 41 |
+
# Crear el modelo LiquidGPT2
|
| 42 |
+
liquid_size = 768 # Mismo tama帽o que las embeddings de GPT-2
|
| 43 |
+
model = LiquidGPT2(gpt2_model, liquid_size)
|
| 44 |
+
|
| 45 |
+
# Funci贸n para generar respuestas
|
| 46 |
+
def generate_response(prompt, model, tokenizer, max_length=50):
|
| 47 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
| 48 |
+
attention_mask = torch.ones(input_ids.shape, dtype=torch.long)
|
| 49 |
+
|
| 50 |
+
output = model(input_ids, attention_mask=attention_mask)
|
| 51 |
+
|
| 52 |
+
# Generar tokens
|
| 53 |
+
generated = tokenizer.decode(torch.argmax(output, dim=-1).squeeze())
|
| 54 |
+
return generated
|
| 55 |
+
|
| 56 |
+
# Ejemplo de uso
|
| 57 |
+
prompt = "Hola, 驴c贸mo est谩s?"
|
| 58 |
+
response = generate_response(prompt, model, tokenizer)
|
| 59 |
+
print(f"Prompt: {prompt}")
|
| 60 |
+
print(f"Respuesta: {response}")
|
| 61 |
+
|
| 62 |
+
# Funci贸n de chat interactivo
|
| 63 |
+
def chat():
|
| 64 |
+
print("隆Hola! Soy un chatbot basado en GPT-2 con una capa l铆quida. Escribe 'salir' para terminar.")
|
| 65 |
+
while True:
|
| 66 |
+
user_input = input("T煤: ")
|
| 67 |
+
if user_input.lower() == 'salir':
|
| 68 |
+
print("隆Hasta luego!")
|
| 69 |
+
break
|
| 70 |
+
response = generate_response(user_input, model, tokenizer)
|
| 71 |
+
print(f"ChatBot: {response}")
|
| 72 |
+
|
| 73 |
+
# Iniciar chat
|
| 74 |
+
chat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|