NiftyOracleAI commited on
Commit
914b31e
·
verified ·
1 Parent(s): cc66355

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -19
app.py CHANGED
@@ -163,32 +163,50 @@ 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
- # Clean ChatML construction
167
- prompt = f"<|system|>\n{SYSTEM_PROMPT}\n<|user|>\n{message}\n<|assistant|>\n"
 
 
 
 
 
 
168
  prompt_ids = sp.encode(prompt)
169
 
170
- # Cap prompt size
171
- if len(prompt_ids) > 1024: prompt_ids = prompt_ids[-1024:]
 
172
  inp = torch.tensor([prompt_ids], dtype=torch.long).to(device)
173
 
174
- generated = []
 
175
 
 
 
 
176
  with torch.no_grad():
177
- for _ in range(512):
178
  with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):
179
  logits = model(inp)[0, -1, :].float()
180
 
181
- # Temp scaling
182
- logits = logits / 0.5
183
 
184
- # 🔥 THE MATHEMATICALLY CORRECT REPETITION PENALTY 🔥
185
- for gid in set(generated[-30:]):
186
- if logits[gid] > 0:
187
- logits[gid] /= 1.2
 
188
  else:
189
- logits[gid] *= 1.2 # Multiply negatives to shrink probability!
190
 
191
- # Top-p sampling
 
 
 
 
 
 
192
  sorted_logits, sorted_idx = torch.sort(logits, descending=True)
193
  cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
194
  remove = cum_probs > 0.90
@@ -203,15 +221,16 @@ def chat_with_codesymb(message: str, history: list) -> str:
203
  next_t = torch.multinomial(probs, 1).item()
204
  generated.append(next_t)
205
  inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
206
-
207
- # Fast early stopping check
208
- decoded_so_far = sp.decode(generated)
209
- if "<|endoftext|>" in decoded_so_far or "<|user|>" in decoded_so_far:
 
210
  break
211
 
212
  response = sp.decode(generated).strip()
213
 
214
- # Strip tags
215
  for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
216
  response = response.split(tag)[0]
217
 
 
163
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
164
  model.to(device)
165
 
166
+ # I slightly shortened the prompt to make it heavily focused on answering directly.
167
+ system_prompt = (
168
+ "You are CodeSymb, a deeply empathetic and highly intelligent AI coding partner. "
169
+ "You were built by Developer Dev Malkar and Founder Deepakkumar Malkar at NiftyOracle in India. "
170
+ "Always answer the user's questions directly, politely, and accurately."
171
+ )
172
+
173
+ prompt = f"<|system|>\n{system_prompt}\n<|user|>\n{message}\n<|assistant|>\n"
174
  prompt_ids = sp.encode(prompt)
175
 
176
+ if len(prompt_ids) > 1024:
177
+ prompt_ids = prompt_ids[-1024:]
178
+
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
186
+
187
  with torch.no_grad():
188
+ for _ in range(MAX_NEW):
189
  with torch.autocast("cuda" if device.type == "cuda" else "cpu", dtype=torch.bfloat16):
190
  logits = model(inp)[0, -1, :].float()
191
 
192
+ # 1. Temperature (Increased to 0.7 to stop repetitive stuttering)
193
+ logits = logits / 0.7
194
 
195
+ # 2. Repetition Penalty (Applied to ALL generated tokens so it never repeats sentences)
196
+ penalty = 1.3
197
+ for tok_id in set(generated):
198
+ if logits[tok_id] > 0:
199
+ logits[tok_id] /= penalty
200
  else:
201
+ logits[tok_id] *= penalty
202
 
203
+ # 3. Top-K Filtering (Cuts off hallucinated garbage tokens)
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
 
221
  next_t = torch.multinomial(probs, 1).item()
222
  generated.append(next_t)
223
  inp = torch.cat([inp, torch.tensor([[next_t]], device=device)], dim=1)
224
+
225
+ # Check if it generated the stop sequence
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
+ # Strip tags to leave clean text
234
  for tag in ["<|user|>", "<|system|>", "<|assistant|>", "<|endoftext|>"]:
235
  response = response.split(tag)[0]
236