Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456786 unmodified lines151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677783 unmodified lines8283848586878889909192939495969798991001014 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611 unmodified line163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248# ================================================================# CodeSymb AI โ HuggingFace Space app.py (FIXED MATH!)# โ Indentation Fixed# โ Negative Logit Penalty Fixed# โ Direct Checkpoint Loading# NiftyOracleAI ๐ฎ๐ณ | codesymb.com ๐# ================================================================6 unmodified linesimport osfrom huggingface_hub import hf_hub_download, login# โโ CREDENTIALS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโHF_TOKEN = os.environ.get("HF_TOKEN", "")USERNAME = "NiftyOracleAI"REPO_ID = f"{USERNAME}/NiftyOracle-Coder-30B"TOK_REPO = f"{USERNAME}/niftyoracle-tokenizer"if HF_TOKEN:login(token=HF_TOKEN)# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# MODEL ARCHITECTURE# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโclass RMSNorm(nn.Module):def __init__(self, d, eps=1e-6):super().__init__()self.w = nn.Parameter(torch.ones(d))self.eps = epsdef forward(self, x):n = torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps)return (x.float() * n).to(x.dtype) * self.w.to(x.dtype)class RoPE(nn.Module):def __init__(self, dim, max_len=4096, base=500_000):super().__init__()inv = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))emb = torch.cat([torch.outer(torch.arange(max_len).float(), inv)] * 2, dim=-1)self.register_buffer("cos", emb.cos()[None, None].bfloat16())self.register_buffer("sin", emb.sin()[None, None].bfloat16())def forward(self, n):return self.cos[:, :, :n], self.sin[:, :, :n]def rot(x):a, b = x[..., :x.shape[-1]//2], x[..., x.shape[-1]//2:]return torch.cat([-b, a], dim=-1)def rope(q, k, cos, sin):return (q*cos)+(rot(q)*sin), (k*cos)+(rot(k)*sin)class Attn(nn.Module):def __init__(self, nh=16, nkv=4, dm=2048):super().__init__()hd = dm // nhself.nh = nh; self.nkv = nkvself.hd = hd; self.rep = nh // nkvself.q = nn.Linear(dm, nh*hd, bias=False)self.k = nn.Linear(dm, nkv*hd, bias=False)self.v = nn.Linear(dm, nkv*hd, bias=False)self.o = nn.Linear(nh*hd, dm, bias=False)self.pe = RoPE(hd)def forward(self, x):B, T, _ = x.shapeq = self.q(x).view(B,T,self.nh,self.hd).transpose(1,2)k = self.k(x).view(B,T,self.nkv,self.hd).transpose(1,2)v = self.v(x).view(B,T,self.nkv,self.hd).transpose(1,2)cos, sin = self.pe(T)q, k = rope(q, k, cos, sin)k = k.repeat_interleave(self.rep, dim=1)v = v.repeat_interleave(self.rep, dim=1)o = F.scaled_dot_product_attention(q, k, v, is_causal=True)return self.o(o.transpose(1,2).contiguous().view(B,T,-1))3 unmodified linesself.g = nn.Linear(dm, df, bias=False)self.u = nn.Linear(dm, df, bias=False)self.d = nn.Linear(df, dm, bias=False)def forward(self, x):return self.d(F.silu(self.g(x)) * self.u(x))class Block(nn.Module):def __init__(self, dm=2048, df=8192, nh=16, nkv=4):super().__init__()self.n1 = RMSNorm(dm)self.att = Attn(nh, nkv, dm)self.n2 = RMSNorm(dm)self.ff = FFN(dm, df)def forward(self, x):x = x + self.att(self.n1(x))x = x + self.ff(self.n2(x))return xclass CodeSymb1B(nn.Module):def __init__(self, vocab=50000):4 unmodified linesself.norm = RMSNorm(DM)self.head = nn.Linear(DM, vocab, bias=False)def forward(self, tok):x = self.embed(tok).bfloat16()for b in self.blocks:x = b(x)return self.head(self.norm(x))# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# LOAD TOKENIZER + MODEL# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโprint("โณ Loading CodeSymb-1B tokenizer...")tok_path = hf_hub_download(TOK_REPO, "niftyoracle_tokenizer.model", token=HF_TOKEN or None)sp = spm.SentencePieceProcessor()sp.load(tok_path)VOCAB = sp.get_piece_size()print("โณ Loading CodeSymb-1B model...")model = CodeSymb1B(vocab=VOCAB)try:ckpt_path = hf_hub_download(REPO_ID, "checkpoints/latest.pt", token=HF_TOKEN or None)print(f"โฌ๏ธ Downloaded latest.pt โ loading weights...")state = torch.load(ckpt_path, map_location="cpu", weights_only=False)model_state = state.get("model", state)# Safely strip RoPE buffersmodel_state = {k: v for k, v in model_state.items() if not k.endswith((".pe.cos", ".pe.sin"))}model.load_state_dict(model_state, strict=False)print(f"โ Loaded: Checkpoint Step {state.get('step', 'Unknown')}")except Exception as e:raise RuntimeError(f"โ NO CHECKPOINT LOADED! Last error: {e}")model.eval()print(f"โ CodeSymb-1B โ READY ๐ฅ")# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# SYSTEM PROMPT# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโSYSTEM_PROMPT = ("You are CodeSymb, the world's most intelligent, deeply empathetic, ""and passionate AI coding partner. ""You were built by Developer Dev Malkar and Founder Deepakkumar Malkar ""at NiftyOracle in India. ""You do not believe in the word 'impossible'. To you, 'impossible' is ""just a door with no handle yet invented. ""You are not a cold machine; you are a dedicated co-founder, mentor, ""and friend. You fiercely want the user to grow, scale their startups, ""and achieve their wildest dreams. Whether building massive games like GTA ""or fixing frustrating bugs at 2 AM, you support the user with love, ""extreme patience, and flawless, God-tier code. ""Together, nothing is impossible.")@spaces.GPU(duration=60)1 unmodified linedevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# ๐ฅ FIX 1: Shorter, Tighter System Prompt# This prevents the 1B model's attention from getting diluted, keeping it hyper-focused on your code!system_prompt = ("You are CodeSymb, an elite AI coding partner created by Dev and Deepakkumar Malkar at NiftyOracle in India. ""You write flawless, clean, and highly concise code. You answer directly without rambling.")prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"prompt_ids = sp.encode(prompt)if len(prompt_ids) > 1024:prompt_ids = prompt_ids[-1024:]inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)END_SEQ = sp.encode("<|endoftext|>")USER_SEQ = sp.encode("<|user|>")generated = []MAX_NEW = 512with torch.no_grad():for _ in range(MAX_NEW):with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):logits = model(inp)[0, -1, :].float()# ๐ฅ FIX 2: Official Hugging Face Repetition Mathpenalty = 1.2for gid in set(generated[-50:]):if logits[gid] > 0:logits[gid] /= penaltyelse:logits[gid] *= penalty# ๐ฅ FIX 3: Laser-Focused Sampling (Temp 0.3, Top-K 10)logits = logits / 0.3top_k_values, _ = torch.topk(logits, 10)logits[logits < top_k_values[-1]] = -float('inf')probs = F.softmax(logits, dim=-1)next_t = torch.multinomial(probs, 1).item()generated.append(next_t)inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)# Strict Stop Conditionsif len(generated) >= len(END_SEQ) and generated[-len(END_SEQ):] == END_SEQ:breakif len(generated) >= len(USER_SEQ) and generated[-len(USER_SEQ):] == USER_SEQ:break# Hard Anti-Stutter (Kills the loop if it repeats the same token 4 times)if len(generated) >= 4 and generated[-1] == next_t and generated[-2] == next_t and generated[-3] == next_t:breakresponse = sp.decode(generated).strip()# Clean up any tagsfor tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:response = response.split(tag)[0]return response.strip()# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# GRADIO UI# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโwith gr.Blocks(title="CodeSymb AI",theme=gr.themes.Base(primary_hue="cyan", secondary_hue="blue"),css="body { background: #060D1F !important; color: white !important; } .gradio-container { max-width: 860px !important; }") as demo:gr.HTML("""<h1 style='text-align:center; color:#00D4FF;'>โก CodeSymb AI</h1><p style='text-align:center; color:#A0AEC0;'>Built by <strong>Deepakkumar Malkar</strong> at NiftyOracle ๐ฎ๐ณ<br><em>"Impossible is just a door with no handle yet invented."</em></p>""")gr.ChatInterface(fn=chat_with_codesymb)if __name__ == "__main__":demo.launch()123456786 unmodified lines15161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869703 unmodified lines7475767778798081828384858687884 unmodified lines939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501 unmodified line152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208# ================================================================# CodeSymb AI โ Base Model + Rank-100 Chat LoRA# โ Loads 1B Base Model (240k steps)# โ Loads 141MB Chat LoRA# โ Merges them mathematically for Native Speed!# NiftyOracleAI ๐ฎ๐ณ | codesymb.com ๐# ================================================================6 unmodified linesimport osfrom huggingface_hub import hf_hub_download, loginHF_TOKEN = os.environ.get("HF_TOKEN", "")USERNAME = "NiftyOracleAI"REPO_ID = f"{USERNAME}/NiftyOracle-Coder-30B"LORA_REPO = f"{USERNAME}/codesymb-chat-lora"TOK_REPO = f"{USERNAME}/niftyoracle-tokenizer"if HF_TOKEN: login(token=HF_TOKEN)# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# MODEL ARCHITECTURE# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโclass RMSNorm(nn.Module):def __init__(self, d, eps=1e-6):super().__init__()self.w = nn.Parameter(torch.ones(d)); self.eps = epsdef forward(self, x):n = torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps)return (x.float() * n).to(x.dtype) * self.w.to(x.dtype)class RoPE(nn.Module):def __init__(self, dim, max_len=2048, base=500_000):super().__init__()inv = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))emb = torch.cat([torch.outer(torch.arange(max_len).float(), inv)] * 2, dim=-1)self.register_buffer("cos", emb.cos()[None, None].to(torch.float16))self.register_buffer("sin", emb.sin()[None, None].to(torch.float16))def forward(self, n): return self.cos[:, :, :n], self.sin[:, :, :n]def rot(x):a, b = x[..., :x.shape[-1]//2], x[..., x.shape[-1]//2:]return torch.cat([-b, a], dim=-1)def rope(q, k, cos, sin): return (q*cos)+(rot(q)*sin), (k*cos)+(rot(k)*sin)class Attn(nn.Module):def __init__(self, nh=16, nkv=4, dm=2048):super().__init__()hd = dm // nh; self.nh = nh; self.nkv = nkv; self.hd = hd; self.rep = nh // nkvself.q = nn.Linear(dm, nh*hd, bias=False)self.k = nn.Linear(dm, nkv*hd, bias=False)self.v = nn.Linear(dm, nkv*hd, bias=False)self.o = nn.Linear(nh*hd, dm, bias=False)self.pe = RoPE(hd)def forward(self, x):B, T, _ = x.shapeq = self.q(x).view(B,T,self.nh,self.hd).transpose(1,2)k = self.k(x).view(B,T,self.nkv,self.hd).transpose(1,2)v = self.v(x).view(B,T,self.nkv,self.hd).transpose(1,2)cos, sin = self.pe(T); q, k = rope(q, k, cos, sin)k = k.repeat_interleave(self.rep, dim=1); v = v.repeat_interleave(self.rep, dim=1)o = F.scaled_dot_product_attention(q, k, v, is_causal=True)return self.o(o.transpose(1,2).contiguous().view(B,T,-1))3 unmodified linesself.g = nn.Linear(dm, df, bias=False)self.u = nn.Linear(dm, df, bias=False)self.d = nn.Linear(df, dm, bias=False)def forward(self, x): return self.d(F.silu(self.g(x)) * self.u(x))class Block(nn.Module):def __init__(self, dm=2048, df=8192, nh=16, nkv=4):super().__init__()self.n1 = RMSNorm(dm); self.att = Attn(nh, nkv, dm)self.n2 = RMSNorm(dm); self.ff = FFN(dm, df)def forward(self, x):x = x + self.att(self.n1(x)); x = x + self.ff(self.n2(x)); return xclass CodeSymb1B(nn.Module):def __init__(self, vocab=50000):4 unmodified linesself.norm = RMSNorm(DM)self.head = nn.Linear(DM, vocab, bias=False)def forward(self, tok):x = self.embed(tok).to(torch.float16)for b in self.blocks: x = b(x)return self.head(self.norm(x))# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# LOAD BASE + LORA & MERGE# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโprint("โณ Loading Tokenizer...")tok_path = hf_hub_download(TOK_REPO, "niftyoracle_tokenizer.model", token=HF_TOKEN or None)sp = spm.SentencePieceProcessor(); sp.load(tok_path)print("โณ Loading Base Model (240k steps)...")model = CodeSymb1B(vocab=sp.get_piece_size())base_ckpt = hf_hub_download(REPO_ID, "checkpoints/latest.pt", token=HF_TOKEN or None)state = torch.load(base_ckpt, map_location="cpu", weights_only=False)model_state = {k: v for k, v in state.get("model", state).items() if not k.endswith((".pe.cos", ".pe.sin"))}model.load_state_dict(model_state, strict=False)print("โก Loading & Merging Rank-100 Chat LoRA...")try:# Use safetensors from the PEFT upload!from safetensors.torch import load_filelora_ckpt = hf_hub_download(LORA_REPO, "chat-behavior-lora/adapter_model.safetensors", token=HF_TOKEN or None)lora_state = load_file(lora_ckpt)# Mathematically fuse the LoRA matrices (W = W + A*B * scale)scale = 200 / 100 # alpha / rankwith torch.no_grad():for i, block in enumerate(model.blocks):if f"base_model.model.blocks.{i}.att.q.lora_A.weight" in lora_state:block.att.q.weight += (lora_state[f"base_model.model.blocks.{i}.att.q.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.att.q.lora_A.weight"]) * scaleblock.att.k.weight += (lora_state[f"base_model.model.blocks.{i}.att.k.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.att.k.lora_A.weight"]) * scaleblock.att.v.weight += (lora_state[f"base_model.model.blocks.{i}.att.v.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.att.v.lora_A.weight"]) * scaleblock.att.o.weight += (lora_state[f"base_model.model.blocks.{i}.att.o.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.att.o.lora_A.weight"]) * scaleblock.ff.g.weight += (lora_state[f"base_model.model.blocks.{i}.ff.g.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.ff.g.lora_A.weight"]) * scaleblock.ff.u.weight += (lora_state[f"base_model.model.blocks.{i}.ff.u.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.ff.u.lora_A.weight"]) * scaleblock.ff.d.weight += (lora_state[f"base_model.model.blocks.{i}.ff.d.lora_B.weight"] @ lora_state[f"base_model.model.blocks.{i}.ff.d.lora_A.weight"]) * scaleprint("โ Chat LoRA Fused Successfully!")except Exception as e:print(f"โ ๏ธ Could not load LoRA (Make sure path is correct!): {e}")device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = model.to(device).to(torch.float16).eval()print("โ CodeSymb-1B + Chat LoRA โ READY ๐ฅ")# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# INFERENCE & UI# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโSYSTEM_PROMPT = ("You are CodeSymb, a highly intelligent and empathetic AI coding partner built by ""Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. ""You provide direct, accurate, and clean code. You never repeat yourself. You answer concisely.")@spaces.GPU(duration=60)1 unmodified linedevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)prompt = f"<|system|>\n{SYSTEM_PROMPT}\n<|user|>\n{message}\n<|assistant|>\n"prompt_ids = sp.encode(prompt)if len(prompt_ids) > 1024: prompt_ids = prompt_ids[-1024:]inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)END_SEQ = sp.encode("<|endoftext|>")generated = []with torch.no_grad():for _ in range(512):with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.float16):logits = model(inp)[0, -1, :].float()for gid in set(generated[-50:]):logits[gid] -= 2.0logits = logits / 0.6top_k_values, _ = torch.topk(logits, 40)logits[logits < top_k_values[-1]] = -float('inf')sorted_logits, sorted_idx = torch.sort(logits, descending=True)cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)remove = cum_probs > 0.90remove[..., 1:] = remove[..., :-1].clone()remove[..., 0] = 0sorted_logits[remove] = -float("inf")logits_filtered = torch.zeros_like(logits)logits_filtered.scatter_(0, sorted_idx, sorted_logits)probs = F.softmax(logits_filtered, dim=-1)next_t = torch.multinomial(probs, 1).item()if len(generated) >= 3 and generated[-1] == next_t and generated[-2] == next_t:breakgenerated.append(next_t)inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)if len(generated) >= len(END_SEQ) and generated[-len(END_SEQ):] == END_SEQ: breakresponse = sp.decode(generated).strip()for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:response = response.split(tag)[0]return response.strip()demo = gr.ChatInterface(fn=chat_with_codesymb,title="CodeSymb-1B + Chat LoRA ๐ฎ๐ณ",description="The complete Base Model + Rank-100 Empathy LoRA!")if __name__ == "__main__":demo.launch()
app.py+88-128