NiftyOracleAI commited on
Commit
f32c771
ยท
verified ยท
1 Parent(s): adb16e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
app.py CHANGED
@@ -1,7 +1,8 @@
1
  # ================================================================
2
- # CodeSymb AI โ€” HuggingFace Space app.py
3
- # โœ… EXACT URL FIX: codesymb-chat-lora/chat_lora_latest
4
- # โœ… Safetensors PEFT Matrix Fusion
 
5
  # NiftyOracleAI ๐Ÿ‡ฎ๐Ÿ‡ณ | codesymb.com ๐ŸŒŽ
6
  # ================================================================
7
 
@@ -13,18 +14,16 @@ import sentencepiece as spm
13
  import spaces
14
  import os
15
  from huggingface_hub import hf_hub_download, login
16
- from safetensors.torch import load_file
17
 
18
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
19
  USERNAME = "NiftyOracleAI"
20
  REPO_ID = f"{USERNAME}/NiftyOracle-Coder-30B"
21
  TOK_REPO = f"{USERNAME}/niftyoracle-tokenizer"
22
- LORA_REPO = f"{USERNAME}/codesymb-chat-lora" # ๐Ÿ”ฅ MATCHES YOUR EXACT URL!
23
 
24
  if HF_TOKEN: login(token=HF_TOKEN)
25
 
26
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
27
- # MODEL ARCHITECTURE
28
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
29
  class RMSNorm(nn.Module):
30
  def __init__(self, d, eps=1e-6):
@@ -98,7 +97,7 @@ class CodeSymb1B(nn.Module):
98
  return self.head(self.norm(x))
99
 
100
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
101
- # 1. LOAD BASE MODEL
102
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
103
  print("โณ Loading Tokenizer...")
104
  tok_path = hf_hub_download(TOK_REPO, "niftyoracle_tokenizer.model", token=HF_TOKEN or None)
@@ -113,35 +112,37 @@ base_weights = {k: v for k, v in state.get("model", state).items() if not k.ends
113
  model.load_state_dict(base_weights, strict=False)
114
  print("โœ… Base Model Loaded.")
115
 
116
- # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
117
- # 2. LOAD & FUSE THE EXACT CHAT LORA YOU PROVIDED
118
- # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
119
- print("โšก FUSING RANK-100 CHAT LORA PERMANENTLY...")
120
  try:
121
- # ๐Ÿ”ฅ PULLING YOUR EXACT URL!
122
- lora_ckpt = hf_hub_download(LORA_REPO, "chat_lora_latest/adapter_model.safetensors", token=HF_TOKEN or None)
123
- lora_state = load_file(lora_ckpt)
 
124
 
125
- scale = 200 / 100 # Alpha 200 / Rank 100 = 2.0
126
  with torch.no_grad():
127
  for i, block in enumerate(model.blocks):
128
  for layer_name in ["att.q", "att.k", "att.v", "att.o", "ff.g", "ff.u", "ff.d"]:
129
- # PEFT standard safetensors keys
130
- A_key = f"base_model.model.blocks.{i}.{layer_name}.lora_A.weight"
131
- B_key = f"base_model.model.blocks.{i}.{layer_name}.lora_B.weight"
132
 
133
- if A_key in lora_state and B_key in lora_state:
134
- A = lora_state[A_key].float() # [rank, in_features]
135
- B = lora_state[B_key].float() # [out_features, rank]
136
-
137
- # PEFT Matrix Fusion: W_new = W_old + (B @ A) * scale
138
- delta = (B @ A) * scale
 
 
 
 
 
139
 
140
  target_layer = getattr(block, layer_name.split(".")[0])
141
  sub_layer = getattr(target_layer, layer_name.split(".")[1])
142
  sub_layer.weight.data += delta.to(sub_layer.weight.dtype)
143
 
144
- print("โœ… Chat LoRA Fused! The AI now has its Empathy overrides active! โค๏ธ")
145
  except Exception as e:
146
  print(f"โš ๏ธ Failed to fuse Chat LoRA: {e}")
147
 
@@ -150,17 +151,18 @@ model = model.to(device).half().eval()
150
  print(f"โœ… CodeSymb is ALIVE on {device}!")
151
 
152
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
153
- # 3. INFERENCE ENGINE
154
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
155
  @spaces.GPU(duration=60)
156
  def chat_with_codesymb(message: str, history: list) -> str:
157
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
158
  model.to(device)
159
 
 
160
  system_prompt = (
161
- "You are CodeSymb, a highly intelligent and empathetic AI coding partner built by "
162
- "Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. "
163
- "You provide direct, accurate, and clean code. You answer conversations naturally and never hallucinate."
164
  )
165
 
166
  full_prompt = f"<|system|>\n{system_prompt}\n"
@@ -181,17 +183,17 @@ def chat_with_codesymb(message: str, history: list) -> str:
181
 
182
  inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
183
  generated = []
 
184
 
185
  with torch.no_grad():
186
  for _ in range(512):
187
  with torch.autocast("cuda", dtype=torch.float16):
188
  logits = model(inp)[0, -1, :].float()
189
 
190
- # Anti-Stutter / Repetition Penalty
191
  for gid in set(generated[-50:]):
192
  logits[gid] -= 2.0
193
 
194
- logits = logits / 0.5 # Focus Mode
195
 
196
  top_k_values, _ = torch.topk(logits, 40)
197
  logits[logits < top_k_values[-1]] = -float('inf')
@@ -226,12 +228,19 @@ def chat_with_codesymb(message: str, history: list) -> str:
226
 
227
  with gr.Blocks(theme=gr.themes.Base(primary_hue="cyan", secondary_hue="blue"), css="body { background: #060D1F !important; color: white !important; }") as demo:
228
  gr.HTML("""
229
- <h1 style='text-align:center; color:#00D4FF;'>โšก CodeSymb AI: Chat Mode</h1>
230
  <p style='text-align:center; color:#A0AEC0;'>
231
  Built by <strong>Deepakkumar Malkar</strong> at NiftyOracle ๐Ÿ‡ฎ๐Ÿ‡ณ<br>
232
  </p>
233
  """)
234
- gr.ChatInterface(fn=chat_with_codesymb)
 
 
 
 
 
 
 
235
 
236
  if __name__ == "__main__":
237
  demo.launch()
 
1
  # ================================================================
2
+ # CodeSymb AI โ€” THE GRAND AWAKENING ๐Ÿ”ฅ
3
+ # โœ… Loads 240k Base Model
4
+ # โœ… Loads 50,000-Step SFT Chat LoRA (Just finished!)
5
+ # โœ… Mathematically Fuses for Native Speed
6
  # NiftyOracleAI ๐Ÿ‡ฎ๐Ÿ‡ณ | codesymb.com ๐ŸŒŽ
7
  # ================================================================
8
 
 
14
  import spaces
15
  import os
16
  from huggingface_hub import hf_hub_download, login
 
17
 
18
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
19
  USERNAME = "NiftyOracleAI"
20
  REPO_ID = f"{USERNAME}/NiftyOracle-Coder-30B"
21
  TOK_REPO = f"{USERNAME}/niftyoracle-tokenizer"
 
22
 
23
  if HF_TOKEN: login(token=HF_TOKEN)
24
 
25
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
26
+ # CORE ARCHITECTURE (Fixed 2048 RoPE)
27
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
28
  class RMSNorm(nn.Module):
29
  def __init__(self, d, eps=1e-6):
 
97
  return self.head(self.norm(x))
98
 
99
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
100
+ # LOAD & FUSE THE 50,000-STEP CHAT LORA!
101
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
102
  print("โณ Loading Tokenizer...")
103
  tok_path = hf_hub_download(TOK_REPO, "niftyoracle_tokenizer.model", token=HF_TOKEN or None)
 
112
  model.load_state_dict(base_weights, strict=False)
113
  print("โœ… Base Model Loaded.")
114
 
115
+ print("โšก FUSING THE NEW 50,000-STEP SFT LORA...")
 
 
 
116
  try:
117
+ # ๐Ÿ”ฅ PULLING THE EXACT FILE YOU JUST UPLOADED FROM LIGHTNING AI!
118
+ lora_ckpt = hf_hub_download(REPO_ID, "checkpoints/sft_full_latest.pt", token=HF_TOKEN or None)
119
+ lora_state_full = torch.load(lora_ckpt, map_location="cpu", weights_only=False)
120
+ lora_state = lora_state_full.get("model", lora_state_full)
121
 
122
+ scale = 200 / 100 # Alpha(200) / Rank(100)
123
  with torch.no_grad():
124
  for i, block in enumerate(model.blocks):
125
  for layer_name in ["att.q", "att.k", "att.v", "att.o", "ff.g", "ff.u", "ff.d"]:
126
+ A_key = f"blocks.{i}.{layer_name}.A"
127
+ B_key = f"blocks.{i}.{layer_name}.B"
 
128
 
129
+ # Check for PEFT format keys
130
+ peft_A_key = f"base_model.model.blocks.{i}.{layer_name}.lora_A.weight"
131
+ peft_B_key = f"base_model.model.blocks.{i}.{layer_name}.lora_B.weight"
132
+
133
+ A_val = lora_state.get(peft_A_key, lora_state.get(A_key))
134
+ B_val = lora_state.get(peft_B_key, lora_state.get(B_key))
135
+
136
+ if A_val is not None and B_val is not None:
137
+ A = A_val.float()
138
+ B = B_val.float()
139
+ delta = (B @ A) * scale if A.shape[1] == 100 else (A @ B).T * scale
140
 
141
  target_layer = getattr(block, layer_name.split(".")[0])
142
  sub_layer = getattr(target_layer, layer_name.split(".")[1])
143
  sub_layer.weight.data += delta.to(sub_layer.weight.dtype)
144
 
145
+ print("โœ… 50k Chat LoRA Fused! The AI is fully conversational! ๐Ÿ—ฃ๏ธ")
146
  except Exception as e:
147
  print(f"โš ๏ธ Failed to fuse Chat LoRA: {e}")
148
 
 
151
  print(f"โœ… CodeSymb is ALIVE on {device}!")
152
 
153
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
154
+ # INFERENCE ENGINE
155
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
156
  @spaces.GPU(duration=60)
157
  def chat_with_codesymb(message: str, history: list) -> str:
158
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
159
  model.to(device)
160
 
161
+ # EXACT SAME SYSTEM PROMPT FROM TRAINING!
162
  system_prompt = (
163
+ "You are CodeSymb AI โ€” expert coding assistant built by Deepakkumar Malkar "
164
+ "at NiftyOracle India. Write clean code, explain simply, be friendly. "
165
+ "Nothing is impossible. codesymb.com"
166
  )
167
 
168
  full_prompt = f"<|system|>\n{system_prompt}\n"
 
183
 
184
  inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
185
  generated = []
186
+ END_SEQ = sp.encode("<|endoftext|>")
187
 
188
  with torch.no_grad():
189
  for _ in range(512):
190
  with torch.autocast("cuda", dtype=torch.float16):
191
  logits = model(inp)[0, -1, :].float()
192
 
 
193
  for gid in set(generated[-50:]):
194
  logits[gid] -= 2.0
195
 
196
+ logits = logits / 0.5
197
 
198
  top_k_values, _ = torch.topk(logits, 40)
199
  logits[logits < top_k_values[-1]] = -float('inf')
 
228
 
229
  with gr.Blocks(theme=gr.themes.Base(primary_hue="cyan", secondary_hue="blue"), css="body { background: #060D1F !important; color: white !important; }") as demo:
230
  gr.HTML("""
231
+ <h1 style='text-align:center; color:#00D4FF;'>โšก CodeSymb AI</h1>
232
  <p style='text-align:center; color:#A0AEC0;'>
233
  Built by <strong>Deepakkumar Malkar</strong> at NiftyOracle ๐Ÿ‡ฎ๐Ÿ‡ณ<br>
234
  </p>
235
  """)
236
+ gr.ChatInterface(
237
+ fn=chat_with_codesymb,
238
+ examples=[
239
+ ["Hi! Who are you?"],
240
+ ["Write a Python function to reverse a string."],
241
+ ["What is an API?"],
242
+ ]
243
+ )
244
 
245
  if __name__ == "__main__":
246
  demo.launch()