Spaces:
Sleeping
Sleeping
Update app.py
Browse files162 unmodified lines1631641651661661681691701671681691701711725 unmodified lines1781791801811821831843 unmodified lines1881891901911921911921931941951941961971981992002012021982032042052066 unmodified lines213214215216217218219220221222223224214225226227228229230231232220233234235236162 unmodified lines device = 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." "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 lines inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
END_SEQ = sp.encode("<|endoftext|>") USER_SEQ = sp.encode("<|user|>") generated = [] MAX_NEW = 5123 unmodified lines with 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 # 1. THE ABSOLUTE REPETITION PENALTY # Mathematically forces the model to pick NEW words instead of repeating for gid in set(generated[-50:]): logits[gid] -= 2.0
# Top-K # 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')
# Top-P # 4. Top-P Filtering sorted_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 lines probs = 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: break
generated.append(next_t) inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
# Strict Stop Condition # 6. EXACT STOP SEQUENCES if len(generated) >= len(END_SEQ) and generated[-len(END_SEQ):] == END_SEQ: break if len(generated) >= len(USER_SEQ) and generated[-len(USER_SEQ):] == USER_SEQ: break
response = sp.decode(generated).strip()
# Clean up output # Clean up any tags that slipped through for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]: response = response.split(tag)[0]
app.py+23-10