Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,12 +15,12 @@ HF_HOME = str(HOME / ".cache" / "huggingface")
|
|
| 15 |
HF_HUB_CACHE = str(HOME / ".cache" / "huggingface" / "hub")
|
| 16 |
HF_TRANSFORMERS = str(HOME / ".cache" / "huggingface" / "transformers")
|
| 17 |
ST_HOME = str(HOME / ".cache" / "sentence-transformers")
|
| 18 |
-
GRADIO_TMP = str(HOME / "app" / "gradio") #
|
| 19 |
GRADIO_CACHE = GRADIO_TMP
|
| 20 |
|
| 21 |
os.environ.setdefault("HF_HOME", HF_HOME)
|
| 22 |
os.environ.setdefault("HF_HUB_CACHE", HF_HUB_CACHE)
|
| 23 |
-
os.environ.setdefault("TRANSFORMERS_CACHE", HF_TRANSFORMERS) #
|
| 24 |
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", ST_HOME)
|
| 25 |
os.environ.setdefault("GRADIO_TEMP_DIR", GRADIO_TMP)
|
| 26 |
os.environ.setdefault("GRADIO_CACHE_DIR", GRADIO_CACHE)
|
|
@@ -376,14 +376,21 @@ h1 { color: var(--brand-text); font-weight: 700; font-size: 28px !important; }
|
|
| 376 |
.message.user, .message.bot { background: var(--brand-accent) !important; color: var(--brand-text-light) !important; border-radius: 12px !important; padding: 8px 12px !important; }
|
| 377 |
textarea, input, .gr-input { border-radius: 12px !important; }
|
| 378 |
|
| 379 |
-
/* Centered handshake overlay */
|
|
|
|
|
|
|
| 380 |
#handshake-overlay {
|
| 381 |
position: absolute;
|
|
|
|
| 382 |
z-index: 50;
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
color: #fff;
|
| 388 |
padding: 18px 22px;
|
| 389 |
border-radius: 14px;
|
|
@@ -392,8 +399,12 @@ textarea, input, .gr-input { border-radius: 12px !important; }
|
|
| 392 |
text-align: center;
|
| 393 |
box-shadow: 0 10px 24px rgba(0,0,0,0.2);
|
| 394 |
}
|
|
|
|
| 395 |
#handshake-overlay.hidden { display: none; }
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
| 397 |
"""
|
| 398 |
|
| 399 |
# ---------- UI ----------
|
|
@@ -401,9 +412,14 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 401 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 402 |
|
| 403 |
with gr.Column(elem_id="chat-container"):
|
| 404 |
-
chat = gr.Chatbot(label="", show_label=False, height=
|
| 405 |
handshake = gr.HTML(
|
| 406 |
-
value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
)
|
| 408 |
|
| 409 |
with gr.Row():
|
|
@@ -426,29 +442,33 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 426 |
state_uploaded = gr.State(value=[])
|
| 427 |
state_awaiting = gr.State(value=False) # False -> Phase 1 next; True -> awaiting answers for Phase 2
|
| 428 |
|
| 429 |
-
def
|
| 430 |
paths = []
|
| 431 |
for f in (files or []):
|
| 432 |
paths.append(getattr(f, "name", None) or f)
|
| 433 |
-
|
|
|
|
|
|
|
| 434 |
|
| 435 |
-
uploads.change(fn=
|
|
|
|
|
|
|
| 436 |
|
| 437 |
def _on_send(user_msg, history, up_paths, awaiting):
|
| 438 |
-
|
| 439 |
try:
|
| 440 |
if not user_msg or not user_msg.strip():
|
| 441 |
-
return history, "", history, awaiting,
|
| 442 |
new_history, new_awaiting = clarityops_reply(
|
| 443 |
user_msg.strip(), history or [], None, up_paths or [], awaiting_answers=awaiting
|
| 444 |
)
|
| 445 |
-
return new_history, "", new_history, new_awaiting,
|
| 446 |
except Exception as e:
|
| 447 |
err = f"Error: {e}"
|
| 448 |
try: traceback.print_exc()
|
| 449 |
except Exception: pass
|
| 450 |
new_hist = (history or []) + [(user_msg or "", err)]
|
| 451 |
-
return new_hist, "", new_hist, awaiting,
|
| 452 |
|
| 453 |
send.click(_on_send, inputs=[msg, state_history, state_uploaded, state_awaiting],
|
| 454 |
outputs=[chat, msg, state_history, state_awaiting, handshake],
|
|
@@ -459,7 +479,13 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 459 |
concurrency_limit=2, queue=True)
|
| 460 |
|
| 461 |
def _on_clear():
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
clear.click(_on_clear, None, [chat, msg, state_history, state_awaiting, handshake])
|
| 465 |
|
|
@@ -468,3 +494,4 @@ if __name__ == "__main__":
|
|
| 468 |
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=8)
|
| 469 |
|
| 470 |
|
|
|
|
|
|
| 15 |
HF_HUB_CACHE = str(HOME / ".cache" / "huggingface" / "hub")
|
| 16 |
HF_TRANSFORMERS = str(HOME / ".cache" / "huggingface" / "transformers")
|
| 17 |
ST_HOME = str(HOME / ".cache" / "sentence-transformers")
|
| 18 |
+
GRADIO_TMP = str(HOME / "app" / "gradio") # or "/tmp/gradio"
|
| 19 |
GRADIO_CACHE = GRADIO_TMP
|
| 20 |
|
| 21 |
os.environ.setdefault("HF_HOME", HF_HOME)
|
| 22 |
os.environ.setdefault("HF_HUB_CACHE", HF_HUB_CACHE)
|
| 23 |
+
os.environ.setdefault("TRANSFORMERS_CACHE", HF_TRANSFORMERS) # warning is fine
|
| 24 |
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", ST_HOME)
|
| 25 |
os.environ.setdefault("GRADIO_TEMP_DIR", GRADIO_TMP)
|
| 26 |
os.environ.setdefault("GRADIO_CACHE_DIR", GRADIO_CACHE)
|
|
|
|
| 376 |
.message.user, .message.bot { background: var(--brand-accent) !important; color: var(--brand-text-light) !important; border-radius: 12px !important; padding: 8px 12px !important; }
|
| 377 |
textarea, input, .gr-input { border-radius: 12px !important; }
|
| 378 |
|
| 379 |
+
/* Centered handshake overlay INSIDE the chat area */
|
| 380 |
+
#chat-container { position: relative; } /* anchor for absolute overlay */
|
| 381 |
+
|
| 382 |
#handshake-overlay {
|
| 383 |
position: absolute;
|
| 384 |
+
inset: 8px; /* fill the chatbot panel with a small margin */
|
| 385 |
z-index: 50;
|
| 386 |
+
display: grid;
|
| 387 |
+
place-items: center;
|
| 388 |
+
pointer-events: none; /* do not block uploads/inputs below */
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
#handshake-overlay .panel {
|
| 392 |
+
pointer-events: auto; /* clickable if needed */
|
| 393 |
+
background: rgba(13, 148, 136, 0.96);
|
| 394 |
color: #fff;
|
| 395 |
padding: 18px 22px;
|
| 396 |
border-radius: 14px;
|
|
|
|
| 399 |
text-align: center;
|
| 400 |
box-shadow: 0 10px 24px rgba(0,0,0,0.2);
|
| 401 |
}
|
| 402 |
+
|
| 403 |
#handshake-overlay.hidden { display: none; }
|
| 404 |
+
|
| 405 |
+
@media (max-height: 700px) {
|
| 406 |
+
#handshake-overlay .panel { font-size: 14px; padding: 14px 16px; max-width: 90vw; }
|
| 407 |
+
}
|
| 408 |
"""
|
| 409 |
|
| 410 |
# ---------- UI ----------
|
|
|
|
| 412 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 413 |
|
| 414 |
with gr.Column(elem_id="chat-container"):
|
| 415 |
+
chat = gr.Chatbot(label="", show_label=False, height=720)
|
| 416 |
handshake = gr.HTML(
|
| 417 |
+
value=(
|
| 418 |
+
'<div id="handshake-overlay">'
|
| 419 |
+
'<div class="panel">ClarityOps loaded. Paste your scenario and attach files. '
|
| 420 |
+
'I’ll ask up to 5 clarifications, then produce the structured analysis</div>'
|
| 421 |
+
'</div>'
|
| 422 |
+
)
|
| 423 |
)
|
| 424 |
|
| 425 |
with gr.Row():
|
|
|
|
| 442 |
state_uploaded = gr.State(value=[])
|
| 443 |
state_awaiting = gr.State(value=False) # False -> Phase 1 next; True -> awaiting answers for Phase 2
|
| 444 |
|
| 445 |
+
def _store_uploads_and_hide(files, current):
|
| 446 |
paths = []
|
| 447 |
for f in (files or []):
|
| 448 |
paths.append(getattr(f, "name", None) or f)
|
| 449 |
+
# Also hide banner when the first action is file upload
|
| 450 |
+
overlay_hidden = gr.update(value='<div id="handshake-overlay" class="hidden"></div>')
|
| 451 |
+
return (current or []) + paths, overlay_hidden
|
| 452 |
|
| 453 |
+
uploads.change(fn=_store_uploads_and_hide,
|
| 454 |
+
inputs=[uploads, state_uploaded],
|
| 455 |
+
outputs=[state_uploaded, handshake])
|
| 456 |
|
| 457 |
def _on_send(user_msg, history, up_paths, awaiting):
|
| 458 |
+
hide_overlay = gr.update(value='<div id="handshake-overlay" class="hidden"></div>')
|
| 459 |
try:
|
| 460 |
if not user_msg or not user_msg.strip():
|
| 461 |
+
return history, "", history, awaiting, hide_overlay
|
| 462 |
new_history, new_awaiting = clarityops_reply(
|
| 463 |
user_msg.strip(), history or [], None, up_paths or [], awaiting_answers=awaiting
|
| 464 |
)
|
| 465 |
+
return new_history, "", new_history, new_awaiting, hide_overlay
|
| 466 |
except Exception as e:
|
| 467 |
err = f"Error: {e}"
|
| 468 |
try: traceback.print_exc()
|
| 469 |
except Exception: pass
|
| 470 |
new_hist = (history or []) + [(user_msg or "", err)]
|
| 471 |
+
return new_hist, "", new_hist, awaiting, hide_overlay
|
| 472 |
|
| 473 |
send.click(_on_send, inputs=[msg, state_history, state_uploaded, state_awaiting],
|
| 474 |
outputs=[chat, msg, state_history, state_awaiting, handshake],
|
|
|
|
| 479 |
concurrency_limit=2, queue=True)
|
| 480 |
|
| 481 |
def _on_clear():
|
| 482 |
+
# Reset everything, show handshake again
|
| 483 |
+
return [], "", [], False, (
|
| 484 |
+
'<div id="handshake-overlay"><div class="panel">'
|
| 485 |
+
'ClarityOps loaded. Paste your scenario and attach files. '
|
| 486 |
+
'I’ll ask up to 5 clarifications, then produce the structured analysis'
|
| 487 |
+
'</div></div>'
|
| 488 |
+
)
|
| 489 |
|
| 490 |
clear.click(_on_clear, None, [chat, msg, state_history, state_awaiting, handshake])
|
| 491 |
|
|
|
|
| 494 |
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=8)
|
| 495 |
|
| 496 |
|
| 497 |
+
|