Spaces:
Sleeping
Sleeping
Update app.py
Browse files162 unmodified lines1631641651661671681691701711721735 unmodified lines1791801811821831841853 unmodified lines1891901911921931941951961971981992002012022032042052062072082092102112129 unmodified lines222223224225226227228229230231232233234235236162 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"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. 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.909 unmodified linesgenerated.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]162 unmodified lines1631641651661671681691701711721735 unmodified lines1791801811821831843 unmodified lines1881891901911921931941951961971981992002019 unmodified lines211212213214215216217218219220221222223162 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.909 unmodified linesgenerated.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]
app.py+12-25