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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -25
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
- # 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"
@@ -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
- # 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
@@ -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
- # 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
 
 
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