Rajan Sharma commited on
Commit
3a7831a
·
verified ·
1 Parent(s): d78ac98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -40,6 +40,11 @@ except Exception:
40
  from transformers import AutoTokenizer, AutoModelForCausalLM
41
  from huggingface_hub import login, HfApi
42
 
 
 
 
 
 
43
  # -------------------
44
  # Config
45
  # -------------------
@@ -184,17 +189,39 @@ def local_generate(model, tokenizer, input_ids, max_new_tokens=350):
184
  return tokenizer.decode(gen_only, skip_special_tokens=True).strip()
185
 
186
  # -------------------
187
- # Chat Function
188
  # -------------------
189
  def chat_fn(message, history, user_tz):
190
  try:
191
- if is_identity_query(message, history):
 
 
 
 
 
 
192
  return "I am ClarityOps, your strategic decision making AI partner."
 
 
193
  if USE_HOSTED_COHERE:
194
- return cohere_chat(message, history)
195
- model, tokenizer = load_local_model()
196
- inputs = build_inputs(tokenizer, message, history)
197
- return local_generate(model, tokenizer, inputs, max_new_tokens=350)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  except Exception as e:
199
  return f"Error: {e}"
200
 
@@ -214,19 +241,16 @@ custom_css = """
214
  --brand-text: #0f172a;
215
  --brand-text-light: #ffffff;
216
  }
217
-
218
  /* Page background */
219
  .gradio-container {
220
  background: var(--brand-bg);
221
  }
222
-
223
  /* Title */
224
  h1 {
225
  color: var(--brand-text);
226
  font-weight: 700;
227
  font-size: 28px !important;
228
  }
229
-
230
  /* Try to hide the default Chatbot label via CSS for multiple Gradio builds */
231
  .chatbot header,
232
  .chatbot .label,
@@ -236,7 +260,6 @@ h1 {
236
  .chatbot > .wrap > header {
237
  display: none !important;
238
  }
239
-
240
  /* Both bot and user bubbles teal with white text */
241
  .message.user, .message.bot {
242
  background: var(--brand-accent) !important;
@@ -244,7 +267,6 @@ h1 {
244
  border-radius: 12px !important;
245
  padding: 8px 12px !important;
246
  }
247
-
248
  /* Inputs a bit softer */
249
  textarea, input, .gr-input {
250
  border-radius: 12px !important;
@@ -308,9 +330,3 @@ if __name__ == "__main__":
308
  max_threads=8, # optional: avoid thread-starvation on tiny CPUs
309
  )
310
 
311
-
312
-
313
-
314
-
315
-
316
-
 
40
  from transformers import AutoTokenizer, AutoModelForCausalLM
41
  from huggingface_hub import login, HfApi
42
 
43
+ # -------------------
44
+ # NEW: Safety imports (from your snippet / safety.py)
45
+ # -------------------
46
+ from safety import safety_filter, refusal_reply
47
+
48
  # -------------------
49
  # Config
50
  # -------------------
 
189
  return tokenizer.decode(gen_only, skip_special_tokens=True).strip()
190
 
191
  # -------------------
192
+ # Chat Function (now with Safety layer)
193
  # -------------------
194
  def chat_fn(message, history, user_tz):
195
  try:
196
+ # ---- INPUT SAFETY (your snippet semantics) ----
197
+ safe_in, blocked_in, reason_in = safety_filter(message, mode="input")
198
+ if blocked_in:
199
+ return refusal_reply(reason_in)
200
+
201
+ # Identity short-circuit (use sanitized input)
202
+ if is_identity_query(safe_in, history):
203
  return "I am ClarityOps, your strategic decision making AI partner."
204
+
205
+ # ---- GENERATION using sanitized input ----
206
  if USE_HOSTED_COHERE:
207
+ out = cohere_chat(safe_in, history)
208
+ else:
209
+ model, tokenizer = load_local_model()
210
+ inputs = build_inputs(tokenizer, safe_in, history)
211
+ out = local_generate(model, tokenizer, inputs, max_new_tokens=350)
212
+
213
+ # Tidy echoes (from your snippet)
214
+ if isinstance(out, str):
215
+ for tag in ("Assistant:", "System:", "User:"):
216
+ if out.startswith(tag):
217
+ out = out[len(tag):].strip()
218
+
219
+ # ---- OUTPUT SAFETY ----
220
+ safe_out, blocked_out, reason_out = safety_filter(out, mode="output")
221
+ if blocked_out:
222
+ return refusal_reply(reason_out)
223
+
224
+ return safe_out
225
  except Exception as e:
226
  return f"Error: {e}"
227
 
 
241
  --brand-text: #0f172a;
242
  --brand-text-light: #ffffff;
243
  }
 
244
  /* Page background */
245
  .gradio-container {
246
  background: var(--brand-bg);
247
  }
 
248
  /* Title */
249
  h1 {
250
  color: var(--brand-text);
251
  font-weight: 700;
252
  font-size: 28px !important;
253
  }
 
254
  /* Try to hide the default Chatbot label via CSS for multiple Gradio builds */
255
  .chatbot header,
256
  .chatbot .label,
 
260
  .chatbot > .wrap > header {
261
  display: none !important;
262
  }
 
263
  /* Both bot and user bubbles teal with white text */
264
  .message.user, .message.bot {
265
  background: var(--brand-accent) !important;
 
267
  border-radius: 12px !important;
268
  padding: 8px 12px !important;
269
  }
 
270
  /* Inputs a bit softer */
271
  textarea, input, .gr-input {
272
  border-radius: 12px !important;
 
330
  max_threads=8, # optional: avoid thread-starvation on tiny CPUs
331
  )
332