Really-Amazing commited on
Commit
ec6ca7c
·
verified ·
1 Parent(s): d309855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
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 using verified local configs...")
13
- # EXACT CONFIG FROM YOUR SUCCESSFUL scripts.chat_web LOG
 
14
  config = GPTConfig(
15
  vocab_size=32768,
16
  n_layer=12,
17
- n_head=6, # Verified: 6 heads
18
- n_kv_head=6, # Verified: 6 KV heads
19
- n_embd=768, # Verified: 768 embedding dim
20
- sequence_len=2048, # Verified: 2048 context
21
- # Note: If your GPT class accepts window_pattern, uncomment below:
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
- # Loading weights - should be 100% match now
37
- model.load_state_dict(state_dict, strict=False)
 
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 using the standard nanochat special tokens
47
  prompt_tokens = []
48
- for user_msg, assistant_msg in history:
49
- prompt_tokens += tokenizer.encode(f"<|user|>{user_msg}<|end|>")
50
- if assistant_msg:
51
- prompt_tokens += tokenizer.encode(f"<|assistant|>{assistant_msg}<|end|>")
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
- # Clean up tags
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("Architecture: 12 Layers, 6 Heads, GQA. Optimized for Fintech testing.")
80
-
81
  gr.ChatInterface(
82
  fn=chat_fn,
83
- examples=["Tell me a joke", "What is NPCI FiMI?", "Explain UPI transaction flow"],
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