Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,58 @@
|
|
1 |
"""
|
2 |
SchoolSpiritΒ AI β Graniteβ3.3β2B chatbot (GradioΒ 4.3, messages API)
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
|
|
|
|
|
6 |
import os, re, time, datetime, traceback
|
|
|
7 |
import gradio as gr
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
9 |
from transformers.utils import logging as hf_logging
|
10 |
|
11 |
-
#
|
12 |
-
os.environ["HF_HOME"] = "/data/.huggingface"
|
13 |
-
LOG_FILE = "/data/requests.log"
|
14 |
|
15 |
-
def log(msg: str):
|
|
|
16 |
ts = datetime.datetime.utcnow().strftime("%H:%M:%S.%f")[:-3]
|
17 |
line = f"[{ts}] {msg}"
|
18 |
print(line, flush=True)
|
19 |
-
try:
|
20 |
with open(LOG_FILE, "a") as f:
|
21 |
f.write(line + "\n")
|
22 |
except FileNotFoundError:
|
23 |
pass
|
24 |
|
25 |
-
#
|
26 |
-
MODEL_ID
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
)
|
32 |
SYSTEM_MSG = (
|
33 |
-
"You are SchoolSpiritΒ AI
|
34 |
-
"onβprem AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
hf_logging.set_verbosity_error()
|
39 |
try:
|
40 |
-
log("Loading model β¦")
|
41 |
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
42 |
model = AutoModelForCausalLM.from_pretrained(
|
43 |
MODEL_ID, device_map="auto", torch_dtype="auto"
|
@@ -48,62 +63,78 @@ try:
|
|
48 |
tokenizer=tok,
|
49 |
max_new_tokens=MAX_TOKENS,
|
50 |
do_sample=True,
|
51 |
-
temperature=0.
|
52 |
)
|
53 |
MODEL_ERR = None
|
54 |
log("Model loaded β")
|
55 |
-
except Exception as exc:
|
56 |
MODEL_ERR, gen = f"Model load error: {exc}", None
|
57 |
log(MODEL_ERR)
|
58 |
|
59 |
-
#
|
60 |
-
clean
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
log(f"User sent {len(user_msg)} chars")
|
|
|
|
|
66 |
if not history or history[0]["role"] != "system":
|
67 |
history = [{"role": "system", "content": SYSTEM_MSG}]
|
68 |
|
|
|
69 |
if MODEL_ERR:
|
70 |
return MODEL_ERR
|
71 |
|
|
|
72 |
user_msg = clean(user_msg or "")
|
73 |
if not user_msg:
|
74 |
return "Please type something."
|
75 |
if len(user_msg) > MAX_INPUT_CH:
|
76 |
return f"Message too long (>{MAX_INPUT_CH} chars)."
|
77 |
|
|
|
78 |
history.append({"role": "user", "content": user_msg})
|
79 |
-
history =
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
86 |
prompt = "\n".join(prompt_lines)
|
87 |
-
log(f"Prompt {len(prompt)} chars
|
88 |
|
|
|
89 |
t0 = time.time()
|
90 |
try:
|
91 |
-
raw
|
92 |
reply = clean(raw.split("AI:", 1)[-1])
|
93 |
-
#
|
94 |
-
reply = re.split(r"\
|
95 |
-
log(f"generate() {time.time() - t0:.2f}s
|
96 |
-
except Exception:
|
97 |
log("β Inference exception:\n" + traceback.format_exc())
|
98 |
-
reply = "Sorryβbackend crashed. Please try again later."
|
99 |
|
100 |
return reply
|
101 |
|
102 |
-
#
|
103 |
gr.ChatInterface(
|
104 |
fn=chat_fn,
|
105 |
chatbot=gr.Chatbot(height=480, type="messages"),
|
106 |
title="SchoolSpiritΒ AI Chat",
|
107 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
108 |
-
type="messages",
|
109 |
).launch()
|
|
|
1 |
"""
|
2 |
SchoolSpiritΒ AI β Graniteβ3.3β2B chatbot (GradioΒ 4.3, messages API)
|
3 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
4 |
+
β’ Persistent HF cache: HF_HOME=/data/.huggingface (25Β GB tier)
|
5 |
+
β’ Persistent request log: /data/requests.log
|
6 |
+
β’ Detailed system prompt (brand + guardrails)
|
7 |
+
β’ Traces every request: Received β Prompt β generate() timing
|
8 |
+
β’ Cleans replies & removes any stray βUser:β / βAI:β echoes
|
9 |
"""
|
10 |
|
11 |
+
# ββββββββββββββββββββ standard libraries βββββββββββββββββββββββββββββββββββ
|
12 |
+
from __future__ import annotations
|
13 |
import os, re, time, datetime, traceback
|
14 |
+
# βββββ gradio + hf transformers ββββββββββββββββββββββββββββββββββββββββββββ
|
15 |
import gradio as gr
|
16 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
17 |
from transformers.utils import logging as hf_logging
|
18 |
|
19 |
+
# ββββββββββββββββββββ persistent disk paths ββββββββββββββββββββββββββββββββ
|
20 |
+
os.environ["HF_HOME"] = "/data/.huggingface" # model / tokenizer cache
|
21 |
+
LOG_FILE = "/data/requests.log" # simple persistent log
|
22 |
|
23 |
+
def log(msg: str) -> None:
|
24 |
+
"""Print + append to /data/requests.log with UTC timestamp."""
|
25 |
ts = datetime.datetime.utcnow().strftime("%H:%M:%S.%f")[:-3]
|
26 |
line = f"[{ts}] {msg}"
|
27 |
print(line, flush=True)
|
28 |
+
try: # ignore firstβrun errors
|
29 |
with open(LOG_FILE, "a") as f:
|
30 |
f.write(line + "\n")
|
31 |
except FileNotFoundError:
|
32 |
pass
|
33 |
|
34 |
+
# ββββββββββββββββββββ chatbot configuration ββββββββββββββββββββββββββββββββ
|
35 |
+
MODEL_ID = "ibm-granite/granite-3.3-2b-instruct" # 2Β B params, Apacheβ2
|
36 |
+
MAX_TURNS = 6 # keep last N user/assistant pairs
|
37 |
+
MAX_TOKENS = 128 # reply length (raise if you have patience)
|
38 |
+
MAX_INPUT_CH = 400 # user message length guard
|
39 |
+
|
|
|
40 |
SYSTEM_MSG = (
|
41 |
+
"You are **SchoolSpiritΒ AI**, the friendly digital mascot for a company "
|
42 |
+
"that provides onβprem AI chat mascots, fineβtuning services, and turnkey "
|
43 |
+
"GPU hardware for schools.\n\n"
|
44 |
+
"β’ Keep answers concise, upbeat, and ageβappropriate (Kβ12).\n"
|
45 |
+
"β’ If you are unsure, say so and suggest contacting a human staff member.\n"
|
46 |
+
"β’ Never request personal data beyond an email if the user volunteers it.\n"
|
47 |
+
"β’ Do **not** provide medical, legal, or financial advice.\n"
|
48 |
+
"β’ No politics, mature content, or profanity.\n"
|
49 |
+
"Respond in a friendly, encouraging toneβas a helpful school mascot!"
|
50 |
)
|
51 |
|
52 |
+
# ββββββββββββββββββββ load model & pipeline ββββββββββββββββββββββββββββββββ
|
53 |
hf_logging.set_verbosity_error()
|
54 |
try:
|
55 |
+
log("Loading tokenizer & model β¦")
|
56 |
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
57 |
model = AutoModelForCausalLM.from_pretrained(
|
58 |
MODEL_ID, device_map="auto", torch_dtype="auto"
|
|
|
63 |
tokenizer=tok,
|
64 |
max_new_tokens=MAX_TOKENS,
|
65 |
do_sample=True,
|
66 |
+
temperature=0.7,
|
67 |
)
|
68 |
MODEL_ERR = None
|
69 |
log("Model loaded β")
|
70 |
+
except Exception as exc: # noqa: BLE001
|
71 |
MODEL_ERR, gen = f"Model load error: {exc}", None
|
72 |
log(MODEL_ERR)
|
73 |
|
74 |
+
# ββββββββββββββββββββ small helpers ββββββββββββββββββββββββββββββββββββββββ
|
75 |
+
def clean(txt: str) -> str:
|
76 |
+
"""Collapse whitespace & guarantee nonβempty string."""
|
77 |
+
return re.sub(r"\s+", " ", txt.strip()) or "β¦"
|
78 |
|
79 |
+
def trim_history(msgs: list[dict]) -> list[dict]:
|
80 |
+
"""Keep system + last MAX_TURNS pairs."""
|
81 |
+
return msgs if len(msgs) <= 1 + MAX_TURNS * 2 else [msgs[0]] + msgs[-MAX_TURNS * 2 :]
|
82 |
+
|
83 |
+
# ββββββββββββββββββββ core chat function βββββββββββββββββββββββββββββββββββ
|
84 |
+
def chat_fn(user_msg: str, history: list[dict] | None):
|
85 |
log(f"User sent {len(user_msg)} chars")
|
86 |
+
|
87 |
+
# ensure history list exists & begins with system prompt
|
88 |
if not history or history[0]["role"] != "system":
|
89 |
history = [{"role": "system", "content": SYSTEM_MSG}]
|
90 |
|
91 |
+
# fatal modelβload failure
|
92 |
if MODEL_ERR:
|
93 |
return MODEL_ERR
|
94 |
|
95 |
+
# basic userβinput checks
|
96 |
user_msg = clean(user_msg or "")
|
97 |
if not user_msg:
|
98 |
return "Please type something."
|
99 |
if len(user_msg) > MAX_INPUT_CH:
|
100 |
return f"Message too long (>{MAX_INPUT_CH} chars)."
|
101 |
|
102 |
+
# add user message & trim
|
103 |
history.append({"role": "user", "content": user_msg})
|
104 |
+
history = trim_history(history)
|
105 |
|
106 |
+
# build prompt string
|
107 |
+
prompt_lines: list[str] = []
|
108 |
+
for m in history:
|
109 |
+
if m["role"] == "system":
|
110 |
+
prompt_lines.append(m["content"])
|
111 |
+
elif m["role"] == "user":
|
112 |
+
prompt_lines.append(f"User: {m['content']}")
|
113 |
+
else:
|
114 |
+
prompt_lines.append(f"AI: {m['content']}")
|
115 |
+
prompt_lines.append("AI:")
|
116 |
prompt = "\n".join(prompt_lines)
|
117 |
+
log(f"Prompt {len(prompt)} chars β’ generatingβ¦")
|
118 |
|
119 |
+
# call generator
|
120 |
t0 = time.time()
|
121 |
try:
|
122 |
+
raw = gen(prompt)[0]["generated_text"]
|
123 |
reply = clean(raw.split("AI:", 1)[-1])
|
124 |
+
# β remove any echoed tags
|
125 |
+
reply = re.split(r"\b(?:User:|AI:)", reply, 1)[0].strip()
|
126 |
+
log(f"generate() {time.time() - t0:.2f}s β’ reply {len(reply)} chars")
|
127 |
+
except Exception: # noqa: BLE001
|
128 |
log("β Inference exception:\n" + traceback.format_exc())
|
129 |
+
reply = "SorryβAI backend crashed. Please try again later."
|
130 |
|
131 |
return reply
|
132 |
|
133 |
+
# ββββββββββββββββββββ Gradio UI ββββββββββββββββββββββββββββββββββββββββββββ
|
134 |
gr.ChatInterface(
|
135 |
fn=chat_fn,
|
136 |
chatbot=gr.Chatbot(height=480, type="messages"),
|
137 |
title="SchoolSpiritΒ AI Chat",
|
138 |
+
theme=gr.themes.Soft(primary_hue="blue"), # lightβblue accent
|
139 |
+
type="messages", # modern message dicts
|
140 |
).launch()
|