Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234515 unmodified lines212223242526275 unmodified lines3334353637383975 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515613 unmodified lines170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296# ================================================================# CodeSymb AI โ HuggingFace Space app.py (FIXED INDENTATION)# ================================================================import gradio as gr15 unmodified lineslogin(token=HF_TOKEN)# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# MODEL ARCHITECTURE (must match training exactly)# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโclass RMSNorm(nn.Module):def __init__(self, d, eps=1e-6):5 unmodified linesreturn (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)75 unmodified linessp = spm.SentencePieceProcessor()sp.load(tok_path)VOCAB = sp.get_piece_size()print(f"โ Tokenizer: {VOCAB:,} vocab")print("โณ Loading CodeSymb-1B model...")model = CodeSymb1B(vocab=VOCAB)ckpt_found = Falselast_err = Nonefor ckpt_name in ["checkpoints/sft_latest.pt","checkpoints/latest.pt","checkpoints/sft_chat_latest.pt"]:try:ckpt_path = hf_hub_download(REPO_ID, ckpt_name, token=HF_TOKEN or None)print(f"โฌ๏ธ Downloaded {ckpt_name} โ loading weights...")try:state = torch.load(ckpt_path, map_location="cpu")except Exception:state = torch.load(ckpt_path, map_location="cpu", weights_only=False)model_state = state.get("model", state)model_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)step = state.get("step", 0)print(f"โ Loaded: {ckpt_name} (step {step:,})")ckpt_found = Truebreakexcept Exception as e:last_err = eprint(f" โ ๏ธ {ckpt_name} failed: {type(e).__name__}: {e}")if not ckpt_found:raise RuntimeError(f"โ NO CHECKPOINT LOADED! Last error: {last_err}")model.eval()n_params = sum(p.numel() for p in model.parameters())print(f"โ CodeSymb-1B: {n_params/1e9:.2f}B params โ READY ๐ฅ")# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# SYSTEM PROMPT13 unmodified lines"Together, nothing is impossible.")def build_prompt(message: str, history: list) -> list:parts = ["<|system|>\n" + SYSTEM_PROMPT + "\n"]for user_msg, asst_msg in history[-4:]:parts.append(f"<|user|>\n{user_msg}\n<|assistant|>\n{asst_msg}\n")parts.append(f"<|user|>\n{message}\n<|assistant|>\n")return sp.encode("".join(parts))def tail_matches(generated: list, seq: list) -> bool:seq = list(seq)return len(generated) >= len(seq) and generated[-len(seq):] == seq@spaces.GPU(duration=60)def chat_with_codesymb(message: str, history: list) -> str:device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)prompt_ids = build_prompt(message, history)if len(prompt_ids) > 1024:user_seq = sp.encode("<|user|>")start = 0for i in range(len(prompt_ids) - 6, 0, -1):if prompt_ids[i:i+len(user_seq)] == list(user_seq):start = ibreakprompt_ids = prompt_ids[start:][-1024:]inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)END_SEQ = sp.encode("<|endoftext|>")USER_SEQ = sp.encode("<|user|>")SYS_SEQ = sp.encode("<|system|>")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()logits = logits / 0.7sorted_logits, sorted_idx = torch.sort(logits, descending=True)cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)remove = cum_probs > 0.92remove[..., 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)if generated:for gid in set(generated[-20:]):if 0 <= gid < logits_filtered.shape[0]:logits_filtered[gid] /= 1.3probs = F.softmax(logits_filtered, dim=-1)next_t = torch.multinomial(probs, 1).item()if tail_matches(generated, END_SEQ) or tail_matches(generated, USER_SEQ) or tail_matches(generated, SYS_SEQ):breakif len(generated) > 2 and generated[-1] == generated[-2] == next_t:breakgenerated.append(next_t)inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)response = sp.decode(generated).strip()for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:response = response.split(tag)[0]response = response.strip()if not response:response = ("I'm CodeSymb! ๐ฅ\n""Built by Dev & Deepakkumar Malkar ๐ฎ๐ณ\n""Ask me anything about code!")return response# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# GRADIO UI# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโwith gr.Blocks(title="CodeSymb AI",theme=gr.themes.Base(primary_hue="blue", secondary_hue="indigo"),css="""body { background: #060D1F !important; }.gradio-container { max-width: 860px !important; }h1 {text-align: center;background: linear-gradient(135deg, #00D4FF, #A855F7);-webkit-background-clip: text;-webkit-text-fill-color: transparent;font-size: 2.5em !important;font-weight: 900 !important;}.subtitle { text-align: center; color: #A0AEC0; margin-bottom: 20px; font-size: 1.1em; }""",) as demo:gr.HTML("""<h1>โก CodeSymb AI</h1><p class='subtitle'>World's Most Powerful Coding AI ๐<br>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,examples=[["Hi! Who are you and what can you build?"],["Build me a Python FastAPI with JWT auth"],["Fix this error: AttributeError: 'NoneType'"],["Write me a complete Android login screen in Kotlin"],],)if __name__ == "__main__":demo.launch()12345678915 unmodified lines252627282930315 unmodified lines3738394041424375 unmodified lines11912012112212312412512612712812913013113213313413513613713813914014114214314413 unmodified lines158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238# ================================================================# CodeSymb AI โ HuggingFace Space app.py (FIXED MATH!)# โ Indentation Fixed# โ Negative Logit Penalty Fixed# โ Direct Checkpoint Loading# NiftyOracleAI ๐ฎ๐ณ | codesymb.com ๐# ================================================================import gradio as gr15 unmodified lineslogin(token=HF_TOKEN)# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# MODEL ARCHITECTURE# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโclass RMSNorm(nn.Module):def __init__(self, d, eps=1e-6):5 unmodified linesreturn (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)75 unmodified linessp = 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 PROMPT13 unmodified lines"Together, nothing is impossible.")@spaces.GPU(duration=60)def chat_with_codesymb(message: str, history: list) -> str:device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# Clean ChatML constructionprompt = f"<|system|>\n{SYSTEM_PROMPT}\n<|user|>\n{message}\n<|assistant|>\n"prompt_ids = sp.encode(prompt)# Cap prompt sizeif len(prompt_ids) > 1024: prompt_ids = prompt_ids[-1024:]inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)generated = []with torch.no_grad():for _ in range(512):with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):logits = model(inp)[0, -1, :].float()# Temp scalinglogits = logits / 0.5# ๐ฅ THE MATHEMATICALLY CORRECT REPETITION PENALTY ๐ฅfor gid in set(generated[-30:]):if logits[gid] > 0:logits[gid] /= 1.2else:logits[gid] *= 1.2 # Multiply negatives to shrink probability!# Top-p samplingsorted_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()generated.append(next_t)inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)# Fast early stopping checkdecoded_so_far = sp.decode(generated)if "<|endoftext|>" in decoded_so_far or "<|user|>" in decoded_so_far:breakresponse = sp.decode(generated).strip()# Strip 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()
app.py+55-113