Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,19 +7,21 @@ from nanochat.gpt import GPT, GPTConfig
|
|
| 7 |
MODEL_PATH = "model_000971.pt"
|
| 8 |
|
| 9 |
print("Waking up the toddler (NanoChat-ClimbMix-D12)...")
|
|
|
|
| 10 |
tokenizer = get_tokenizer()
|
| 11 |
|
| 12 |
-
print("Creating GPT model skeleton
|
| 13 |
-
|
|
|
|
| 14 |
config = GPTConfig(
|
| 15 |
vocab_size=32768,
|
| 16 |
n_layer=12,
|
| 17 |
-
n_head=6,
|
| 18 |
-
n_kv_head=6,
|
| 19 |
-
n_embd=768,
|
| 20 |
-
sequence_len=2048,
|
| 21 |
-
#
|
| 22 |
-
# window_pattern='L'
|
| 23 |
)
|
| 24 |
|
| 25 |
model = GPT(config)
|
|
@@ -27,14 +29,14 @@ model = GPT(config)
|
|
| 27 |
print("Loading weights...")
|
| 28 |
state_dict = torch.load(MODEL_PATH, map_location="cpu", weights_only=False)
|
| 29 |
|
| 30 |
-
# Remove the DDP wrapper prefix if present
|
| 31 |
unwanted_prefix = '_orig_mod.'
|
| 32 |
for k in list(state_dict.keys()):
|
| 33 |
if k.startswith(unwanted_prefix):
|
| 34 |
state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
model.to("cpu")
|
| 39 |
model.eval()
|
| 40 |
print("Model ready!")
|
|
@@ -43,17 +45,15 @@ engine = Engine(model=model, tokenizer=tokenizer)
|
|
| 43 |
|
| 44 |
def chat_fn(message, history):
|
| 45 |
try:
|
| 46 |
-
# Build prompt
|
| 47 |
prompt_tokens = []
|
| 48 |
-
for
|
| 49 |
-
prompt_tokens += tokenizer.encode(f"<|user|>{
|
| 50 |
-
if
|
| 51 |
-
prompt_tokens += tokenizer.encode(f"<|assistant|>{
|
| 52 |
-
|
| 53 |
-
# Current turn
|
| 54 |
prompt_tokens += tokenizer.encode(f"<|user|>{message}<|end|><|assistant|>")
|
| 55 |
|
| 56 |
-
# Blocking generation (NanoChat Engine standard)
|
| 57 |
new_tokens = engine.generate(
|
| 58 |
prompt_tokens,
|
| 59 |
max_tokens=512,
|
|
@@ -62,25 +62,20 @@ def chat_fn(message, history):
|
|
| 62 |
)
|
| 63 |
|
| 64 |
response = tokenizer.decode(new_tokens).strip()
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
for stop_tag in ["<|end|>", "<|assistant_end|>", "<|EOS|>"]:
|
| 68 |
-
if stop_tag in response:
|
| 69 |
-
response = response.split(stop_tag)[0].strip()
|
| 70 |
-
|
| 71 |
-
return response or "The toddler is lost in thought... 😅"
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
return f"Toddler tantrum: {str(e)}"
|
| 75 |
|
| 76 |
-
# Gradio UI
|
| 77 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 78 |
-
gr.Markdown("# 🧸 NanoChat-ClimbMix-D12")
|
| 79 |
-
gr.Markdown("
|
| 80 |
-
|
| 81 |
gr.ChatInterface(
|
| 82 |
fn=chat_fn,
|
| 83 |
-
examples=["Tell me a joke", "What is
|
| 84 |
title="Chat with the Toddler"
|
| 85 |
)
|
| 86 |
|
|
|
|
| 7 |
MODEL_PATH = "model_000971.pt"
|
| 8 |
|
| 9 |
print("Waking up the toddler (NanoChat-ClimbMix-D12)...")
|
| 10 |
+
|
| 11 |
tokenizer = get_tokenizer()
|
| 12 |
|
| 13 |
+
print("Creating GPT model skeleton with exact local config...")
|
| 14 |
+
|
| 15 |
+
# Exact config from your local chat_web.py load
|
| 16 |
config = GPTConfig(
|
| 17 |
vocab_size=32768,
|
| 18 |
n_layer=12,
|
| 19 |
+
n_head=6, # ← Critical: matches local load (n_head=6)
|
| 20 |
+
n_kv_head=6, # ← Matches n_head (no GQA here)
|
| 21 |
+
n_embd=768,
|
| 22 |
+
sequence_len=2048, # ← From local log (2048, not 1024)
|
| 23 |
+
# dropout=0.1, # omit if TypeError
|
| 24 |
+
# window_pattern='L' # if GPTConfig has this field — add if needed
|
| 25 |
)
|
| 26 |
|
| 27 |
model = GPT(config)
|
|
|
|
| 29 |
print("Loading weights...")
|
| 30 |
state_dict = torch.load(MODEL_PATH, map_location="cpu", weights_only=False)
|
| 31 |
|
|
|
|
| 32 |
unwanted_prefix = '_orig_mod.'
|
| 33 |
for k in list(state_dict.keys()):
|
| 34 |
if k.startswith(unwanted_prefix):
|
| 35 |
state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
|
| 36 |
|
| 37 |
+
missing, unexpected = model.load_state_dict(state_dict, strict=False)
|
| 38 |
+
print(f"Load info: {len(missing)} missing keys, {len(unexpected)} unexpected keys")
|
| 39 |
+
|
| 40 |
model.to("cpu")
|
| 41 |
model.eval()
|
| 42 |
print("Model ready!")
|
|
|
|
| 45 |
|
| 46 |
def chat_fn(message, history):
|
| 47 |
try:
|
| 48 |
+
# Build prompt like local chat_web.py (adjust tags if needed)
|
| 49 |
prompt_tokens = []
|
| 50 |
+
for user, assistant in history:
|
| 51 |
+
prompt_tokens += tokenizer.encode(f"<|user|>{user}<|end|>")
|
| 52 |
+
if assistant:
|
| 53 |
+
prompt_tokens += tokenizer.encode(f"<|assistant|>{assistant}<|end|>")
|
| 54 |
+
|
|
|
|
| 55 |
prompt_tokens += tokenizer.encode(f"<|user|>{message}<|end|><|assistant|>")
|
| 56 |
|
|
|
|
| 57 |
new_tokens = engine.generate(
|
| 58 |
prompt_tokens,
|
| 59 |
max_tokens=512,
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
response = tokenizer.decode(new_tokens).strip()
|
| 65 |
+
if "<|end|>" in response:
|
| 66 |
+
response = response.split("<|end|>")[0].strip()
|
| 67 |
|
| 68 |
+
return response or "Toddler says: ... 😅"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
return f"Toddler tantrum: {str(e)}"
|
| 72 |
|
|
|
|
| 73 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 74 |
+
gr.Markdown("# 🧸 NanoChat-ClimbMix-D12 – Confident Toddler")
|
| 75 |
+
gr.Markdown("Loaded with exact config from local chat_web.py — n_head=6, seq_len=2048")
|
|
|
|
| 76 |
gr.ChatInterface(
|
| 77 |
fn=chat_fn,
|
| 78 |
+
examples=["Tell me a joke", "What is UPI?", "Write hello world Python"],
|
| 79 |
title="Chat with the Toddler"
|
| 80 |
)
|
| 81 |
|