phanerozoic commited on
Commit
e34a054
Β·
verified Β·
1 Parent(s): 747a64c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -1,8 +1,7 @@
1
  """
2
  SchoolSpiritΒ AI – Granite‑3.3‑2B chatbot Space
3
  ----------------------------------------------
4
- β€’ Uses IBM Granite‑3.3‑2B‑Instruct (public, no access token).
5
- β€’ Fits HF CPU Space (2‑B params, bfloat16).
6
  β€’ Keeps last MAX_TURNS exchanges.
7
  β€’ β€œClear chat” button resets context.
8
  β€’ Robust error handling & logging.
@@ -17,7 +16,7 @@ from transformers import (
17
  )
18
  from transformers.utils import logging as hf_logging
19
 
20
- # ────────────────────────── Config ──────────────────────────────────────────
21
  hf_logging.set_verbosity_error()
22
  LOG = hf_logging.get_logger("SchoolSpirit")
23
 
@@ -33,13 +32,11 @@ SYSTEM_MSG = (
33
  "say so and suggest contacting a human. Do not ask for personal data."
34
  )
35
 
36
- # ────────────────────────── Model Load ──────────────────────────────────────
37
  try:
38
  tok = AutoTokenizer.from_pretrained(MODEL_ID)
39
  model = AutoModelForCausalLM.from_pretrained(
40
- MODEL_ID,
41
- device_map="auto",
42
- torch_dtype="auto", # bfloat16/float16 under the hood
43
  )
44
  generator = pipeline(
45
  "text-generation",
@@ -55,20 +52,17 @@ except Exception as exc: # noqa: BLE001
55
  generator = None
56
  LOG.error(MODEL_ERR)
57
 
58
- # ────────────────────────── Helpers ────────────────────────────────────────
59
  def truncate(hist):
60
- """Return last MAX_TURNS (u,a) pairs."""
61
  return hist[-MAX_TURNS:] if len(hist) > MAX_TURNS else hist
62
 
63
 
64
  def clean(text: str) -> str:
65
- """Collapse whitespace; never return empty string."""
66
- out = re.sub(r"\s+", " ", text.strip())
67
- return out or "…"
68
 
69
- # ────────────────────────── Chat Callback ───────────────────────────────────
70
  def chat(history, user_msg):
71
- history = list(history) # Gradio ensures list of tuples
72
 
73
  if MODEL_ERR:
74
  history.append((user_msg, MODEL_ERR))
@@ -84,7 +78,6 @@ def chat(history, user_msg):
84
 
85
  history = truncate(history)
86
 
87
- # Build prompt
88
  prompt_lines = [SYSTEM_MSG]
89
  for u, a in history:
90
  prompt_lines += [f"User: {u}", f"AI: {a}"]
@@ -92,7 +85,7 @@ def chat(history, user_msg):
92
  prompt = "\n".join(prompt_lines)
93
 
94
  try:
95
- completion = generator(prompt, truncate=4096)[0]["generated_text"]
96
  reply = clean(completion.split("AI:", 1)[-1])
97
  except Exception as err: # noqa: BLE001
98
  LOG.error(f"Inference error: {err}")
@@ -101,16 +94,16 @@ def chat(history, user_msg):
101
  history.append((user_msg, reply))
102
  return history, ""
103
 
104
- # ────────────────────────── Clear Chat ──────────────────────────────────────
105
  def clear_chat():
106
  return [], ""
107
 
108
- # ────────────────────────── UI Launch ───────────────────────────────────────
109
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
110
  gr.Markdown("# SchoolSpiritΒ AI Chat")
111
- chatbot = gr.Chatbot()
112
- msg_box = gr.Textbox(placeholder="Ask me anything about SchoolSpiritΒ AI…")
113
- send_btn = gr.Button("Send")
114
  clear_btn = gr.Button("Clear Chat", variant="secondary")
115
 
116
  send_btn.click(chat, [chatbot, msg_box], [chatbot, msg_box])
 
1
  """
2
  SchoolSpiritΒ AI – Granite‑3.3‑2B chatbot Space
3
  ----------------------------------------------
4
+ β€’ Uses IBM Granite‑3.3‑2B‑Instruct (Apache‑2).
 
5
  β€’ Keeps last MAX_TURNS exchanges.
6
  β€’ β€œClear chat” button resets context.
7
  β€’ Robust error handling & logging.
 
16
  )
17
  from transformers.utils import logging as hf_logging
18
 
19
+ # ─────────────── Config ────────────────────────────────────────────────────
20
  hf_logging.set_verbosity_error()
21
  LOG = hf_logging.get_logger("SchoolSpirit")
22
 
 
32
  "say so and suggest contacting a human. Do not ask for personal data."
33
  )
34
 
35
+ # ─────────────── Model Load ────────────────────────────────────────────────
36
  try:
37
  tok = AutoTokenizer.from_pretrained(MODEL_ID)
38
  model = AutoModelForCausalLM.from_pretrained(
39
+ MODEL_ID, device_map="auto", torch_dtype="auto"
 
 
40
  )
41
  generator = pipeline(
42
  "text-generation",
 
52
  generator = None
53
  LOG.error(MODEL_ERR)
54
 
55
+ # ─────────────── Helpers ───────────────────────────────────────────────────
56
  def truncate(hist):
 
57
  return hist[-MAX_TURNS:] if len(hist) > MAX_TURNS else hist
58
 
59
 
60
  def clean(text: str) -> str:
61
+ return re.sub(r"\s+", " ", text.strip()) or "…"
 
 
62
 
63
+ # ─────────────── Chat Callback ─────────────────────────────────────────────
64
  def chat(history, user_msg):
65
+ history = list(history)
66
 
67
  if MODEL_ERR:
68
  history.append((user_msg, MODEL_ERR))
 
78
 
79
  history = truncate(history)
80
 
 
81
  prompt_lines = [SYSTEM_MSG]
82
  for u, a in history:
83
  prompt_lines += [f"User: {u}", f"AI: {a}"]
 
85
  prompt = "\n".join(prompt_lines)
86
 
87
  try:
88
+ completion = generator(prompt)[0]["generated_text"]
89
  reply = clean(completion.split("AI:", 1)[-1])
90
  except Exception as err: # noqa: BLE001
91
  LOG.error(f"Inference error: {err}")
 
94
  history.append((user_msg, reply))
95
  return history, ""
96
 
97
+ # ─────────────── Clear Chat ────────────────────────────────────────────────
98
  def clear_chat():
99
  return [], ""
100
 
101
+ # ─────────────── UI Launch ────────────────────────────────────────────────
102
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
103
  gr.Markdown("# SchoolSpiritΒ AI Chat")
104
+ chatbot = gr.Chatbot(type="tuple") # legacy tuple format
105
+ msg_box = gr.Textbox(placeholder="Ask me anything about SchoolSpiritΒ AI…")
106
+ send_btn = gr.Button("Send")
107
  clear_btn = gr.Button("Clear Chat", variant="secondary")
108
 
109
  send_btn.click(chat, [chatbot, msg_box], [chatbot, msg_box])