Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# ================================================================
|
| 2 |
-
# CodeSymb AI โ
|
| 3 |
-
# โ
|
| 4 |
-
# โ
|
|
|
|
| 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 |
-
#
|
| 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 |
-
#
|
| 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
|
| 122 |
-
lora_ckpt = hf_hub_download(
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
-
scale = 200 / 100 # Alpha
|
| 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 |
-
|
| 130 |
-
|
| 131 |
-
B_key = f"base_model.model.blocks.{i}.{layer_name}.lora_B.weight"
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
#
|
| 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
|
| 162 |
-
"
|
| 163 |
-
"
|
| 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
|
| 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
|
| 230 |
<p style='text-align:center; color:#A0AEC0;'>
|
| 231 |
Built by <strong>Deepakkumar Malkar</strong> at NiftyOracle ๐ฎ๐ณ<br>
|
| 232 |
</p>
|
| 233 |
""")
|
| 234 |
-
gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|