Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,11 +65,16 @@ def pick_dtype_and_map():
|
|
| 65 |
|
| 66 |
def is_identity_query(message, history):
|
| 67 |
patterns = [
|
| 68 |
-
r"\bwho\s+are\s+you\b",
|
| 69 |
-
r"\bwhat\s+
|
| 70 |
-
r"\
|
| 71 |
-
r"\
|
| 72 |
-
r"\
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
]
|
| 74 |
def match(t):
|
| 75 |
return any(re.search(p, (t or "").strip().lower()) for p in patterns)
|
|
@@ -82,8 +87,7 @@ def is_identity_query(message, history):
|
|
| 82 |
return False
|
| 83 |
|
| 84 |
def _iter_user_assistant(history):
|
| 85 |
-
"""
|
| 86 |
-
Yield (user, assistant) pairs from a Gradio history list.
|
| 87 |
Safely handles items that are lists/tuples with >2 elements.
|
| 88 |
"""
|
| 89 |
for item in (history or []):
|
|
@@ -91,7 +95,7 @@ def _iter_user_assistant(history):
|
|
| 91 |
u = item[0] if len(item) > 0 else ""
|
| 92 |
a = item[1] if len(item) > 1 else ""
|
| 93 |
yield u, a
|
| 94 |
-
|
| 95 |
|
| 96 |
def _history_to_prompt(message, history):
|
| 97 |
"""Build a simple text prompt for the stable cohere.chat API."""
|
|
@@ -189,11 +193,11 @@ def local_generate(model, tokenizer, input_ids, max_new_tokens=350):
|
|
| 189 |
return tokenizer.decode(gen_only, skip_special_tokens=True).strip()
|
| 190 |
|
| 191 |
# -------------------
|
| 192 |
-
# Chat Function (
|
| 193 |
# -------------------
|
| 194 |
def chat_fn(message, history, user_tz):
|
| 195 |
try:
|
| 196 |
-
# ---- INPUT SAFETY
|
| 197 |
safe_in, blocked_in, reason_in = safety_filter(message, mode="input")
|
| 198 |
if blocked_in:
|
| 199 |
return refusal_reply(reason_in)
|
|
@@ -210,7 +214,7 @@ def chat_fn(message, history, user_tz):
|
|
| 210 |
inputs = build_inputs(tokenizer, safe_in, history)
|
| 211 |
out = local_generate(model, tokenizer, inputs, max_new_tokens=350)
|
| 212 |
|
| 213 |
-
# Tidy echoes
|
| 214 |
if isinstance(out, str):
|
| 215 |
for tag in ("Assistant:", "System:", "User:"):
|
| 216 |
if out.startswith(tag):
|
|
@@ -220,7 +224,6 @@ def chat_fn(message, history, user_tz):
|
|
| 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}"
|
|
@@ -241,35 +244,38 @@ custom_css = """
|
|
| 241 |
--brand-text: #0f172a;
|
| 242 |
--brand-text-light: #ffffff;
|
| 243 |
}
|
|
|
|
| 244 |
/* Page background */
|
| 245 |
-
.gradio-container {
|
| 246 |
-
|
| 247 |
-
}
|
| 248 |
/* Title */
|
| 249 |
h1 {
|
| 250 |
color: var(--brand-text);
|
| 251 |
font-weight: 700;
|
| 252 |
font-size: 28px !important;
|
| 253 |
}
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
.chatbot .label,
|
| 257 |
-
.chatbot .label-wrap,
|
| 258 |
-
.chatbot .top,
|
| 259 |
-
.chatbot .header,
|
| 260 |
-
.chatbot > .wrap > header {
|
| 261 |
display: none !important;
|
| 262 |
}
|
| 263 |
-
|
|
|
|
| 264 |
.message.user, .message.bot {
|
| 265 |
background: var(--brand-accent) !important;
|
| 266 |
color: var(--brand-text-light) !important;
|
| 267 |
border-radius: 12px !important;
|
| 268 |
padding: 8px 12px !important;
|
| 269 |
}
|
|
|
|
| 270 |
/* Inputs a bit softer */
|
| 271 |
-
textarea, input, .gr-input {
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
}
|
| 274 |
"""
|
| 275 |
|
|
@@ -279,8 +285,12 @@ textarea, input, .gr-input {
|
|
| 279 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 280 |
# Hidden box to carry timezone (still useful for future features)
|
| 281 |
tz_box = gr.Textbox(visible=False)
|
| 282 |
-
demo.load(
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
# Extra JS hard-removal of the Chatbot label to cover all DOM variants
|
| 286 |
hide_label_sink = gr.HTML(visible=False)
|
|
@@ -289,35 +299,45 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 289 |
inputs=None,
|
| 290 |
outputs=hide_label_sink,
|
| 291 |
js="""
|
| 292 |
-
() => {
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
}
|
| 304 |
-
"""
|
| 305 |
)
|
| 306 |
|
| 307 |
-
#
|
| 308 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 309 |
|
|
|
|
| 310 |
gr.ChatInterface(
|
| 311 |
fn=chat_fn,
|
| 312 |
type="messages",
|
| 313 |
additional_inputs=[tz_box],
|
| 314 |
-
chatbot=gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
examples=[
|
| 316 |
-
["What are the symptoms of hypertension?"
|
| 317 |
-
["What are common drug interactions with aspirin?"
|
| 318 |
-
["What are the warning signs of diabetes?"
|
| 319 |
],
|
| 320 |
-
cache_examples=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
)
|
| 322 |
|
| 323 |
if __name__ == "__main__":
|
|
@@ -326,7 +346,8 @@ if __name__ == "__main__":
|
|
| 326 |
demo.launch(
|
| 327 |
server_name="0.0.0.0",
|
| 328 |
server_port=port,
|
| 329 |
-
show_api=False,
|
| 330 |
-
max_threads=8,
|
| 331 |
)
|
| 332 |
|
|
|
|
|
|
| 65 |
|
| 66 |
def is_identity_query(message, history):
|
| 67 |
patterns = [
|
| 68 |
+
r"\bwho\s+are\s+you\b",
|
| 69 |
+
r"\bwhat\s+are\s+you\b",
|
| 70 |
+
r"\bwhat\s+is\s+your\s+name\b",
|
| 71 |
+
r"\bwho\s+is\s+this\b",
|
| 72 |
+
r"\bidentify\s+yourself\b",
|
| 73 |
+
r"\btell\s+me\s+about\s+yourself\b",
|
| 74 |
+
r"\bdescribe\s+yourself\b",
|
| 75 |
+
r"\band\s+you\s*\?\b",
|
| 76 |
+
r"\byour\s+name\b",
|
| 77 |
+
r"\bwho\s+am\s+i\s+chatting\s+with\b"
|
| 78 |
]
|
| 79 |
def match(t):
|
| 80 |
return any(re.search(p, (t or "").strip().lower()) for p in patterns)
|
|
|
|
| 87 |
return False
|
| 88 |
|
| 89 |
def _iter_user_assistant(history):
|
| 90 |
+
"""Yield (user, assistant) pairs from a Gradio history list.
|
|
|
|
| 91 |
Safely handles items that are lists/tuples with >2 elements.
|
| 92 |
"""
|
| 93 |
for item in (history or []):
|
|
|
|
| 95 |
u = item[0] if len(item) > 0 else ""
|
| 96 |
a = item[1] if len(item) > 1 else ""
|
| 97 |
yield u, a
|
| 98 |
+
# If dicts ever appear, extend handling here.
|
| 99 |
|
| 100 |
def _history_to_prompt(message, history):
|
| 101 |
"""Build a simple text prompt for the stable cohere.chat API."""
|
|
|
|
| 193 |
return tokenizer.decode(gen_only, skip_special_tokens=True).strip()
|
| 194 |
|
| 195 |
# -------------------
|
| 196 |
+
# Chat Function (with Safety layer)
|
| 197 |
# -------------------
|
| 198 |
def chat_fn(message, history, user_tz):
|
| 199 |
try:
|
| 200 |
+
# ---- INPUT SAFETY ----
|
| 201 |
safe_in, blocked_in, reason_in = safety_filter(message, mode="input")
|
| 202 |
if blocked_in:
|
| 203 |
return refusal_reply(reason_in)
|
|
|
|
| 214 |
inputs = build_inputs(tokenizer, safe_in, history)
|
| 215 |
out = local_generate(model, tokenizer, inputs, max_new_tokens=350)
|
| 216 |
|
| 217 |
+
# Tidy echoes
|
| 218 |
if isinstance(out, str):
|
| 219 |
for tag in ("Assistant:", "System:", "User:"):
|
| 220 |
if out.startswith(tag):
|
|
|
|
| 224 |
safe_out, blocked_out, reason_out = safety_filter(out, mode="output")
|
| 225 |
if blocked_out:
|
| 226 |
return refusal_reply(reason_out)
|
|
|
|
| 227 |
return safe_out
|
| 228 |
except Exception as e:
|
| 229 |
return f"Error: {e}"
|
|
|
|
| 244 |
--brand-text: #0f172a;
|
| 245 |
--brand-text-light: #ffffff;
|
| 246 |
}
|
| 247 |
+
|
| 248 |
/* Page background */
|
| 249 |
+
.gradio-container { background: var(--brand-bg); }
|
| 250 |
+
|
|
|
|
| 251 |
/* Title */
|
| 252 |
h1 {
|
| 253 |
color: var(--brand-text);
|
| 254 |
font-weight: 700;
|
| 255 |
font-size: 28px !important;
|
| 256 |
}
|
| 257 |
+
|
| 258 |
+
/* Hide default Chatbot label (cover most Gradio builds) */
|
| 259 |
+
.chatbot header, .chatbot .label, .chatbot .label-wrap, .chatbot .top, .chatbot .header, .chatbot > .wrap > header {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
display: none !important;
|
| 261 |
}
|
| 262 |
+
|
| 263 |
+
/* Bubble styling */
|
| 264 |
.message.user, .message.bot {
|
| 265 |
background: var(--brand-accent) !important;
|
| 266 |
color: var(--brand-text-light) !important;
|
| 267 |
border-radius: 12px !important;
|
| 268 |
padding: 8px 12px !important;
|
| 269 |
}
|
| 270 |
+
|
| 271 |
/* Inputs a bit softer */
|
| 272 |
+
textarea, input, .gr-input { border-radius: 12px !important; }
|
| 273 |
+
|
| 274 |
+
/* Center examples */
|
| 275 |
+
.examples, .examples .grid {
|
| 276 |
+
display: flex !important;
|
| 277 |
+
justify-content: center !important;
|
| 278 |
+
text-align: center !important;
|
| 279 |
}
|
| 280 |
"""
|
| 281 |
|
|
|
|
| 285 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 286 |
# Hidden box to carry timezone (still useful for future features)
|
| 287 |
tz_box = gr.Textbox(visible=False)
|
| 288 |
+
demo.load(
|
| 289 |
+
lambda tz: tz,
|
| 290 |
+
inputs=[tz_box],
|
| 291 |
+
outputs=[tz_box],
|
| 292 |
+
js="() => Intl.DateTimeFormat().resolvedOptions().timeZone",
|
| 293 |
+
)
|
| 294 |
|
| 295 |
# Extra JS hard-removal of the Chatbot label to cover all DOM variants
|
| 296 |
hide_label_sink = gr.HTML(visible=False)
|
|
|
|
| 299 |
inputs=None,
|
| 300 |
outputs=hide_label_sink,
|
| 301 |
js="""
|
| 302 |
+
() => {
|
| 303 |
+
const sel = [
|
| 304 |
+
'.chatbot header',
|
| 305 |
+
'.chatbot .label',
|
| 306 |
+
'.chatbot .label-wrap',
|
| 307 |
+
'.chatbot .top',
|
| 308 |
+
'.chatbot .header',
|
| 309 |
+
'.chatbot > .wrap > header'
|
| 310 |
+
];
|
| 311 |
+
sel.forEach(s => document.querySelectorAll(s).forEach(el => el.style.display = 'none'));
|
| 312 |
+
return "";
|
| 313 |
+
}
|
| 314 |
+
""",
|
| 315 |
)
|
| 316 |
|
| 317 |
+
# Title
|
| 318 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 319 |
|
| 320 |
+
# Chat interface (larger chat, no Undo; examples centered & single-column)
|
| 321 |
gr.ChatInterface(
|
| 322 |
fn=chat_fn,
|
| 323 |
type="messages",
|
| 324 |
additional_inputs=[tz_box],
|
| 325 |
+
chatbot=gr.Chatbot(
|
| 326 |
+
label="",
|
| 327 |
+
show_label=False,
|
| 328 |
+
type="messages",
|
| 329 |
+
height=700, # larger chat window
|
| 330 |
+
),
|
| 331 |
examples=[
|
| 332 |
+
["What are the symptoms of hypertension?"],
|
| 333 |
+
["What are common drug interactions with aspirin?"],
|
| 334 |
+
["What are the warning signs of diabetes?"],
|
| 335 |
],
|
| 336 |
+
cache_examples=False,
|
| 337 |
+
submit_btn="Submit",
|
| 338 |
+
retry_btn="Retry",
|
| 339 |
+
clear_btn="Clear",
|
| 340 |
+
undo_btn=None, # removed Undo button
|
| 341 |
)
|
| 342 |
|
| 343 |
if __name__ == "__main__":
|
|
|
|
| 346 |
demo.launch(
|
| 347 |
server_name="0.0.0.0",
|
| 348 |
server_port=port,
|
| 349 |
+
show_api=False, # optional: less overhead
|
| 350 |
+
max_threads=8, # optional: avoid thread-starvation on tiny CPUs
|
| 351 |
)
|
| 352 |
|
| 353 |
+
|