JerameeUC commited on
Commit ·
e63200a
1
Parent(s): 269b3f6
Fixed what I broke I think this time.
Browse files- core/memory.py +10 -8
- core/model.py +1 -0
- core/storefront.py +25 -0
- space_app.py +0 -41
core/memory.py
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
|
|
| 1 |
def build_prompt_from_history(history, user_text, k=4) -> str:
|
| 2 |
"""
|
| 3 |
history is a list of [user, bot] pairs (Gradio Chatbot format).
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
lines = [
|
| 7 |
-
"
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
"-
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
]
|
| 15 |
for u, b in (history or [])[-k:]:
|
| 16 |
if u: lines.append(f"User: {u}")
|
|
@@ -18,3 +19,4 @@ def build_prompt_from_history(history, user_text, k=4) -> str:
|
|
| 18 |
lines.append(f"User: {user_text}")
|
| 19 |
lines.append("Assistant:")
|
| 20 |
return "\n".join(lines)
|
|
|
|
|
|
| 1 |
+
# core/memory.py
|
| 2 |
def build_prompt_from_history(history, user_text, k=4) -> str:
|
| 3 |
"""
|
| 4 |
history is a list of [user, bot] pairs (Gradio Chatbot format).
|
| 5 |
+
Keep a compact, factual system preface to ground the model.
|
| 6 |
"""
|
| 7 |
lines = [
|
| 8 |
+
"System: Answer questions about the university graduation storefront using the facts below.",
|
| 9 |
+
"System: Be concise. If unsure, say what is known.",
|
| 10 |
+
"Facts:",
|
| 11 |
+
"- Cap & Gown Set (CG-SET): $59.00, tassel included; ships until 10 days before the event.",
|
| 12 |
+
"- Parking Pass (PK-1): $10.00; multiple passes allowed per student.",
|
| 13 |
+
"- Venue: formal attire recommended; no muscle shirts; no sagging pants.",
|
| 14 |
+
"- Parking: no double parking; vehicles in handicap spaces will be towed.",
|
| 15 |
]
|
| 16 |
for u, b in (history or [])[-k:]:
|
| 17 |
if u: lines.append(f"User: {u}")
|
|
|
|
| 19 |
lines.append(f"User: {user_text}")
|
| 20 |
lines.append("Assistant:")
|
| 21 |
return "\n".join(lines)
|
| 22 |
+
|
core/model.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
| 1 |
+
# core/model.py
|
| 2 |
import os
|
| 3 |
from transformers import pipeline
|
| 4 |
|
core/storefront.py
CHANGED
|
@@ -97,3 +97,28 @@ def storefront_qna(data, user_text: str):
|
|
| 97 |
return "\n".join(lines)
|
| 98 |
|
| 99 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return "\n".join(lines)
|
| 98 |
|
| 99 |
return None
|
| 100 |
+
|
| 101 |
+
# app_storefront.py
|
| 102 |
+
|
| 103 |
+
def clean_generation(text: str) -> str:
|
| 104 |
+
s = (text or "").strip()
|
| 105 |
+
|
| 106 |
+
# If the prompt contained "Assistant:", keep only what comes after the last one
|
| 107 |
+
last = s.rfind("Assistant:")
|
| 108 |
+
if last != -1:
|
| 109 |
+
s = s[last + len("Assistant:"):].strip()
|
| 110 |
+
|
| 111 |
+
# If it accidentally continued into a new "User:" or instructions, cut there
|
| 112 |
+
cut_marks = ["\nUser:", "\nYOU ARE ANSWERING", "\nProducts:", "\nVenue rules:", "\nParking rules:"]
|
| 113 |
+
cut_positions = [s.find(m) for m in cut_marks if s.find(m) != -1]
|
| 114 |
+
if cut_positions:
|
| 115 |
+
s = s[:min(cut_positions)].strip()
|
| 116 |
+
|
| 117 |
+
# Collapse repeated lines like "Yes, multiple parking passes..." spam
|
| 118 |
+
lines, out = s.splitlines(), []
|
| 119 |
+
seen = set()
|
| 120 |
+
for ln in lines:
|
| 121 |
+
# dedupe only exact consecutive repeats; keep normal conversation lines
|
| 122 |
+
if not out or ln != out[-1]:
|
| 123 |
+
out.append(ln)
|
| 124 |
+
return "\n".join(out).strip()
|
space_app.py
DELETED
|
@@ -1,41 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
|
| 5 |
-
MODEL_NAME = os.getenv("HF_MODEL_GENERATION", "distilgpt2")
|
| 6 |
-
|
| 7 |
-
_pipe = None
|
| 8 |
-
def _get_pipe():
|
| 9 |
-
global _pipe
|
| 10 |
-
if _pipe is None:
|
| 11 |
-
_pipe = pipeline("text-generation", model=MODEL_NAME)
|
| 12 |
-
return _pipe
|
| 13 |
-
|
| 14 |
-
def chat_fn(message, max_new_tokens=128, temperature=0.8, top_p=0.95):
|
| 15 |
-
message = (message or "").strip()
|
| 16 |
-
if not message:
|
| 17 |
-
return "Please type something!"
|
| 18 |
-
pipe = _get_pipe()
|
| 19 |
-
out = pipe(
|
| 20 |
-
message,
|
| 21 |
-
max_new_tokens=int(max_new_tokens),
|
| 22 |
-
do_sample=True,
|
| 23 |
-
temperature=float(temperature),
|
| 24 |
-
top_p=float(top_p),
|
| 25 |
-
pad_token_id=50256
|
| 26 |
-
)
|
| 27 |
-
return out[0]["generated_text"]
|
| 28 |
-
|
| 29 |
-
with gr.Blocks(title="Agentic-Chat-bot") as demo:
|
| 30 |
-
gr.Markdown("# 🤖 Agentic Chat Bot\nGradio + Transformers demo")
|
| 31 |
-
prompt = gr.Textbox(label="Prompt", placeholder="Ask me anything…", lines=4)
|
| 32 |
-
out = gr.Textbox(label="Response", lines=6)
|
| 33 |
-
max_new = gr.Slider(32, 512, 128, 1, label="Max new tokens")
|
| 34 |
-
temp = gr.Slider(0.1, 1.5, 0.8, 0.05, label="Temperature")
|
| 35 |
-
topp = gr.Slider(0.1, 1.0, 0.95, 0.05, label="Top-p")
|
| 36 |
-
btn = gr.Button("Send")
|
| 37 |
-
btn.click(chat_fn, [prompt, max_new, temp, topp], out)
|
| 38 |
-
prompt.submit(chat_fn, [prompt, max_new, temp, topp], out)
|
| 39 |
-
|
| 40 |
-
if __name__ == "__main__":
|
| 41 |
-
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|