phanerozoic commited on
Commit
b9083a8
·
verified ·
1 Parent(s): 9007cad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
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
- # Persistent cache + request log
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" # 2‑B fits HF CPU Space
28
- MAX_TURNS = 4 # keep last N user/AI pairs
29
  MAX_TOKENS = 64
30
  MAX_INPUT_CH = 300
31
 
32
  SYSTEM_MSG = (
33
- "You are **SchoolSpirit AI**, the official digital mascot for "
34
- "SchoolSpirit AI LLC, founded by Charles Norton in 2025. The company "
35
- "specializes in on‑prem AI chat mascots, custom fine‑tuning of language "
36
- "models, and turnkey GPU servers for K‑12 schools and education vendors.\n\n"
37
  "GUIDELINES:\n"
38
- "• Respond in a warm, encouraging tone suitable for students, parents, "
39
- "and staff.\n"
40
- "• Keep answers concise (≤ 4 sentences) unless asked for detail.\n"
41
- "• If unsure or out of scope, say you’re not sure and offer human follow‑up.\n"
42
- "• No personal data collection, no medical/legal/financial advice.\n"
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
- # Seed system + welcome messages on first call
 
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=[("", WELCOME_MSG)], # pre-populate AI welcome bubble
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"),