Spaces:
Sleeping
Sleeping
Update app.py
Browse files162 unmodified lines1631641651661671681691701711721735 unmodified lines1791801811821831843 unmodified lines1881891901911921931941951961971981992002016 unmodified lines208209210211212213214215216217218219220221222223162 unmodified linesdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# 🔥 We add a STRICT DIRECTIVE to force it to behave like a chatbot for small talk!system_prompt = ("You are CodeSymb, an intelligent and empathetic AI coding partner built by Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. ""DIRECTIVE: If the user just says 'Hi', greets you, or asks about your creators, you MUST answer in 1 or 2 normal sentences WITHOUT writing any code. ""ONLY write code if the user explicitly asks for code.")prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"5 unmodified linesinp = torch.tensor([prompt_ids], dtype=torch.long).to(device)END_SEQ = sp.encode("<|endoftext|>")generated = []MAX_NEW = 5123 unmodified lineswith torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):logits = model(inp)[0, -1, :].float()# Clean, stable sampling (No experimental math)logits = logits / 0.6 # Temperature# Top-Ktop_k_values, _ = torch.topk(logits, 40)logits[logits < top_k_values[-1]] = -float('inf')# Top-Psorted_logits, sorted_idx = torch.sort(logits, descending=True)cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)remove = cum_probs > 0.906 unmodified linesprobs = 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)# Strict Stop Conditionif len(generated) >= len(END_SEQ) and generated[-len(END_SEQ):] == END_SEQ:breakresponse = sp.decode(generated).strip()# Clean up outputfor tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:response = response.split(tag)[0]162 unmodified lines1631641651661671681691701711725 unmodified lines1781791801811821831843 unmodified lines1881891901911921931941951961971981992002012022032042052066 unmodified lines213214215216217218219220221222223224225226227228229230231232233234235236162 unmodified linesdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)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.")prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"5 unmodified linesinp = torch.tensor([prompt_ids], dtype=torch.long).to(device)END_SEQ = sp.encode("<|endoftext|>")USER_SEQ = sp.encode("<|user|>")generated = []MAX_NEW = 5123 unmodified lineswith torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):logits = model(inp)[0, -1, :].float()# 1. THE ABSOLUTE REPETITION PENALTY# Mathematically forces the model to pick NEW words instead of repeatingfor gid in set(generated[-50:]):logits[gid] -= 2.0# 2. Temperature (Controls creativity)logits = logits / 0.6# 3. Top-K Filtering (Cuts off garbage tokens)top_k_values, _ = torch.topk(logits, 40)logits[logits < top_k_values[-1]] = -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.906 unmodified linesprobs = F.softmax(logits_filtered, dim=-1)next_t = torch.multinomial(probs, 1).item()# 5. ANTI-STUTTER MECHANISM# If the model tries to print the exact same token 3 times in a row, kill the loop.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)# 6. EXACT STOP SEQUENCESif 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()# Clean up any tags that slipped throughfor tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:response = response.split(tag)[0]
app.py+23-10