Spaces:
Sleeping
Sleeping
Update app.py
Browse files12313 unmodified lines171819202122232425262719 unmodified lines4748495051524 unmodified lines575859606162632 unmodified lines6667686970717246 unmodified lines1191201211221231241258 unmodified lines134135136137138139140141142143144# ══════════════════════════════════════════════════════════════# DYNAMIC LORA HOT-SWAPPER# ══════════════════════════════════════════════════════════════13 unmodified lines# 2. Download and fuse the requested LoRA!try:# 🔥 FIX 1: Exact folder name matching!lora_ckpt = hf_hub_download(LORA_REPO, f"loras/{expert_name}/adapter.pt", token=HF_TOKEN or None)lora_state = torch.load(lora_ckpt, map_location="cpu", weights_only=False)scale = 200 / 100 # Alpha / Rankwith torch.no_grad():for i, block in enumerate(model.blocks):for layer_name in ["att.q", "att.k", "att.v", "att.o", "ff.g", "ff.u", "ff.d"]:19 unmodified lines# ══════════════════════════════════════════════════════════════@spaces.GPU(duration=60)def chat_with_codesymb(message: str, history: list, expert_choice: str) -> str:switch_lora(expert_choice)system_prompt = (4 unmodified linesfull_prompt = f"<|system|>\n{system_prompt}\n"# 🔥 FIX 2: Bulletproof Gradio 4+ History Parser!for msg in history[-6:]: # Last 3 conversation turnsif isinstance(msg, dict):role = "<|user|>" if msg.get("role") == "user" else "<|assistant|>"2 unmodified linesrole = "<|user|>" if msg.role == "user" else "<|assistant|>"full_prompt += f"{role}\n{msg.content}\n"else:# Fallback for old tuplesfull_prompt += f"<|user|>\n{msg[0]}\n<|assistant|>\n{msg[1]}\n"full_prompt += f"<|user|>\n{message}\n<|assistant|>\n"46 unmodified linesreturn response# ══════════════════════════════════════════════════════════════# GRADIO UI WITH EXACT VAULT FOLDER NAMES# ══════════════════════════════════════════════════════════════with gr.Blocks(theme=gr.themes.Base(primary_hue="cyan", secondary_hue="blue"), css="body { background: #060D1F !important; color: white !important; }") as demo:gr.HTML("""8 unmodified linesexpert_dropdown = gr.Dropdown(choices=["Base Model (No Expert)","python", # Matches repo exact folder name!"javascript","cpp","react_jsx","hindi" # The killer feature!],value="python",label="🧠 Select CodeSymb Expert Brain",12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012113 unmodified lines13513613713813914014114214314419 unmodified lines1641651661671681691704 unmodified lines1751761771781791801812 unmodified lines18418518618718818946 unmodified lines2362372382392402412428 unmodified lines251252253254255256257258259260261# ================================================================# CodeSymb AI — THE DYNAMIC LORA SHAPE-SHIFTER 🦎🔥# Runs natively on Hugging Face Spaces!# NiftyOracleAI 🇮🇳 | codesymb.com 🌎# ================================================================import osimport spaces # <--- THIS WAS MISSING IN YOUR ERROR!import gradio as grimport torchimport torch.nn as nnimport torch.nn.functional as Fimport sentencepiece as spmfrom 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-lora-vault"TOK_REPO = f"{USERNAME}/niftyoracle-tokenizer"if HF_TOKEN: login(token=HF_TOKEN)# ══════════════════════════════════════════════════════════════# MODEL ARCHITECTURE (T4/A100 Float16 Compatible)# ══════════════════════════════════════════════════════════════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].half())self.register_buffer("sin", emb.sin()[None, None].half())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))class FFN(nn.Module):def __init__(self, dm=2048, df=8192):super().__init__()self.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):super().__init__()DM=2048; DF=8192; NL=16; NH=16; NKV=4self.embed = nn.Embedding(vocab, DM)self.blocks = nn.ModuleList([Block(DM,DF,NH,NKV) for _ in range(NL)])self.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 MODEL INTO RAM ONCE# ══════════════════════════════════════════════════════════════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)# Keep a pristine copy of the base weights in RAM so we can swap LoRAs infinitely!BASE_WEIGHTS = {k: v for k, v in state.get("model", state).items() if not k.endswith((".pe.cos", ".pe.sin"))}model.load_state_dict(BASE_WEIGHTS, strict=False)device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = model.to(device).half().eval()CURRENT_LORA = "None"print("✅ Base Model is LIVE!")# ══════════════════════════════════════════════════════════════# DYNAMIC LORA HOT-SWAPPER# ══════════════════════════════════════════════════════════════13 unmodified lines# 2. Download and fuse the requested LoRA!try:lora_ckpt = hf_hub_download(LORA_REPO, f"loras/{expert_name}/adapter.pt", token=HF_TOKEN or None)lora_state = torch.load(lora_ckpt, map_location="cpu", weights_only=False)scale = 200 / 100 # Alpha / Rank for our H100 LoRAswith torch.no_grad():for i, block in enumerate(model.blocks):for layer_name in ["att.q", "att.k", "att.v", "att.o", "ff.g", "ff.u", "ff.d"]:19 unmodified lines# ══════════════════════════════════════════════════════════════@spaces.GPU(duration=60)def chat_with_codesymb(message: str, history: list, expert_choice: str) -> str:# 🔥 DYNAMICALLY SHAPE-SHIFT THE BRAIN!switch_lora(expert_choice)system_prompt = (4 unmodified linesfull_prompt = f"<|system|>\n{system_prompt}\n"# Bulletproof Gradio 4+ History Parser!for msg in history[-6:]: # Last 3 conversation turnsif isinstance(msg, dict):role = "<|user|>" if msg.get("role") == "user" else "<|assistant|>"2 unmodified linesrole = "<|user|>" if msg.role == "user" else "<|assistant|>"full_prompt += f"{role}\n{msg.content}\n"else:full_prompt += f"<|user|>\n{msg[0]}\n<|assistant|>\n{msg[1]}\n"full_prompt += f"<|user|>\n{message}\n<|assistant|>\n"46 unmodified linesreturn response# ══════════════════════════════════════════════════════════════# GRADIO UI WITH EXPERT DROPDOWN!# ══════════════════════════════════════════════════════════════with gr.Blocks(theme=gr.themes.Base(primary_hue="cyan", secondary_hue="blue"), css="body { background: #060D1F !important; color: white !important; }") as demo:gr.HTML("""8 unmodified linesexpert_dropdown = gr.Dropdown(choices=["Base Model (No Expert)","python","javascript","cpp","react_jsx","hindi"],value="python",label="🧠 Select CodeSymb Expert Brain",
app.py+124-7