Spaces:
Running on Zero
Running on Zero
Upload 34 files
Browse files- app.py +5 -5
- src/llm_client.py +27 -2
app.py
CHANGED
|
@@ -30,7 +30,7 @@ from src.anomaly_model import score_reading
|
|
| 30 |
from src.data_generation import generate_inventory_db, generate_orders_db
|
| 31 |
from src.intent_model import INTENT_DESCRIPTIONS, load_pipeline, predict as intent_predict
|
| 32 |
from src.inventory_db import query_inventory, query_orders
|
| 33 |
-
from src.llm_client import answer_query, test_connection
|
| 34 |
from src.retriever import KBRetriever
|
| 35 |
|
| 36 |
ROOT = os.path.dirname(os.path.abspath(__file__))
|
|
@@ -104,7 +104,7 @@ anomaly_eval = _load_json("anomaly_eval.json")
|
|
| 104 |
retrieval_eval = _load_json("retrieval_eval.json")
|
| 105 |
latency_eval = _load_json("latency_eval.json")
|
| 106 |
|
| 107 |
-
HF_TOKEN_SET = bool(
|
| 108 |
|
| 109 |
# --------------------------------------------------------------------------
|
| 110 |
# ZeroGPU compatibility shim
|
|
@@ -493,10 +493,10 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), css=CUSTOM_CSS, title="
|
|
| 493 |
)
|
| 494 |
if not HF_TOKEN_SET:
|
| 495 |
gr.Markdown(
|
| 496 |
-
"> ⚠️ **No
|
| 497 |
"**retrieval-only fallback mode** (still functional, just not LLM-generated "
|
| 498 |
-
"prose). Add
|
| 499 |
-
"to enable full LLM responses."
|
| 500 |
)
|
| 501 |
|
| 502 |
with gr.Tab("💬 AI Assistant"):
|
|
|
|
| 30 |
from src.data_generation import generate_inventory_db, generate_orders_db
|
| 31 |
from src.intent_model import INTENT_DESCRIPTIONS, load_pipeline, predict as intent_predict
|
| 32 |
from src.inventory_db import query_inventory, query_orders
|
| 33 |
+
from src.llm_client import answer_query, test_connection, _get_hf_token, TOKEN_ENV_VAR_CANDIDATES
|
| 34 |
from src.retriever import KBRetriever
|
| 35 |
|
| 36 |
ROOT = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 104 |
retrieval_eval = _load_json("retrieval_eval.json")
|
| 105 |
latency_eval = _load_json("latency_eval.json")
|
| 106 |
|
| 107 |
+
HF_TOKEN_SET = bool(_get_hf_token())
|
| 108 |
|
| 109 |
# --------------------------------------------------------------------------
|
| 110 |
# ZeroGPU compatibility shim
|
|
|
|
| 493 |
)
|
| 494 |
if not HF_TOKEN_SET:
|
| 495 |
gr.Markdown(
|
| 496 |
+
"> ⚠️ **No HF token secret detected.** The AI Assistant tab will run in "
|
| 497 |
"**retrieval-only fallback mode** (still functional, just not LLM-generated "
|
| 498 |
+
f"prose). Add a secret named `HF_TOKEN` (or one of: {', '.join(TOKEN_ENV_VAR_CANDIDATES[1:])}) "
|
| 499 |
+
"in *Space settings → Variables and secrets* to enable full LLM responses."
|
| 500 |
)
|
| 501 |
|
| 502 |
with gr.Tab("💬 AI Assistant"):
|
src/llm_client.py
CHANGED
|
@@ -47,6 +47,27 @@ if _env_model:
|
|
| 47 |
|
| 48 |
DEFAULT_MODEL_ID = MODEL_CANDIDATES[0]
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
SYSTEM_PROMPT = (
|
| 51 |
"You are the Smart Warehouse AI Assistant, a helpful operations copilot "
|
| 52 |
"for a large automated distribution center (conveyors, AS/RS, AGVs/AMRs, "
|
|
@@ -96,7 +117,7 @@ def answer_query(
|
|
| 96 |
sources = retriever.retrieve(query, k=k)
|
| 97 |
context_block = "\n\n".join(f"[{s.title}]\n{s.text}" for s in sources)
|
| 98 |
|
| 99 |
-
hf_token =
|
| 100 |
|
| 101 |
if not hf_token:
|
| 102 |
answer = _extractive_fallback(query, sources)
|
|
@@ -106,7 +127,11 @@ def answer_query(
|
|
| 106 |
sources=sources,
|
| 107 |
latency_s=time.time() - start,
|
| 108 |
model_id="extractive-fallback",
|
| 109 |
-
debug_errors=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
try:
|
|
|
|
| 47 |
|
| 48 |
DEFAULT_MODEL_ID = MODEL_CANDIDATES[0]
|
| 49 |
|
| 50 |
+
# The token is normally expected as `HF_TOKEN`, but we also accept a few
|
| 51 |
+
# common alternate secret names in case the Space was set up with a
|
| 52 |
+
# different name. First one found wins. Add your own name here if needed.
|
| 53 |
+
TOKEN_ENV_VAR_CANDIDATES = [
|
| 54 |
+
"HF_TOKEN",
|
| 55 |
+
"Smart_Warehouse",
|
| 56 |
+
"HUGGINGFACE_TOKEN",
|
| 57 |
+
"HUGGINGFACEHUB_API_TOKEN",
|
| 58 |
+
"HF_API_TOKEN",
|
| 59 |
+
"HUGGING_FACE_HUB_TOKEN",
|
| 60 |
+
"HF_ACCESS_TOKEN",
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _get_hf_token() -> Optional[str]:
|
| 65 |
+
for var in TOKEN_ENV_VAR_CANDIDATES:
|
| 66 |
+
val = os.environ.get(var)
|
| 67 |
+
if val:
|
| 68 |
+
return val
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
SYSTEM_PROMPT = (
|
| 72 |
"You are the Smart Warehouse AI Assistant, a helpful operations copilot "
|
| 73 |
"for a large automated distribution center (conveyors, AS/RS, AGVs/AMRs, "
|
|
|
|
| 117 |
sources = retriever.retrieve(query, k=k)
|
| 118 |
context_block = "\n\n".join(f"[{s.title}]\n{s.text}" for s in sources)
|
| 119 |
|
| 120 |
+
hf_token = _get_hf_token()
|
| 121 |
|
| 122 |
if not hf_token:
|
| 123 |
answer = _extractive_fallback(query, sources)
|
|
|
|
| 127 |
sources=sources,
|
| 128 |
latency_s=time.time() - start,
|
| 129 |
model_id="extractive-fallback",
|
| 130 |
+
debug_errors=[
|
| 131 |
+
"No HF token secret found. Checked env vars: "
|
| 132 |
+
+ ", ".join(TOKEN_ENV_VAR_CANDIDATES)
|
| 133 |
+
+ ". Set one of these as a Space secret (Settings -> Variables and secrets)."
|
| 134 |
+
],
|
| 135 |
)
|
| 136 |
|
| 137 |
try:
|