Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os, re, json
|
| 2 |
from functools import lru_cache
|
| 3 |
|
| 4 |
import gradio as gr
|
|
@@ -302,7 +302,14 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 302 |
|
| 303 |
return history + [(user_msg, safe_out)]
|
| 304 |
except Exception as e:
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
# ---------- Theme & CSS ----------
|
| 308 |
theme = gr.themes.Soft(primary_hue="teal", neutral_hue="slate", radius_size=gr.themes.sizes.radius_lg)
|
|
@@ -340,6 +347,18 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 340 |
js="() => Intl.DateTimeFormat().resolvedOptions().timeZone",
|
| 341 |
)
|
| 342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 344 |
|
| 345 |
# Main chat (tuple-format history)
|
|
@@ -375,12 +394,21 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 375 |
|
| 376 |
uploads.change(fn=_store_uploads, inputs=[uploads, state_uploaded], outputs=state_uploaded)
|
| 377 |
|
| 378 |
-
# Send / Enter handlers (
|
| 379 |
def _on_send(user_msg, history, tz, up_paths):
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
send.click(
|
| 386 |
fn=_on_send,
|
|
@@ -402,6 +430,8 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 402 |
|
| 403 |
if __name__ == "__main__":
|
| 404 |
port = int(os.environ.get("PORT", "7860"))
|
|
|
|
| 405 |
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=8)
|
| 406 |
|
| 407 |
|
|
|
|
|
|
| 1 |
+
import os, re, json, threading, traceback
|
| 2 |
from functools import lru_cache
|
| 3 |
|
| 4 |
import gradio as gr
|
|
|
|
| 302 |
|
| 303 |
return history + [(user_msg, safe_out)]
|
| 304 |
except Exception as e:
|
| 305 |
+
# Surface the error in-chat so the websocket doesn’t die silently
|
| 306 |
+
err = f"Error: {e}"
|
| 307 |
+
try:
|
| 308 |
+
# Optionally log stack for ops
|
| 309 |
+
traceback.print_exc()
|
| 310 |
+
except Exception:
|
| 311 |
+
pass
|
| 312 |
+
return history + [(user_msg, err)]
|
| 313 |
|
| 314 |
# ---------- Theme & CSS ----------
|
| 315 |
theme = gr.themes.Soft(primary_hue="teal", neutral_hue="slate", radius_size=gr.themes.sizes.radius_lg)
|
|
|
|
| 347 |
js="() => Intl.DateTimeFormat().resolvedOptions().timeZone",
|
| 348 |
)
|
| 349 |
|
| 350 |
+
# --- Background warmup so first message doesn't time out ---
|
| 351 |
+
def _warmup():
|
| 352 |
+
def _bg():
|
| 353 |
+
try:
|
| 354 |
+
# Preload local fallback model quietly (download may be large)
|
| 355 |
+
load_local_model()
|
| 356 |
+
except Exception:
|
| 357 |
+
pass
|
| 358 |
+
threading.Thread(target=_bg, daemon=True).start()
|
| 359 |
+
return ""
|
| 360 |
+
demo.load(_warmup, None, None) # fire-and-forget
|
| 361 |
+
|
| 362 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 363 |
|
| 364 |
# Main chat (tuple-format history)
|
|
|
|
| 394 |
|
| 395 |
uploads.change(fn=_store_uploads, inputs=[uploads, state_uploaded], outputs=state_uploaded)
|
| 396 |
|
| 397 |
+
# Send / Enter handlers (defensive wrapper)
|
| 398 |
def _on_send(user_msg, history, tz, up_paths):
|
| 399 |
+
try:
|
| 400 |
+
if not user_msg or not user_msg.strip():
|
| 401 |
+
return history, "", history
|
| 402 |
+
new_history = clarityops_reply(user_msg.strip(), history or [], tz, up_paths or [])
|
| 403 |
+
return new_history, "", new_history
|
| 404 |
+
except Exception as e:
|
| 405 |
+
# Return the error in the chat so UI doesn't show a websocket failure
|
| 406 |
+
err = f"Error: {e}"
|
| 407 |
+
try:
|
| 408 |
+
traceback.print_exc()
|
| 409 |
+
except Exception:
|
| 410 |
+
pass
|
| 411 |
+
return (history or []) + [(user_msg or "", err)], "", (history or []) + [(user_msg or "", err)]
|
| 412 |
|
| 413 |
send.click(
|
| 414 |
fn=_on_send,
|
|
|
|
| 430 |
|
| 431 |
if __name__ == "__main__":
|
| 432 |
port = int(os.environ.get("PORT", "7860"))
|
| 433 |
+
# max_threads gives the worker pool; leave as-is unless you need more parallelism
|
| 434 |
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=8)
|
| 435 |
|
| 436 |
|
| 437 |
+
|