NiftyOracleAI commited on
Commit
b512b82
ยท
verified ยท
1 Parent(s): 454592e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -53
app.py CHANGED
@@ -1,10 +1,5 @@
1
  # ================================================================
2
- # CodeSymb AI โ€” HuggingFace Space app.py (FINAL v2.0)
3
- # โœ… Single-string tokenization (matches training exactly)
4
- # โœ… Full-sequence stop tags (no more "<" premature death)
5
- # โœ… Vocab size verification
6
- # โœ… Hard fail if no checkpoint (no silent random model)
7
- # NiftyOracleAI ๐Ÿ‡ฎ๐Ÿ‡ณ | codesymb.com ๐ŸŒŽ
8
  # ================================================================
9
 
10
  import gradio as gr
@@ -38,7 +33,7 @@ class RMSNorm(nn.Module):
38
  return (x.float() * n).to(x.dtype) * self.w.to(x.dtype)
39
 
40
  class RoPE(nn.Module):
41
- def __init__(self, dim, max_len=2048, base=500_000):
42
  super().__init__()
43
  inv = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
44
  emb = torch.cat([torch.outer(torch.arange(max_len).float(), inv)] * 2, dim=-1)
@@ -99,7 +94,6 @@ class Block(nn.Module):
99
  return x
100
 
101
  class CodeSymb1B(nn.Module):
102
- """CodeSymb 1B | NiftyOracleAI ๐Ÿ‡ฎ๐Ÿ‡ณ | codesymb.com"""
103
  def __init__(self, vocab=50000):
104
  super().__init__()
105
  DM=2048; DF=8192; NL=16; NH=16; NKV=4
@@ -124,7 +118,7 @@ VOCAB = sp.get_piece_size()
124
  print(f"โœ… Tokenizer: {VOCAB:,} vocab")
125
 
126
  print("โณ Loading CodeSymb-1B model...")
127
- model = CodeSymb1B(vocab=VOCAB) # โœ… architecture built to match REAL vocab size
128
 
129
  ckpt_found = False
130
  last_err = None
@@ -135,14 +129,13 @@ for ckpt_name in ["checkpoints/sft_latest.pt",
135
  ckpt_path = hf_hub_download(REPO_ID, ckpt_name, token=HF_TOKEN or None)
136
  print(f"โฌ‡๏ธ Downloaded {ckpt_name} โ€” loading weights...")
137
  try:
138
- state = torch.load(ckpt_path, map_location="cpu") # โœ… NO mmap
139
  except Exception:
140
  state = torch.load(ckpt_path, map_location="cpu", weights_only=False)
 
141
  model_state = state.get("model", state)
142
- # โœ… RoPE buffers are rebuilt from config anyway โ€” never let them crash the load
143
- model_state = {k: v for k, v in model_state.items()
144
- if not k.endswith((".pe.cos", ".pe.sin"))}
145
- model.load_state_dict(model_state, strict=False)
146
  model.load_state_dict(model_state, strict=False)
147
  step = state.get("step", 0)
148
  print(f"โœ… Loaded: {ckpt_name} (step {step:,})")
@@ -150,18 +143,17 @@ model.load_state_dict(model_state, strict=False)
150
  break
151
  except Exception as e:
152
  last_err = e
153
- print(f" โš ๏ธ {ckpt_name} failed: {type(e).__name__}: {e}") # โœ… show REAL error
154
 
155
  if not ckpt_found:
156
  raise RuntimeError(f"โŒ NO CHECKPOINT LOADED! Last error: {last_err}")
157
 
158
-
159
  model.eval()
160
  n_params = sum(p.numel() for p in model.parameters())
161
  print(f"โœ… CodeSymb-1B: {n_params/1e9:.2f}B params โ€” READY ๐Ÿ”ฅ")
162
 
163
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
164
- # SYSTEM PROMPT (byte-identical to SFT training data)
165
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
166
  SYSTEM_PROMPT = (
167
  "You are CodeSymb, the world's most intelligent, deeply empathetic, "
@@ -179,10 +171,6 @@ SYSTEM_PROMPT = (
179
  )
180
 
181
  def build_prompt(message: str, history: list) -> list:
182
- """
183
- โœ… Build ONE string exactly like training did, encode ONCE.
184
- SentencePiece is not concatenative โ€” never encode pieces separately!
185
- """
186
  parts = ["<|system|>\n" + SYSTEM_PROMPT + "\n"]
187
  for user_msg, asst_msg in history[-4:]:
188
  parts.append(f"<|user|>\n{user_msg}\n<|assistant|>\n{asst_msg}\n")
@@ -190,7 +178,6 @@ def build_prompt(message: str, history: list) -> list:
190
  return sp.encode("".join(parts))
191
 
192
  def tail_matches(generated: list, seq: list) -> bool:
193
- """โœ… Only stop on COMPLETE tag sequence โ€” a lone '<' won't kill generation"""
194
  seq = list(seq)
195
  return len(generated) >= len(seq) and generated[-len(seq):] == seq
196
 
@@ -200,8 +187,6 @@ def chat_with_codesymb(message: str, history: list) -> str:
200
  model.to(device)
201
 
202
  prompt_ids = build_prompt(message, history)
203
-
204
- # โœ… Trim safely: if too long, keep from a <|user|> boundary, never mid-tag
205
  if len(prompt_ids) > 1024:
206
  user_seq = sp.encode("<|user|>")
207
  start = 0
@@ -213,7 +198,6 @@ def chat_with_codesymb(message: str, history: list) -> str:
213
 
214
  inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
215
 
216
- # โœ… Stop sequences (complete multi-piece tags, not single pieces)
217
  END_SEQ = sp.encode("<|endoftext|>")
218
  USER_SEQ = sp.encode("<|user|>")
219
  SYS_SEQ = sp.encode("<|system|>")
@@ -223,14 +207,11 @@ def chat_with_codesymb(message: str, history: list) -> str:
223
 
224
  with torch.no_grad():
225
  for _ in range(MAX_NEW):
226
- with torch.autocast("cuda" if device.type == "cuda" else "cpu",
227
- dtype=torch.bfloat16):
228
  logits = model(inp)[0, -1, :].float()
229
 
230
- # Temperature
231
  logits = logits / 0.7
232
 
233
- # Top-p (nucleus) sampling
234
  sorted_logits, sorted_idx = torch.sort(logits, descending=True)
235
  cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
236
  remove = cum_probs > 0.92
@@ -240,7 +221,6 @@ def chat_with_codesymb(message: str, history: list) -> str:
240
  logits_filtered = torch.zeros_like(logits)
241
  logits_filtered.scatter_(0, sorted_idx, sorted_logits)
242
 
243
- # Repetition penalty (last 20 tokens)
244
  if generated:
245
  for gid in set(generated[-20:]):
246
  if 0 <= gid < logits_filtered.shape[0]:
@@ -249,13 +229,9 @@ def chat_with_codesymb(message: str, history: list) -> str:
249
  probs = F.softmax(logits_filtered, dim=-1)
250
  next_t = torch.multinomial(probs, 1).item()
251
 
252
- # โœ… Stop ONLY on complete turn-ending tags
253
- if tail_matches(generated, END_SEQ) or \
254
- tail_matches(generated, USER_SEQ) or \
255
- tail_matches(generated, SYS_SEQ):
256
  break
257
 
258
- # Anti-loop: same token 3x in a row
259
  if len(generated) > 2 and generated[-1] == generated[-2] == next_t:
260
  break
261
 
@@ -264,15 +240,14 @@ def chat_with_codesymb(message: str, history: list) -> str:
264
 
265
  response = sp.decode(generated).strip()
266
 
267
- # Strip any leaked tags
268
  for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
269
  response = response.split(tag)[0]
270
  response = response.strip()
271
 
272
  if not response:
273
  response = (
274
- "I'm CodeSymb, your coding partner! ๐Ÿ”ฅ\n"
275
- "Built by Deepakkumar Malkar at NiftyOracle ๐Ÿ‡ฎ๐Ÿ‡ณ\n"
276
  "Ask me anything about code!"
277
  )
278
  return response
@@ -281,7 +256,7 @@ def chat_with_codesymb(message: str, history: list) -> str:
281
  # GRADIO UI
282
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
283
  with gr.Blocks(
284
- title="CodeSymb AI โ€” codesymb.com",
285
  theme=gr.themes.Base(primary_hue="blue", secondary_hue="indigo"),
286
  css="""
287
  body { background: #060D1F !important; }
@@ -312,23 +287,10 @@ with gr.Blocks(
312
  examples=[
313
  ["Hi! Who are you and what can you build?"],
314
  ["Build me a Python FastAPI with JWT auth"],
315
- ["Fix this error: AttributeError: 'NoneType' has no attribute 'split'"],
316
- ["Explain async/await in simple Hindi"],
317
  ["Write me a complete Android login screen in Kotlin"],
318
- ["Build a 3D WebGPU game with Rust-WASM"],
319
  ],
320
- retry_btn="๐Ÿ”„ Retry",
321
- undo_btn="โ†ฉ Undo",
322
- clear_btn="๐Ÿ—‘ Clear",
323
  )
324
 
325
- gr.HTML("""
326
- <div style='text-align:center;padding:20px;color:#4A5568;font-size:13px'>
327
- <strong>CodeSymb-1B</strong> | 1.08B params | NiftyOracle ๐Ÿ‡ฎ๐Ÿ‡ณ<br>
328
- <a href='https://codesymb.com' style='color:#1D6FEB'>codesymb.com</a> ๐ŸŒŽ |
329
- <a href='https://huggingface.co/NiftyOracleAI' style='color:#1D6FEB'>HuggingFace</a>
330
- </div>
331
- """)
332
-
333
  if __name__ == "__main__":
334
  demo.launch()
 
1
  # ================================================================
2
+ # CodeSymb AI โ€” HuggingFace Space app.py (FIXED INDENTATION)
 
 
 
 
 
3
  # ================================================================
4
 
5
  import gradio as gr
 
33
  return (x.float() * n).to(x.dtype) * self.w.to(x.dtype)
34
 
35
  class RoPE(nn.Module):
36
+ def __init__(self, dim, max_len=2048, base=500_000):
37
  super().__init__()
38
  inv = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
39
  emb = torch.cat([torch.outer(torch.arange(max_len).float(), inv)] * 2, dim=-1)
 
94
  return x
95
 
96
  class CodeSymb1B(nn.Module):
 
97
  def __init__(self, vocab=50000):
98
  super().__init__()
99
  DM=2048; DF=8192; NL=16; NH=16; NKV=4
 
118
  print(f"โœ… Tokenizer: {VOCAB:,} vocab")
119
 
120
  print("โณ Loading CodeSymb-1B model...")
121
+ model = CodeSymb1B(vocab=VOCAB)
122
 
123
  ckpt_found = False
124
  last_err = None
 
129
  ckpt_path = hf_hub_download(REPO_ID, ckpt_name, token=HF_TOKEN or None)
130
  print(f"โฌ‡๏ธ Downloaded {ckpt_name} โ€” loading weights...")
131
  try:
132
+ state = torch.load(ckpt_path, map_location="cpu")
133
  except Exception:
134
  state = torch.load(ckpt_path, map_location="cpu", weights_only=False)
135
+
136
  model_state = state.get("model", state)
137
+ model_state = {k: v for k, v in model_state.items() if not k.endswith((".pe.cos", ".pe.sin"))}
138
+
 
 
139
  model.load_state_dict(model_state, strict=False)
140
  step = state.get("step", 0)
141
  print(f"โœ… Loaded: {ckpt_name} (step {step:,})")
 
143
  break
144
  except Exception as e:
145
  last_err = e
146
+ print(f" โš ๏ธ {ckpt_name} failed: {type(e).__name__}: {e}")
147
 
148
  if not ckpt_found:
149
  raise RuntimeError(f"โŒ NO CHECKPOINT LOADED! Last error: {last_err}")
150
 
 
151
  model.eval()
152
  n_params = sum(p.numel() for p in model.parameters())
153
  print(f"โœ… CodeSymb-1B: {n_params/1e9:.2f}B params โ€” READY ๐Ÿ”ฅ")
154
 
155
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
156
+ # SYSTEM PROMPT
157
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
158
  SYSTEM_PROMPT = (
159
  "You are CodeSymb, the world's most intelligent, deeply empathetic, "
 
171
  )
172
 
173
  def build_prompt(message: str, history: list) -> list:
 
 
 
 
174
  parts = ["<|system|>\n" + SYSTEM_PROMPT + "\n"]
175
  for user_msg, asst_msg in history[-4:]:
176
  parts.append(f"<|user|>\n{user_msg}\n<|assistant|>\n{asst_msg}\n")
 
178
  return sp.encode("".join(parts))
179
 
180
  def tail_matches(generated: list, seq: list) -> bool:
 
181
  seq = list(seq)
182
  return len(generated) >= len(seq) and generated[-len(seq):] == seq
183
 
 
187
  model.to(device)
188
 
189
  prompt_ids = build_prompt(message, history)
 
 
190
  if len(prompt_ids) > 1024:
191
  user_seq = sp.encode("<|user|>")
192
  start = 0
 
198
 
199
  inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
200
 
 
201
  END_SEQ = sp.encode("<|endoftext|>")
202
  USER_SEQ = sp.encode("<|user|>")
203
  SYS_SEQ = sp.encode("<|system|>")
 
207
 
208
  with torch.no_grad():
209
  for _ in range(MAX_NEW):
210
+ with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):
 
211
  logits = model(inp)[0, -1, :].float()
212
 
 
213
  logits = logits / 0.7
214
 
 
215
  sorted_logits, sorted_idx = torch.sort(logits, descending=True)
216
  cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
217
  remove = cum_probs > 0.92
 
221
  logits_filtered = torch.zeros_like(logits)
222
  logits_filtered.scatter_(0, sorted_idx, sorted_logits)
223
 
 
224
  if generated:
225
  for gid in set(generated[-20:]):
226
  if 0 <= gid < logits_filtered.shape[0]:
 
229
  probs = F.softmax(logits_filtered, dim=-1)
230
  next_t = torch.multinomial(probs, 1).item()
231
 
232
+ if tail_matches(generated, END_SEQ) or tail_matches(generated, USER_SEQ) or tail_matches(generated, SYS_SEQ):
 
 
 
233
  break
234
 
 
235
  if len(generated) > 2 and generated[-1] == generated[-2] == next_t:
236
  break
237
 
 
240
 
241
  response = sp.decode(generated).strip()
242
 
 
243
  for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
244
  response = response.split(tag)[0]
245
  response = response.strip()
246
 
247
  if not response:
248
  response = (
249
+ "I'm CodeSymb! ๐Ÿ”ฅ\n"
250
+ "Built by Dev & Deepakkumar Malkar ๐Ÿ‡ฎ๐Ÿ‡ณ\n"
251
  "Ask me anything about code!"
252
  )
253
  return response
 
256
  # GRADIO UI
257
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
258
  with gr.Blocks(
259
+ title="CodeSymb AI",
260
  theme=gr.themes.Base(primary_hue="blue", secondary_hue="indigo"),
261
  css="""
262
  body { background: #060D1F !important; }
 
287
  examples=[
288
  ["Hi! Who are you and what can you build?"],
289
  ["Build me a Python FastAPI with JWT auth"],
290
+ ["Fix this error: AttributeError: 'NoneType'"],
 
291
  ["Write me a complete Android login screen in Kotlin"],
 
292
  ],
 
 
 
293
  )
294
 
 
 
 
 
 
 
 
 
295
  if __name__ == "__main__":
296
  demo.launch()