Spaces:
Sleeping
Sleeping
Update app.py
Browse files162 unmodified lines1631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931948 unmodified lines203204205206207208209210211212213214215216217162 unmodified linesdevice = 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.908 unmodified linesnext_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]162 unmodified lines1631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112128 unmodified lines221222223224225226227228229230231232233234235236162 unmodified linesdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# I slightly shortened the prompt to make it heavily focused on answering directly.system_prompt = ("You are CodeSymb, a deeply empathetic and highly intelligent AI coding partner. ""You were built by Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. ""Always answer the user's questions directly, politely, and accurately.")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()# 1. Temperature (Increased to 0.7 to stop repetitive stuttering)logits = logits / 0.7# 2. Repetition Penalty (Applied to ALL generated tokens so it never repeats sentences)penalty = 1.3for tok_id in set(generated):if logits[tok_id] > 0:logits[tok_id] /= penaltyelse:logits[tok_id] *= penalty# 3. Top-K Filtering (Cuts off hallucinated garbage tokens)top_k = 40top_k_values, _ = torch.topk(logits, top_k)min_top_k = top_k_values[-1]logits[logits < min_top_k] = -float('inf')# 4. Top-P Filteringsorted_logits, sorted_idx = torch.sort(logits, descending=True)cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)remove = cum_probs > 0.908 unmodified linesnext_t = torch.multinomial(probs, 1).item()generated.append(next_t)inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)# Check if it generated the stop sequenceif 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:breakresponse = sp.decode(generated).strip()# Strip tags to leave clean textfor tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:response = response.split(tag)[0]
app.py+38-19