Really-Amazing commited on
Commit
b6bf288
·
verified ·
1 Parent(s): f856df9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -34
app.py CHANGED
@@ -7,21 +7,18 @@ from nanochat.gpt import GPT, GPTConfig
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)
@@ -34,9 +31,7 @@ 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 +40,53 @@ engine = Engine(model=model, tokenizer=tokenizer)
45
 
46
  def chat_fn(message, history):
47
  try:
 
48
  prompt_tokens = []
49
-
50
- # Force list conversion on encode output
51
- for user, assistant in history:
52
- prompt_tokens.extend(list(tokenizer.encode(f"<|user|>{user}<|end|>")))
53
- if assistant:
54
- prompt_tokens.extend(list(tokenizer.encode(f"<|assistant|>{assistant}<|end|>")))
55
-
 
56
  prompt_tokens.extend(list(tokenizer.encode(f"<|user|>{message}<|end|><|assistant|>")))
57
 
58
- # Optional: add BOS if your model needs it
59
- # bos = tokenizer.bos_token_id or list(tokenizer.encode("<|BOS|>"))[0]
60
- # prompt_tokens = [bos] + prompt_tokens
61
-
62
  new_tokens = engine.generate(
63
- prompt_tokens, # now definitely list[int]
64
  max_tokens=512,
65
- temperature=0.85,
66
  top_k=50,
67
  )
68
 
69
- response = tokenizer.decode(new_tokens).strip()
70
-
71
- # Clean end token
72
- for end in ["<|end|>", "<|assistant_end|>", "<|EOS|>"]:
73
- if end in response:
74
- response = response.split(end)[0].strip()
75
- break
76
 
77
- return response or "Toddler says: ... 😅"
 
 
 
 
 
78
 
79
  except Exception as e:
 
 
 
80
  return f"Toddler tantrum: {str(e)}"
81
 
82
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
83
  gr.Markdown("# 🧸 NanoChat-ClimbMix-D12 – Confident Toddler")
84
- gr.Markdown("Loaded with exact config from local chat_web.py — n_head=6, seq_len=2048")
 
85
  gr.ChatInterface(
86
  fn=chat_fn,
87
- examples=["Tell me a joke", "What is UPI?", "Write hello world Python"],
88
  title="Chat with the Toddler"
89
  )
90
 
 
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 with exact local config...")
13
+ # EXACT values from your local scripts.chat_web output:
14
+ # {'sequence_len': 2048, 'vocab_size': 32768, 'n_layer': 12, 'n_head': 6, 'n_kv_head': 6, 'n_embd': 768}
15
  config = GPTConfig(
16
  vocab_size=32768,
17
  n_layer=12,
18
+ n_head=6, # Critical fix
19
+ n_kv_head=6, # Critical fix
20
  n_embd=768,
21
+ sequence_len=2048, # Updated from 1024
 
 
22
  )
23
 
24
  model = GPT(config)
 
31
  if k.startswith(unwanted_prefix):
32
  state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
33
 
34
+ model.load_state_dict(state_dict, strict=False)
 
 
35
  model.to("cpu")
36
  model.eval()
37
  print("Model ready!")
 
40
 
41
  def chat_fn(message, history):
42
  try:
43
+ # 1. Build prompt and FORCE everything to be a flat list of integers
44
  prompt_tokens = []
45
+
46
+ for user_msg, assistant_msg in history:
47
+ # We wrap in list() to kill any generator objects returned by tokenizer
48
+ prompt_tokens.extend(list(tokenizer.encode(f"<|user|>{user_msg}<|end|>")))
49
+ if assistant_msg:
50
+ prompt_tokens.extend(list(tokenizer.encode(f"<|assistant|>{assistant_msg}<|end|>")))
51
+
52
+ # Current message
53
  prompt_tokens.extend(list(tokenizer.encode(f"<|user|>{message}<|end|><|assistant|>")))
54
 
55
+ # 2. Generate (Blocking)
56
+ # Note: We pass prompt_tokens which is now guaranteed to be a List[int]
 
 
57
  new_tokens = engine.generate(
58
+ prompt_tokens,
59
  max_tokens=512,
60
+ temperature=0.8,
61
  top_k=50,
62
  )
63
 
64
+ # 3. Handle the output (ensure it's a list for decoding)
65
+ if hasattr(new_tokens, 'tolist'):
66
+ new_tokens = new_tokens.tolist()
67
+
68
+ response = tokenizer.decode(list(new_tokens)).strip()
 
 
69
 
70
+ # 4. Clean up the tags
71
+ for stop_tag in ["<|end|>", "<|assistant_end|>", "<|EOS|>"]:
72
+ if stop_tag in response:
73
+ response = response.split(stop_tag)[0].strip()
74
+
75
+ return response or "Toddler is thinking... 😅"
76
 
77
  except Exception as e:
78
+ # If it still fails, this will tell us exactly which line crashed
79
+ import traceback
80
+ print(traceback.format_exc())
81
  return f"Toddler tantrum: {str(e)}"
82
 
83
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
84
  gr.Markdown("# 🧸 NanoChat-ClimbMix-D12 – Confident Toddler")
85
+ gr.Markdown("Architecture: 12 Layers, 6 Heads (Verified). Fixed Sequence/Generator error.")
86
+
87
  gr.ChatInterface(
88
  fn=chat_fn,
89
+ examples=["Tell me a joke", "What is UPI?", "Explain QA automation"],
90
  title="Chat with the Toddler"
91
  )
92