Spaces:
Running
Running
Graceful startup — show setup guide if credentials missing
Browse files- openmark/ui/app.py +31 -8
openmark/ui/app.py
CHANGED
|
@@ -11,22 +11,41 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
|
| 11 |
sys.stdout.reconfigure(encoding="utf-8")
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
-
from openmark.agent.graph import build_agent, ask
|
| 15 |
-
from openmark.embeddings.factory import get_embedder
|
| 16 |
-
from openmark.stores import chroma as chroma_store
|
| 17 |
-
from openmark.stores import neo4j_store
|
| 18 |
from openmark import config
|
| 19 |
|
| 20 |
-
# Load once at startup
|
| 21 |
print("Loading OpenMark...")
|
| 22 |
-
_embedder =
|
| 23 |
-
_agent =
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
# ── Chat tab ──────────────────────────────────────────────────
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def chat_fn(message: str, history: list, thread_id: str):
|
|
|
|
|
|
|
|
|
|
| 30 |
if not message.strip():
|
| 31 |
return history, ""
|
| 32 |
response = ask(_agent, message, thread_id=thread_id or "default")
|
|
@@ -37,6 +56,8 @@ def chat_fn(message: str, history: list, thread_id: str):
|
|
| 37 |
# ── Search tab ────────────────────────────────────────────────
|
| 38 |
|
| 39 |
def search_fn(query: str, category: str, min_score: float, n_results: int):
|
|
|
|
|
|
|
| 40 |
if not query.strip():
|
| 41 |
return "Enter a search query."
|
| 42 |
|
|
@@ -65,6 +86,8 @@ def search_fn(query: str, category: str, min_score: float, n_results: int):
|
|
| 65 |
# ── Stats tab ─────────────────────────────────────────────────
|
| 66 |
|
| 67 |
def stats_fn():
|
|
|
|
|
|
|
| 68 |
chroma = chroma_store.get_stats()
|
| 69 |
neo4j = neo4j_store.get_stats()
|
| 70 |
|
|
|
|
| 11 |
sys.stdout.reconfigure(encoding="utf-8")
|
| 12 |
|
| 13 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
from openmark import config
|
| 15 |
|
| 16 |
+
# Load once at startup — fail gracefully if credentials are not configured
|
| 17 |
print("Loading OpenMark...")
|
| 18 |
+
_embedder = None
|
| 19 |
+
_agent = None
|
| 20 |
+
_setup_error = None
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
from openmark.embeddings.factory import get_embedder
|
| 24 |
+
from openmark.agent.graph import build_agent, ask
|
| 25 |
+
from openmark.stores import chroma as chroma_store
|
| 26 |
+
from openmark.stores import neo4j_store
|
| 27 |
+
_embedder = get_embedder()
|
| 28 |
+
_agent = build_agent()
|
| 29 |
+
print("OpenMark ready.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
_setup_error = str(e)
|
| 32 |
+
print(f"OpenMark setup incomplete: {e}")
|
| 33 |
|
| 34 |
|
| 35 |
# ── Chat tab ──────────────────────────────────────────────────
|
| 36 |
|
| 37 |
+
_NOT_READY = (
|
| 38 |
+
"## Setup required\n\n"
|
| 39 |
+
"This Space is a **demo shell** — it requires your own credentials to run.\n\n"
|
| 40 |
+
"See the [GitHub repo](https://github.com/OthmanAdi/OpenMark) for full setup instructions.\n\n"
|
| 41 |
+
f"```\n{_setup_error}\n```" if _setup_error else ""
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
def chat_fn(message: str, history: list, thread_id: str):
|
| 46 |
+
if _agent is None:
|
| 47 |
+
history.append((message, _NOT_READY))
|
| 48 |
+
return history, ""
|
| 49 |
if not message.strip():
|
| 50 |
return history, ""
|
| 51 |
response = ask(_agent, message, thread_id=thread_id or "default")
|
|
|
|
| 56 |
# ── Search tab ────────────────────────────────────────────────
|
| 57 |
|
| 58 |
def search_fn(query: str, category: str, min_score: float, n_results: int):
|
| 59 |
+
if _embedder is None:
|
| 60 |
+
return _NOT_READY
|
| 61 |
if not query.strip():
|
| 62 |
return "Enter a search query."
|
| 63 |
|
|
|
|
| 86 |
# ── Stats tab ─────────────────────────────────────────────────
|
| 87 |
|
| 88 |
def stats_fn():
|
| 89 |
+
if _embedder is None:
|
| 90 |
+
return _NOT_READY
|
| 91 |
chroma = chroma_store.get_stats()
|
| 92 |
neo4j = neo4j_store.get_stats()
|
| 93 |
|