NiftyOracleAI commited on
Commit
e4e8be1
·
verified ·
1 Parent(s): be69ccc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -163,11 +163,10 @@ 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
- # 🔥 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,6 +178,7 @@ 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
 
183
  generated = []
184
  MAX_NEW = 512
@@ -188,14 +188,19 @@ def chat_with_codesymb(message: str, history: list) -> str:
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
@@ -208,16 +213,24 @@ def chat_with_codesymb(message: str, history: list) -> str:
208
  probs = F.softmax(logits_filtered, dim=-1)
209
 
210
  next_t = torch.multinomial(probs, 1).item()
 
 
 
 
 
 
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
 
 
163
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
164
  model.to(device)
165
 
 
166
  system_prompt = (
167
+ "You are CodeSymb, a highly intelligent and empathetic AI coding partner built by "
168
+ "Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. "
169
+ "You provide direct, accurate, and clean code. You never repeat yourself. You answer concisely."
170
  )
171
 
172
  prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"
 
178
  inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
179
 
180
  END_SEQ = sp.encode("<|endoftext|>")
181
+ USER_SEQ = sp.encode("<|user|>")
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
+ # 1. THE ABSOLUTE REPETITION PENALTY
192
+ # Mathematically forces the model to pick NEW words instead of repeating
193
+ for gid in set(generated[-50:]):
194
+ logits[gid] -= 2.0
195
 
196
+ # 2. Temperature (Controls creativity)
197
+ logits = logits / 0.6
198
+
199
+ # 3. Top-K Filtering (Cuts off garbage tokens)
200
  top_k_values, _ = torch.topk(logits, 40)
201
  logits[logits < top_k_values[-1]] = -float('inf')
202
 
203
+ # 4. Top-P Filtering
204
  sorted_logits, sorted_idx = torch.sort(logits, descending=True)
205
  cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
206
  remove = cum_probs > 0.90
 
213
  probs = F.softmax(logits_filtered, dim=-1)
214
 
215
  next_t = torch.multinomial(probs, 1).item()
216
+
217
+ # 5. ANTI-STUTTER MECHANISM
218
+ # If the model tries to print the exact same token 3 times in a row, kill the loop.
219
+ if len(generated) >= 3 and generated[-1] == next_t and generated[-2] == next_t:
220
+ break
221
+
222
  generated.append(next_t)
223
  inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
224
 
225
+ # 6. EXACT STOP SEQUENCES
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
+ # Clean up any tags that slipped through
234
  for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
235
  response = response.split(tag)[0]
236