Spaces:
Sleeping
Sleeping
File size: 13,258 Bytes
6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 6b9e0b5 d245330 |
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
import gradio as gr
import torch
import torch.nn as nn
import torch.nn.functional as F
from tokenizers import Tokenizer
import json
import math
print("π Starting Twitter Reply Bot...")
# Load configuration
with open('model_config.json', 'r') as f:
config = json.load(f)
print(f"β
Config loaded: {config['vocab_size']} vocab, {config['d_model']} d_model")
# Load tokenizer
tokenizer = Tokenizer.from_file("twitter_tokenizer.json")
print("β
Tokenizer loaded")
# ==================== EXACT MODEL ARCHITECTURE FROM TRAINING ====================
class RMSNorm(nn.Module):
"""Root Mean Square Layer Normalization"""
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x):
rms = torch.sqrt(torch.mean(x ** 2, dim=-1, keepdim=True) + self.eps)
x_normed = x / rms
return self.weight * x_normed
class RotaryPositionEmbedding(nn.Module):
"""Rotary Position Embeddings (RoPE)"""
def __init__(self, dim: int, max_seq_len: int = 2048, base: int = 10000):
super().__init__()
self.dim = dim
self.max_seq_len = max_seq_len
self.base = base
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
self.register_buffer("inv_freq", inv_freq)
self._build_cache(max_seq_len)
def _build_cache(self, seq_len: int):
t = torch.arange(seq_len, dtype=self.inv_freq.dtype, device=self.inv_freq.device)
freqs = torch.outer(t, self.inv_freq)
emb = torch.cat((freqs, freqs), dim=-1)
self.register_buffer("cos_cached", emb.cos()[None, None, :, :], persistent=False)
self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)
def forward(self, q, k):
seq_len = q.shape[2]
cos = self.cos_cached[:, :, :seq_len, ...]
sin = self.sin_cached[:, :, :seq_len, ...]
q_rot = (q * cos) + (self._rotate_half(q) * sin)
k_rot = (k * cos) + (self._rotate_half(k) * sin)
return q_rot, k_rot
def _rotate_half(self, x):
x1, x2 = x[..., :x.shape[-1] // 2], x[..., x.shape[-1] // 2:]
return torch.cat((-x2, x1), dim=-1)
class MultiHeadAttention(nn.Module):
"""Multi-Head Self Attention with RoPE"""
def __init__(self, d_model: int, n_heads: int, max_seq_len: int):
super().__init__()
assert d_model % n_heads == 0
self.d_model = d_model
self.n_heads = n_heads
self.head_dim = d_model // n_heads
self.q_proj = nn.Linear(d_model, d_model, bias=False)
self.k_proj = nn.Linear(d_model, d_model, bias=False)
self.v_proj = nn.Linear(d_model, d_model, bias=False)
self.o_proj = nn.Linear(d_model, d_model, bias=False)
self.rope = RotaryPositionEmbedding(self.head_dim, max_seq_len)
def forward(self, x, mask=None):
batch_size, seq_len, d_model = x.shape
q = self.q_proj(x).view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
k = self.k_proj(x).view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
v = self.v_proj(x).view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
q, k = self.rope(q, k)
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.head_dim)
if mask is not None:
scores = scores.masked_fill(mask == 0, float('-inf'))
attn_weights = F.softmax(scores, dim=-1)
attn_output = torch.matmul(attn_weights, v)
attn_output = attn_output.transpose(1, 2).contiguous().view(batch_size, seq_len, d_model)
return self.o_proj(attn_output)
class SwiGLU(nn.Module):
"""SwiGLU Activation Function"""
def __init__(self, d_model: int, d_ff: int):
super().__init__()
self.w1 = nn.Linear(d_model, d_ff, bias=False)
self.w2 = nn.Linear(d_ff, d_model, bias=False)
self.w3 = nn.Linear(d_model, d_ff, bias=False)
def forward(self, x):
return self.w2(F.silu(self.w1(x)) * self.w3(x))
class TransformerBlock(nn.Module):
"""Single Transformer Block"""
def __init__(self, d_model: int, n_heads: int, d_ff: int, max_seq_len: int):
super().__init__()
self.attention = MultiHeadAttention(d_model, n_heads, max_seq_len)
self.feed_forward = SwiGLU(d_model, d_ff)
self.norm1 = RMSNorm(d_model)
self.norm2 = RMSNorm(d_model)
def forward(self, x, mask=None):
x = x + self.attention(self.norm1(x), mask)
x = x + self.feed_forward(self.norm2(x))
return x
class TwitterTransformer(nn.Module):
"""Twitter Reply Transformer Model - EXACT TRAINING ARCHITECTURE"""
def __init__(self, vocab_size=8000, d_model=256, n_layers=6, n_heads=8,
d_ff=1024, max_seq_len=128, pad_token_id=0):
super().__init__()
self.vocab_size = vocab_size
self.d_model = d_model
self.max_seq_len = max_seq_len
self.pad_token_id = pad_token_id
self.token_embedding = nn.Embedding(vocab_size, d_model)
self.layers = nn.ModuleList([
TransformerBlock(d_model, n_heads, d_ff, max_seq_len)
for _ in range(n_layers)
])
self.norm = RMSNorm(d_model)
self.lm_head = nn.Linear(d_model, vocab_size, bias=False)
# Weight tying
self.lm_head.weight = self.token_embedding.weight
def forward(self, input_ids, attention_mask=None):
batch_size, seq_len = input_ids.shape
# Create causal mask
causal_mask = torch.tril(torch.ones(seq_len, seq_len, device=input_ids.device))
causal_mask = causal_mask.unsqueeze(0).unsqueeze(0)
if attention_mask is not None:
attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)
causal_mask = causal_mask * attention_mask
x = self.token_embedding(input_ids)
for layer in self.layers:
x = layer(x, causal_mask)
x = self.norm(x)
logits = self.lm_head(x)
return logits
@torch.no_grad()
def generate(self, input_ids, max_new_tokens=50, temperature=0.8, top_k=50, eos_token_id=None):
self.eval()
for _ in range(max_new_tokens):
input_ids_cropped = input_ids[:, -self.max_seq_len:]
logits = self(input_ids_cropped)
logits = logits[:, -1, :] / temperature
if top_k > 0:
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
logits[indices_to_remove] = float('-inf')
probs = F.softmax(logits, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
input_ids = torch.cat([input_ids, next_token], dim=1)
if eos_token_id is not None and next_token.item() == eos_token_id:
break
return input_ids
# ==================== LOAD MODEL ====================
print("π₯ Loading model...")
model = TwitterTransformer(
vocab_size=config['vocab_size'],
d_model=config['d_model'],
n_layers=6, # From your training
n_heads=8, # From your training
d_ff=1024, # From your training
max_seq_len=config['max_seq_len'],
pad_token_id=config['pad_token_id']
)
# Load weights
model.load_state_dict(torch.load('twitter_reply_model_final.pt', map_location='cpu'))
model.eval()
print("β
Model loaded successfully!")
print(f"π Parameters: {sum(p.numel() for p in model.parameters()):,}")
# ==================== GENERATION FUNCTION ====================
def generate_reply(tweet, personality, temperature, top_k):
"""Generate a reply to a tweet"""
try:
# Validate input
if not tweet or len(tweet.strip()) < 3:
return "β οΈ Please enter a valid tweet (at least 3 characters)"
# Format input
input_text = f"{personality}{tweet.strip()}<SEP>"
# Tokenize
input_ids = [config['bos_token_id']] + tokenizer.encode(input_text).ids
input_ids = torch.tensor([input_ids], dtype=torch.long)
# Generate
with torch.no_grad():
output = model.generate(
input_ids,
max_new_tokens=50,
temperature=max(0.5, min(temperature, 1.5)), # Clamp temperature
top_k=int(top_k),
eos_token_id=config['eos_token_id']
)
# Decode
text = tokenizer.decode(output[0].tolist())
# Extract reply
try:
reply = text.split('<SEP>')[1].split('[EOS]')[0].strip()
# Remove any leftover special tokens
reply = reply.replace('[BOS]', '').replace('[EOS]', '').replace('<SEP>', '').strip()
except:
reply = text.strip()
return reply if reply else "π€ Hmm, try adjusting temperature or rephrasing the tweet!"
except Exception as e:
return f"β Error: {str(e)}\n\nTry refreshing the page or adjusting parameters."
# ==================== GRADIO INTERFACE ====================
examples = [
["Why is my internet so slow today?", "[HELPFUL]", 0.7, 40],
["Your customer service is terrible!", "[PROFESSIONAL]", 0.6, 40],
["I love your product!", "[WITTY]", 0.8, 50],
["This is the worst service ever", "[HUMOR]", 0.8, 40],
["How do I reset my password?", "[FRIENDLY]", 0.7, 40],
["My order hasn't arrived yet", "[PROFESSIONAL]", 0.6, 40],
]
# Create interface
with gr.Blocks(theme=gr.themes.Soft(), title="Twitter Reply Bot") as demo:
gr.Markdown("""
# π€ Twitter Reply Bot
## 8.34M Parameter Custom Transformer
Generate witty, contextual replies to tweets using an AI model trained from scratch on 100K customer service conversations.
**Training Stats:** Final Loss: 3.43 | 3 Epochs | 15 mins on T4 GPU
""")
with gr.Row():
with gr.Column(scale=1):
tweet_input = gr.Textbox(
label="π± Tweet",
placeholder="Enter a tweet to reply to...",
lines=4,
max_lines=6
)
personality_dropdown = gr.Dropdown(
choices=["[WITTY]", "[HUMOR]", "[FRIENDLY]", "[PROFESSIONAL]", "[HELPFUL]"],
label="π Reply Personality",
value="[WITTY]",
info="Choose the tone for the reply"
)
with gr.Row():
temperature_slider = gr.Slider(
minimum=0.5,
maximum=1.2,
value=0.7,
step=0.1,
label="π‘οΈ Temperature",
info="Higher = more creative"
)
top_k_slider = gr.Slider(
minimum=10,
maximum=100,
value=40,
step=10,
label="π― Top-K",
info="Token selection diversity"
)
generate_btn = gr.Button("β¨ Generate Reply", variant="primary", size="lg")
with gr.Column(scale=1):
output = gr.Textbox(
label="π€ Generated Reply",
lines=6,
max_lines=8,
placeholder="Your AI-generated reply will appear here..."
)
gr.Markdown("""
### π‘ Personality Guide:
- **π WITTY**: Clever, playful, engaging
- **π HUMOR**: Light-hearted, funny
- **π€ FRIENDLY**: Warm, conversational
- **π PROFESSIONAL**: Formal, business tone
- **π HELPFUL**: Solution-focused, supportive
### βοΈ Parameter Tips:
- **Low temp (0.5-0.6)**: Consistent, safe replies
- **Mid temp (0.7-0.8)**: Balanced creativity
- **High temp (0.9-1.2)**: More creative, riskier
""")
# Examples section
gr.Markdown("### π Try These Examples:")
gr.Examples(
examples=examples,
inputs=[tweet_input, personality_dropdown, temperature_slider, top_k_slider],
outputs=output,
fn=generate_reply,
cache_examples=False,
)
# Connect button
generate_btn.click(
fn=generate_reply,
inputs=[tweet_input, personality_dropdown, temperature_slider, top_k_slider],
outputs=output
)
gr.Markdown("""
---
**β‘ Model Architecture:** Custom Transformer with RoPE + SwiGLU + RMSNorm
**π Training Data:** 945K customer service tweets
**π οΈ Built with:** PyTorch, Tokenizers, Gradio
**π Deployed on:** HuggingFace Spaces (Free CPU)
""")
# Launch
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
|