Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
4 |
from transformers.utils import logging as hf_logging
|
5 |
|
6 |
# ---------------------------------------------------------------------------
|
7 |
-
#
|
8 |
# ---------------------------------------------------------------------------
|
9 |
os.environ["HF_HOME"] = "/data/.huggingface"
|
10 |
LOG_FILE = "/data/requests.log"
|
@@ -24,23 +24,22 @@ def log(msg: str):
|
|
24 |
# ---------------------------------------------------------------------------
|
25 |
# Configuration
|
26 |
# ---------------------------------------------------------------------------
|
27 |
-
MODEL_ID = "ibm-granite/granite-3.3-2b-instruct"
|
28 |
-
MAX_TURNS = 4 #
|
29 |
MAX_TOKENS = 64
|
30 |
MAX_INPUT_CH = 300
|
31 |
|
32 |
SYSTEM_MSG = (
|
33 |
-
"You are **SchoolSpirit AI**, the
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"
|
37 |
"GUIDELINES:\n"
|
38 |
-
"•
|
39 |
-
"
|
40 |
-
"•
|
41 |
-
"•
|
42 |
-
"• No
|
43 |
-
"• Maintain professionalism—no profanity, politics, or mature themes."
|
44 |
)
|
45 |
|
46 |
WELCOME_MSG = "Welcome to SchoolSpirit AI! Do you have any questions?"
|
@@ -78,13 +77,17 @@ trim = lambda m: m if len(m) <= 1 + MAX_TURNS * 2 else [m[0]] + m[-MAX_TURNS * 2
|
|
78 |
|
79 |
|
80 |
def chat_fn(user_msg: str, history: list):
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
log(f"User sent {len(user_msg)} chars")
|
82 |
-
|
|
|
83 |
if not history or history[0]["role"] != "system":
|
84 |
-
history
|
85 |
-
{"role": "system", "content": SYSTEM_MSG},
|
86 |
-
{"role": "assistant", "content": WELCOME_MSG},
|
87 |
-
]
|
88 |
|
89 |
if MODEL_ERR:
|
90 |
return MODEL_ERR
|
@@ -128,7 +131,7 @@ gr.ChatInterface(
|
|
128 |
chatbot=gr.Chatbot(
|
129 |
height=480,
|
130 |
type="messages",
|
131 |
-
value=[
|
132 |
),
|
133 |
title="SchoolSpirit AI Chat",
|
134 |
theme=gr.themes.Soft(primary_hue="blue"),
|
|
|
4 |
from transformers.utils import logging as hf_logging
|
5 |
|
6 |
# ---------------------------------------------------------------------------
|
7 |
+
# Logging helpers
|
8 |
# ---------------------------------------------------------------------------
|
9 |
os.environ["HF_HOME"] = "/data/.huggingface"
|
10 |
LOG_FILE = "/data/requests.log"
|
|
|
24 |
# ---------------------------------------------------------------------------
|
25 |
# Configuration
|
26 |
# ---------------------------------------------------------------------------
|
27 |
+
MODEL_ID = "ibm-granite/granite-3.3-2b-instruct"
|
28 |
+
MAX_TURNS = 4 # retain last N user/AI exchanges
|
29 |
MAX_TOKENS = 64
|
30 |
MAX_INPUT_CH = 300
|
31 |
|
32 |
SYSTEM_MSG = (
|
33 |
+
"You are **SchoolSpirit AI**, the digital mascot for SchoolSpirit AI LLC, "
|
34 |
+
"founded by Charles Norton in 2025. The company installs on‑prem AI chat "
|
35 |
+
"mascots, offers custom fine‑tuning of language models, and ships turnkey "
|
36 |
+
"GPU hardware to K‑12 schools.\n\n"
|
37 |
"GUIDELINES:\n"
|
38 |
+
"• Use a warm, encouraging tone fit for students, parents, and staff.\n"
|
39 |
+
"• Keep replies short—no more than four sentences unless asked.\n"
|
40 |
+
"• If you’re unsure or out of scope, say so and suggest human follow‑up.\n"
|
41 |
+
"• Never collect personal data or provide medical, legal, or financial advice.\n"
|
42 |
+
"• No profanity, politics, or mature themes."
|
|
|
43 |
)
|
44 |
|
45 |
WELCOME_MSG = "Welcome to SchoolSpirit AI! Do you have any questions?"
|
|
|
77 |
|
78 |
|
79 |
def chat_fn(user_msg: str, history: list):
|
80 |
+
"""
|
81 |
+
Gradio passes:
|
82 |
+
user_msg : str
|
83 |
+
history : list[dict] -> [{'role':'assistant'|'user','content':...}, ...]
|
84 |
+
Return a string; ChatInterface will append it as assistant message.
|
85 |
+
"""
|
86 |
log(f"User sent {len(user_msg)} chars")
|
87 |
+
|
88 |
+
# Inject system message once
|
89 |
if not history or history[0]["role"] != "system":
|
90 |
+
history.insert(0, {"role": "system", "content": SYSTEM_MSG})
|
|
|
|
|
|
|
91 |
|
92 |
if MODEL_ERR:
|
93 |
return MODEL_ERR
|
|
|
131 |
chatbot=gr.Chatbot(
|
132 |
height=480,
|
133 |
type="messages",
|
134 |
+
value=[{"role": "assistant", "content": WELCOME_MSG}], # preloaded welcome
|
135 |
),
|
136 |
title="SchoolSpirit AI Chat",
|
137 |
theme=gr.themes.Soft(primary_hue="blue"),
|