File size: 12,429 Bytes
4e4b89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# storefront_tabs_memory_app.py
import os, json
import gradio as gr
from transformers import pipeline

# ---------------- Model ----------------
MODEL_NAME = os.getenv("HF_MODEL_GENERATION", "distilgpt2")
_pipe = None
def _get_pipe():
    global _pipe
    if _pipe is None:
        _pipe = pipeline("text-generation", model=MODEL_NAME)
    return _pipe

def model_generate(prompt, max_new_tokens=128, temperature=0.8, top_p=0.95):
    out = _get_pipe()(
        prompt,
        max_new_tokens=int(max_new_tokens),
        do_sample=True,
        temperature=float(temperature),
        top_p=float(top_p),
        pad_token_id=50256,
    )
    return out[0]["generated_text"]


# ---------------- Storefront knowledge (helper module preferred) ----------------
STORE_DATA, USE_HELPERS = None, False
try:
    # Optional helper module under agenticcore/
    from agenticcore.storefront_rules import (
        load_storefront, answer_faq, get_parking_rules, get_venue_rules, search_products
    )
    STORE_DATA = load_storefront()
    USE_HELPERS = True
except Exception:
    # Fallback: try JSON next to this file or under agenticcore/
    CANDIDATES = [
        os.path.join(os.path.dirname(__file__), "storefront_data.json"),
        os.path.join(os.path.dirname(__file__), "agenticcore", "storefront_data.json"),
    ]
    for p in CANDIDATES:
        if os.path.exists(p):
            with open(p, "r", encoding="utf-8") as f:
                STORE_DATA = json.load(f)
            break

# Defaults if JSON/module missing
DEFAULT_PRODUCTS = [
    {"SKU": "CG-SET", "Name": "Cap & Gown Set", "Price": 59.00, "Notes": "Tassel included; ship until 10 days before event"},
    {"SKU": "PK-1",   "Name": "Parking Pass",   "Price": 10.00, "Notes": "Multiple passes allowed per student"},
]
DEFAULT_PARKING = ["No double parking.", "Vehicles parked in handicap will be towed."]
DEFAULT_VENUE   = ["Formal attire recommended (not required).", "No muscle shirts.", "No sagging pants."]

# Normalize JSON to tables for the UI
if STORE_DATA:
    try:
        DEFAULT_PRODUCTS = [{
            "SKU": p.get("sku",""),
            "Name": p.get("name",""),
            "Price": p.get("price_usd",""),
            "Notes": (p.get("description") or "")[:120],
        } for p in STORE_DATA.get("products", [])]
        DEFAULT_PARKING = STORE_DATA.get("policies", {}).get("parking_rules", DEFAULT_PARKING) or DEFAULT_PARKING
        DEFAULT_VENUE   = STORE_DATA.get("policies", {}).get("venue_rules", DEFAULT_VENUE)     or DEFAULT_VENUE
    except Exception:
        pass


# ---------------- Memory seeding ----------------
def seed_storefront_facts() -> str:
    """Small system-like primer injected ahead of chat history to bias the LLM toward storefront truth."""
    lines = ["You are answering questions about a graduation storefront.",
             "Products:"]
    for p in DEFAULT_PRODUCTS:
        price = p.get("Price")
        if isinstance(price, (int, float)):
            price = f"${price:.2f}"
        lines.append(f"- {p.get('Name','Item')} ({p.get('SKU','')}) — {price}: {p.get('Notes','')}")
    lines.append("Venue rules:")
    for r in DEFAULT_VENUE:
        lines.append(f"- {r}")
    lines.append("Parking rules:")
    for r in DEFAULT_PARKING:
        lines.append(f"- {r}")
    lines.append("Answer concisely using these facts. If unsure, say what’s known from the list above.")
    return "\n".join(lines)

SEED_TEXT = seed_storefront_facts()

def build_prompt_from_history(history, user_text, k=4):
    """history = [[user, bot], ...] — build a short rolling prompt + seed facts."""
    lines = [SEED_TEXT, "", "Conversation so far:"]
    for u, b in (history or [])[-k:]:
        if u: lines.append(f"User: {u}")
        if b: lines.append(f"Assistant: {b}")
    lines.append(f"User: {user_text}")
    lines.append("Assistant:")
    return "\n".join(lines)


# ---------------- Storefront Q&A router (storefront first, then LLM) ----------------
def storefront_qna(text: str) -> str | None:
    t = (text or "").lower().strip()
    if not t:
        return None

    # Single-word catches to avoid LLM drift
    if t in {"parking"}:
        return "Parking rules:\n- " + "\n- ".join(DEFAULT_PARKING)
    if t in {"venue", "attire", "dress", "dress code"}:
        return "Venue rules:\n- " + "\n- ".join(DEFAULT_VENUE)
    if t in {"passes", "parking pass", "parking passes"}:
        return "Yes, multiple parking passes are allowed per student."

    # Prefer helper functions if available
    if USE_HELPERS and STORE_DATA:
        a = answer_faq(STORE_DATA, t)
        if a:
            return a
        if "parking" in t and "rule" in t:
            r = get_parking_rules(STORE_DATA)
            if r:
                return "Parking rules:\n- " + "\n- ".join(r)
        if ("venue" in t and "rule" in t) or "attire" in t or "dress code" in t:
            r = get_venue_rules(STORE_DATA)
            if r:
                return "Venue rules:\n- " + "\n- ".join(r)
        # Specific timing phrasing to avoid hallucinated dates
        if "parking" in t and ("hours" in t or "time" in t or "open" in t):
            return "Parking lots open 2 hours before the ceremony."
        hits = search_products(STORE_DATA, t)
        if hits:
            return "\n".join(
                f"{p.get('name','Item')} — ${p.get('price_usd',0):.2f}: {p.get('description','')}"
                for p in hits
            )
        return None

    # Fallback rules (no helper module)
    if "parking" in t and ("more than one" in t or "multiple" in t or "extra" in t):
        return "Yes, multiple parking passes are allowed per student."
    if "parking" in t and "rule" in t:
        return "Parking rules:\n- " + "\n- ".join(DEFAULT_PARKING)
    if "parking" in t and ("hours" in t or "time" in t or "open" in t):
        return "Parking lots open 2 hours before the ceremony."
    if "attire" in t or "dress code" in t or ("venue" in t and "rule" in t):
        return "Venue rules:\n- " + "\n- ".join(DEFAULT_VENUE)
    if "cap" in t or "gown" in t:
        return "\n".join(
            f"{p['Name']} — ${p['Price']:.2f}: {p['Notes']}"
            for p in DEFAULT_PRODUCTS
        )
    return None


def chat_pipeline(history, message, max_new_tokens=128, temperature=0.8, top_p=0.95):
    # 1) Try storefront knowledge first
    sf = storefront_qna(message)
    if sf:
        return sf
    # 2) Memory-aware model prompt
    prompt = build_prompt_from_history(history, message, k=4)
    return model_generate(prompt, max_new_tokens, temperature, top_p)


# ---------------- Gradio UI (Tabs + Accordion) ----------------
CSS = """
:root { --bg:#0b0d12; --panel:#0f172a; --border:#1f2940; --text:#e5e7eb; --muted:#9ca3af; }
.gradio-container { background: var(--bg) !important; color: var(--text) !important; }
.panel { border:1px solid var(--border); border-radius:16px; background:var(--panel); }
.small { font-size:12px; color: var(--muted); }
"""

with gr.Blocks(title="Storefront Chat", css=CSS) as demo:
    gr.Markdown("## Storefront Chat")

    # Keep a single source of truth for chat history (pairs of [user, bot])
    history_state = gr.State([])

    with gr.Tabs():
        # --- TAB 1: Chat (sliders tucked into an accordion) ---
        with gr.TabItem("Chat"):
            with gr.Group(elem_classes=["panel"]):
                chat = gr.Chatbot(height=360, bubble_full_width=False, label="Chat")

                with gr.Row():
                    msg = gr.Textbox(placeholder="Ask about parking rules, attire, cap & gown, pickup times…", scale=5)
                    send = gr.Button("Send", scale=1)

                # Quick chips
                with gr.Row():
                    chip1 = gr.Button("Parking rules", variant="secondary")
                    chip2 = gr.Button("Multiple passes", variant="secondary")
                    chip3 = gr.Button("Attire", variant="secondary")
                    chip4 = gr.Button("When do lots open?", variant="secondary")

                # Advanced options hidden
                with gr.Accordion("Advanced chat options", open=False):
                    max_new = gr.Slider(32, 512, 128, 1, label="Max new tokens")
                    temp    = gr.Slider(0.1, 1.5, 0.8, 0.05, label="Temperature")
                    topp    = gr.Slider(0.1, 1.0, 0.95, 0.05, label="Top-p")

                # Small utilities
                with gr.Row():
                    health_btn = gr.Button("Health", variant="secondary")
                    caps_btn   = gr.Button("Capabilities", variant="secondary")
                    status_md  = gr.Markdown("Status: not checked", elem_classes=["small"])

        # --- TAB 2: Products ---
        with gr.TabItem("Products"):
            gr.Markdown("### Available Items")
            cols = list(DEFAULT_PRODUCTS[0].keys()) if DEFAULT_PRODUCTS else ["SKU","Name","Price","Notes"]
            data = [[p.get(c,"") for c in cols] for p in DEFAULT_PRODUCTS]
            _products_tbl = gr.Dataframe(headers=cols, value=data, interactive=False, wrap=True, label="Products")

        # --- TAB 3: Rules ---
        with gr.TabItem("Rules"):
            gr.Markdown("### Venue rules")
            gr.Markdown("- " + "\n- ".join(DEFAULT_VENUE))
            gr.Markdown("### Parking rules")
            gr.Markdown("- " + "\n- ".join(DEFAULT_PARKING))

        # --- TAB 4: Logistics ---
        with gr.TabItem("Logistics"):
            gr.Markdown(
                "### Event Logistics\n"
                "- Shipping available until 10 days before event (typ. 3–5 business days)\n"
                "- Pickup: Student Center Bookstore during the week prior to event\n"
                "- Graduates arrive 90 minutes early; guests 60 minutes early\n"
                "- Lots A & B open 2 hours before; overflow as needed\n"
                "\n*Try asking the bot:* “What time should I arrive?” • “Where do I pick up the gown?”"
            )

    # ---------- Helpers that keep Chatbot history valid (list of [u,b]) ----------
    def _append_bot_md(history, md_text):
        """Append a bot markdown message without breaking the [user, bot] format."""
        history = history or []
        return history + [[None, md_text]]

    # ---------- Callbacks ----------
    def on_send(history, message, max_new_tokens, temperature, top_p):
        t = (message or "").strip()
        if not t:
            return history, history, ""  # no-op
        history = (history or []) + [[t, None]]
        reply = chat_pipeline(history[:-1], t, max_new_tokens, temperature, top_p)
        history[-1][1] = reply
        # Return updated state AND what the Chatbot should render
        return history, history, ""

    def _health_cb(history):
        md = (f"### Status: ✅ Healthy\n"
              f"- Model: `{MODEL_NAME}`\n"
              f"- Storefront module: {'yes' if USE_HELPERS else 'no'}\n"
              f"- Storefront JSON: {'loaded' if bool(STORE_DATA) else 'not found'}")
        new_hist = _append_bot_md(history, md)
        return new_hist, new_hist, "Status: ✅ Healthy"

    def _caps_cb(history):
        caps = [
            "Chat (LLM text-generation, memory-aware prompt)",
            "Storefront Q&A (parking, attire, products, logistics)",
            "Adjustable: max_new_tokens, temperature, top-p",
        ]
        md = "### Capabilities\n- " + "\n- ".join(caps)
        new_hist = _append_bot_md(history, md)
        return new_hist, new_hist

    # Wire up (note: always update both the state and the Chatbot value)
    send.click(on_send, [history_state, msg, max_new, temp, topp], [history_state, chat, msg])
    msg.submit(on_send, [history_state, msg, max_new, temp, topp], [history_state, chat, msg])

    # Chips
    chip1.click(lambda: "What are the parking rules?", outputs=msg)
    chip2.click(lambda: "Can I buy multiple parking passes?", outputs=msg)
    chip3.click(lambda: "Is formal attire required?", outputs=msg)
    chip4.click(lambda: "What time do the parking lots open?", outputs=msg)

    # Health / capabilities (append to chat, keep valid tuple format)
    health_btn.click(_health_cb, inputs=[history_state], outputs=[history_state, chat, status_md])
    caps_btn.click(_caps_cb, inputs=[history_state], outputs=[history_state, chat])

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))