Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,100 @@ except ImportError:
|
|
| 16 |
return func
|
| 17 |
spaces = type('', (), {'GPU': dummy_gpu_decorator})()
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Update the load_model function
|
| 22 |
@spaces.GPU
|
|
@@ -79,4 +172,121 @@ async def gradio_generate(prompt, max_length, temperature, top_k):
|
|
| 79 |
output += token
|
| 80 |
yield output
|
| 81 |
|
| 82 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return func
|
| 17 |
spaces = type('', (), {'GPU': dummy_gpu_decorator})()
|
| 18 |
|
| 19 |
+
# Define the GPTConfig class
|
| 20 |
+
class GPTConfig:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
self.block_size = 1024
|
| 23 |
+
self.vocab_size = 50304
|
| 24 |
+
self.n_layer = 12
|
| 25 |
+
self.n_head = 12
|
| 26 |
+
self.n_embd = 768
|
| 27 |
+
|
| 28 |
+
# Define other necessary classes
|
| 29 |
+
class CausalSelfAttention(nn.Module):
|
| 30 |
+
def __init__(self, config):
|
| 31 |
+
super().__init__()
|
| 32 |
+
assert config.n_embd % config.n_head == 0
|
| 33 |
+
self.c_attn = nn.Linear(config.n_embd, 3 * config.n_embd)
|
| 34 |
+
self.c_proj = nn.Linear(config.n_embd, config.n_embd)
|
| 35 |
+
self.n_head = config.n_head
|
| 36 |
+
self.n_embd = config.n_embd
|
| 37 |
+
self.register_buffer("bias", torch.tril(torch.ones(config.block_size, config.block_size)).view(1, 1, config.block_size, config.block_size))
|
| 38 |
+
|
| 39 |
+
def forward(self, x):
|
| 40 |
+
B, T, C = x.size()
|
| 41 |
+
q, k, v = self.c_attn(x).split(self.n_embd, dim=2)
|
| 42 |
+
k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
|
| 43 |
+
q = q.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
|
| 44 |
+
v = v.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
|
| 45 |
+
y = F.scaled_dot_product_attention(q, k, v, attn_mask=None, dropout_p=0.0, is_causal=True)
|
| 46 |
+
y = y.transpose(1, 2).contiguous().view(B, T, C)
|
| 47 |
+
return self.c_proj(y)
|
| 48 |
+
|
| 49 |
+
class MLP(nn.Module):
|
| 50 |
+
def __init__(self, config):
|
| 51 |
+
super().__init__()
|
| 52 |
+
self.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd)
|
| 53 |
+
self.gelu = nn.GELU()
|
| 54 |
+
self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd)
|
| 55 |
+
|
| 56 |
+
def forward(self, x):
|
| 57 |
+
return self.c_proj(self.gelu(self.c_fc(x)))
|
| 58 |
+
|
| 59 |
+
class Block(nn.Module):
|
| 60 |
+
def __init__(self, config):
|
| 61 |
+
super().__init__()
|
| 62 |
+
self.ln_1 = nn.LayerNorm(config.n_embd)
|
| 63 |
+
self.attn = CausalSelfAttention(config)
|
| 64 |
+
self.ln_2 = nn.LayerNorm(config.n_embd)
|
| 65 |
+
self.mlp = MLP(config)
|
| 66 |
+
|
| 67 |
+
def forward(self, x):
|
| 68 |
+
x = x + self.attn(self.ln_1(x))
|
| 69 |
+
x = x + self.mlp(self.ln_2(x))
|
| 70 |
+
return x
|
| 71 |
+
|
| 72 |
+
class GPT(nn.Module):
|
| 73 |
+
def __init__(self, config):
|
| 74 |
+
super().__init__()
|
| 75 |
+
self.config = config
|
| 76 |
+
self.transformer = nn.ModuleDict(dict(
|
| 77 |
+
wte = nn.Embedding(config.vocab_size, config.n_embd),
|
| 78 |
+
wpe = nn.Embedding(config.block_size, config.n_embd),
|
| 79 |
+
h = nn.ModuleList([Block(config) for _ in range(config.n_layer)]),
|
| 80 |
+
ln_f = nn.LayerNorm(config.n_embd),
|
| 81 |
+
))
|
| 82 |
+
self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
|
| 83 |
+
self.transformer.wte.weight = self.lm_head.weight
|
| 84 |
+
self.apply(self._init_weights)
|
| 85 |
+
|
| 86 |
+
def _init_weights(self, module):
|
| 87 |
+
if isinstance(module, nn.Linear):
|
| 88 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 89 |
+
if module.bias is not None:
|
| 90 |
+
torch.nn.init.zeros_(module.bias)
|
| 91 |
+
elif isinstance(module, nn.Embedding):
|
| 92 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 93 |
+
|
| 94 |
+
def forward(self, idx, targets=None):
|
| 95 |
+
device = idx.device
|
| 96 |
+
b, t = idx.size()
|
| 97 |
+
assert t <= self.config.block_size, f"Cannot forward sequence of length {t}, block size is only {self.config.block_size}"
|
| 98 |
+
pos = torch.arange(0, t, dtype=torch.long, device=device).unsqueeze(0)
|
| 99 |
+
|
| 100 |
+
tok_emb = self.transformer.wte(idx)
|
| 101 |
+
pos_emb = self.transformer.wpe(pos)
|
| 102 |
+
x = tok_emb + pos_emb
|
| 103 |
+
for block in self.transformer.h:
|
| 104 |
+
x = block(x)
|
| 105 |
+
x = self.transformer.ln_f(x)
|
| 106 |
+
logits = self.lm_head(x)
|
| 107 |
+
|
| 108 |
+
loss = None
|
| 109 |
+
if targets is not None:
|
| 110 |
+
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
|
| 111 |
+
|
| 112 |
+
return logits, loss
|
| 113 |
|
| 114 |
# Update the load_model function
|
| 115 |
@spaces.GPU
|
|
|
|
| 172 |
output += token
|
| 173 |
yield output
|
| 174 |
|
| 175 |
+
# # Your existing imports and model code here...
|
| 176 |
+
|
| 177 |
+
css = """
|
| 178 |
+
<style>
|
| 179 |
+
body {
|
| 180 |
+
background-color: #0f1624;
|
| 181 |
+
color: #e0e0e0;
|
| 182 |
+
font-family: 'Courier New', monospace;
|
| 183 |
+
background-image:
|
| 184 |
+
radial-gradient(white, rgba(255,255,255,.2) 2px, transparent 40px),
|
| 185 |
+
radial-gradient(white, rgba(255,255,255,.15) 1px, transparent 30px),
|
| 186 |
+
radial-gradient(white, rgba(255,255,255,.1) 2px, transparent 40px),
|
| 187 |
+
radial-gradient(rgba(255,255,255,.4), rgba(255,255,255,.1) 2px, transparent 30px);
|
| 188 |
+
background-size: 550px 550px, 350px 350px, 250px 250px, 150px 150px;
|
| 189 |
+
background-position: 0 0, 40px 60px, 130px 270px, 70px 100px;
|
| 190 |
+
animation: backgroundScroll 60s linear infinite;
|
| 191 |
+
}
|
| 192 |
+
@keyframes backgroundScroll {
|
| 193 |
+
0% { background-position: 0 0, 40px 60px, 130px 270px, 70px 100px; }
|
| 194 |
+
100% { background-position: 550px 550px, 590px 610px, 680px 820px, 620px 650px; }
|
| 195 |
+
}
|
| 196 |
+
.container { max-width: 800px; margin: 0 auto; padding: 20px; }
|
| 197 |
+
.header {
|
| 198 |
+
text-align: center;
|
| 199 |
+
margin-bottom: 30px;
|
| 200 |
+
font-family: 'Copperplate', fantasy;
|
| 201 |
+
color: #ffd700;
|
| 202 |
+
text-shadow: 0 0 10px #ffd700, 0 0 20px #ffd700, 0 0 30px #ffd700;
|
| 203 |
+
}
|
| 204 |
+
.chat-box {
|
| 205 |
+
background-color: rgba(42, 42, 42, 0.7);
|
| 206 |
+
border-radius: 15px;
|
| 207 |
+
padding: 20px;
|
| 208 |
+
margin-bottom: 20px;
|
| 209 |
+
box-shadow: 0 0 20px rgba(255, 215, 0, 0.3);
|
| 210 |
+
}
|
| 211 |
+
.user-input {
|
| 212 |
+
background-color: rgba(58, 58, 58, 0.8);
|
| 213 |
+
border: 2px solid #ffd700;
|
| 214 |
+
color: #ffffff;
|
| 215 |
+
padding: 10px;
|
| 216 |
+
border-radius: 5px;
|
| 217 |
+
width: 100%;
|
| 218 |
+
transition: all 0.3s ease;
|
| 219 |
+
}
|
| 220 |
+
.user-input:focus {
|
| 221 |
+
box-shadow: 0 0 15px #ffd700;
|
| 222 |
+
}
|
| 223 |
+
.generate-btn {
|
| 224 |
+
background-color: #ffd700;
|
| 225 |
+
color: #0f1624;
|
| 226 |
+
border: none;
|
| 227 |
+
padding: 10px 20px;
|
| 228 |
+
border-radius: 5px;
|
| 229 |
+
cursor: pointer;
|
| 230 |
+
font-weight: bold;
|
| 231 |
+
transition: all 0.3s ease;
|
| 232 |
+
}
|
| 233 |
+
.generate-btn:hover {
|
| 234 |
+
background-color: #ffec8b;
|
| 235 |
+
transform: scale(1.05);
|
| 236 |
+
}
|
| 237 |
+
.output-box {
|
| 238 |
+
background-color: rgba(42, 42, 42, 0.7);
|
| 239 |
+
border-radius: 15px;
|
| 240 |
+
padding: 20px;
|
| 241 |
+
margin-top: 20px;
|
| 242 |
+
min-height: 100px;
|
| 243 |
+
border: 1px solid #ffd700;
|
| 244 |
+
white-space: pre-wrap;
|
| 245 |
+
font-family: 'Georgia', serif;
|
| 246 |
+
line-height: 1.6;
|
| 247 |
+
box-shadow: inset 0 0 10px rgba(255, 215, 0, 0.3);
|
| 248 |
+
}
|
| 249 |
+
.gr-slider {
|
| 250 |
+
--slider-color: #ffd700;
|
| 251 |
+
}
|
| 252 |
+
.gr-box {
|
| 253 |
+
border-color: #ffd700;
|
| 254 |
+
background-color: rgba(42, 42, 42, 0.7);
|
| 255 |
+
}
|
| 256 |
+
</style>
|
| 257 |
+
"""
|
| 258 |
+
|
| 259 |
+
with gr.Blocks(css=css) as demo:
|
| 260 |
+
gr.HTML("<div class='header'><h1>🌟 Enchanted Tales Generator 🌟</h1></div>")
|
| 261 |
+
|
| 262 |
+
with gr.Row():
|
| 263 |
+
with gr.Column(scale=3):
|
| 264 |
+
prompt = gr.Textbox(
|
| 265 |
+
placeholder="Begin your magical journey here (e.g., 'In a realm beyond the mists of time...')",
|
| 266 |
+
label="Story Incantation",
|
| 267 |
+
elem_classes="user-input"
|
| 268 |
+
)
|
| 269 |
+
with gr.Column(scale=1):
|
| 270 |
+
generate_btn = gr.Button("Weave the Tale", elem_classes="generate-btn")
|
| 271 |
+
|
| 272 |
+
with gr.Row():
|
| 273 |
+
max_length = gr.Slider(minimum=50, maximum=500, value=432, step=1, label="Scroll Length")
|
| 274 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.8, step=0.1, label="Magical Intensity")
|
| 275 |
+
top_k = gr.Slider(minimum=1, maximum=100, value=40, step=1, label="Arcane Diversity")
|
| 276 |
+
|
| 277 |
+
output = gr.Markdown(elem_classes="output-box")
|
| 278 |
+
|
| 279 |
+
generate_btn.click(
|
| 280 |
+
gradio_generate,
|
| 281 |
+
inputs=[prompt, max_length, temperature, top_k],
|
| 282 |
+
outputs=output
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
gr.HTML("""
|
| 286 |
+
<div style="text-align: center; margin-top: 20px; font-style: italic; color: #ffd700;">
|
| 287 |
+
"In the realm of imagination, every word is a spell, every sentence a charm."
|
| 288 |
+
</div>
|
| 289 |
+
""")
|
| 290 |
+
|
| 291 |
+
if __name__ == "__main__":
|
| 292 |
+
demo.launch()
|