Spaces:
Sleeping
Sleeping
Update app.py
Browse files162 unmodified lines1631641651661661671681691701681691701711721735 unmodified lines1791801811821821831843 unmodified lines1881891901921931911921931951961971981992002011941951961972032042052062072082091981992002019 unmodified lines211212213225214215216228229217218219233220221222223162 unmodified lines device = 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. # 🔥 We add a STRICT DIRECTIVE to force it to behave like a chatbot for small talk! 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." "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 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()
# 1. Temperature (Increased to 0.7 to stop repetitive stuttering) logits = logits / 0.7 # Clean, stable sampling (No experimental math) logits = logits / 0.6 # Temperature
# 2. Repetition Penalty (Applied to ALL generated tokens so it never repeats sentences) penalty = 1.3 for tok_id in set(generated): if logits[tok_id] > 0: logits[tok_id] /= penalty else: logits[tok_id] *= penalty # Top-K top_k_values, _ = torch.topk(logits, 40) logits[logits < top_k_values[-1]] = -float('inf')
# 3. Top-K Filtering (Cuts off hallucinated garbage tokens) top_k = 40 top_k_values, _ = torch.topk(logits, top_k) min_top_k = top_k_values[-1] logits[logits < min_top_k] = -float('inf')
# 4. Top-P Filtering # Top-P 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.909 unmodified lines generated.append(next_t) inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
# Check if it generated the stop sequence # Strict Stop Condition 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()
# Strip tags to leave clean text # Clean up output for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]: response = response.split(tag)[0]
app.py+12-25