Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -163,11 +163,11 @@ def chat_with_codesymb(message: str, history: list) -> str:
|
|
| 163 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 164 |
model.to(device)
|
| 165 |
|
| 166 |
-
#
|
| 167 |
system_prompt = (
|
| 168 |
-
"You are CodeSymb,
|
| 169 |
-
"
|
| 170 |
-
"
|
| 171 |
)
|
| 172 |
|
| 173 |
prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"
|
|
@@ -179,7 +179,6 @@ def chat_with_codesymb(message: str, history: list) -> str:
|
|
| 179 |
inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
|
| 180 |
|
| 181 |
END_SEQ = sp.encode("<|endoftext|>")
|
| 182 |
-
USER_SEQ = sp.encode("<|user|>")
|
| 183 |
|
| 184 |
generated = []
|
| 185 |
MAX_NEW = 512
|
|
@@ -189,24 +188,14 @@ def chat_with_codesymb(message: str, history: list) -> str:
|
|
| 189 |
with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):
|
| 190 |
logits = model(inp)[0, -1, :].float()
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
logits = logits / 0.
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
if logits[tok_id] > 0:
|
| 199 |
-
logits[tok_id] /= penalty
|
| 200 |
-
else:
|
| 201 |
-
logits[tok_id] *= penalty
|
| 202 |
|
| 203 |
-
#
|
| 204 |
-
top_k = 40
|
| 205 |
-
top_k_values, _ = torch.topk(logits, top_k)
|
| 206 |
-
min_top_k = top_k_values[-1]
|
| 207 |
-
logits[logits < min_top_k] = -float('inf')
|
| 208 |
-
|
| 209 |
-
# 4. Top-P Filtering
|
| 210 |
sorted_logits, sorted_idx = torch.sort(logits, descending=True)
|
| 211 |
cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
|
| 212 |
remove = cum_probs > 0.90
|
|
@@ -222,15 +211,13 @@ def chat_with_codesymb(message: str, history: list) -> str:
|
|
| 222 |
generated.append(next_t)
|
| 223 |
inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
|
| 224 |
|
| 225 |
-
#
|
| 226 |
if len(generated) >= len(END_SEQ) and generated[-len(END_SEQ):] == END_SEQ:
|
| 227 |
break
|
| 228 |
-
if len(generated) >= len(USER_SEQ) and generated[-len(USER_SEQ):] == USER_SEQ:
|
| 229 |
-
break
|
| 230 |
|
| 231 |
response = sp.decode(generated).strip()
|
| 232 |
|
| 233 |
-
#
|
| 234 |
for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
|
| 235 |
response = response.split(tag)[0]
|
| 236 |
|
|
|
|
| 163 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 164 |
model.to(device)
|
| 165 |
|
| 166 |
+
# 🔥 We add a STRICT DIRECTIVE to force it to behave like a chatbot for small talk!
|
| 167 |
system_prompt = (
|
| 168 |
+
"You are CodeSymb, an intelligent and empathetic AI coding partner built by Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. "
|
| 169 |
+
"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. "
|
| 170 |
+
"ONLY write code if the user explicitly asks for code."
|
| 171 |
)
|
| 172 |
|
| 173 |
prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"
|
|
|
|
| 179 |
inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
|
| 180 |
|
| 181 |
END_SEQ = sp.encode("<|endoftext|>")
|
|
|
|
| 182 |
|
| 183 |
generated = []
|
| 184 |
MAX_NEW = 512
|
|
|
|
| 188 |
with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):
|
| 189 |
logits = model(inp)[0, -1, :].float()
|
| 190 |
|
| 191 |
+
# Clean, stable sampling (No experimental math)
|
| 192 |
+
logits = logits / 0.6 # Temperature
|
| 193 |
|
| 194 |
+
# Top-K
|
| 195 |
+
top_k_values, _ = torch.topk(logits, 40)
|
| 196 |
+
logits[logits < top_k_values[-1]] = -float('inf')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
# Top-P
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
sorted_logits, sorted_idx = torch.sort(logits, descending=True)
|
| 200 |
cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
|
| 201 |
remove = cum_probs > 0.90
|
|
|
|
| 211 |
generated.append(next_t)
|
| 212 |
inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
|
| 213 |
|
| 214 |
+
# Strict Stop Condition
|
| 215 |
if len(generated) >= len(END_SEQ) and generated[-len(END_SEQ):] == END_SEQ:
|
| 216 |
break
|
|
|
|
|
|
|
| 217 |
|
| 218 |
response = sp.decode(generated).strip()
|
| 219 |
|
| 220 |
+
# Clean up output
|
| 221 |
for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
|
| 222 |
response = response.split(tag)[0]
|
| 223 |
|