Spaces:
Paused
Paused
Update app.py
Browse files
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 (
|
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 |
-
#
|
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 |
-
#
|
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 |
-
#
|
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 |
-
"""
|
66 |
-
out = re.sub(r"\s+", " ", text.strip())
|
67 |
-
return out or "β¦"
|
68 |
|
69 |
-
#
|
70 |
def chat(history, user_msg):
|
71 |
-
history = list(history)
|
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
|
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 |
-
#
|
105 |
def clear_chat():
|
106 |
return [], ""
|
107 |
|
108 |
-
#
|
109 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
110 |
gr.Markdown("# SchoolSpiritΒ AI Chat")
|
111 |
-
chatbot
|
112 |
-
msg_box
|
113 |
-
send_btn
|
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])
|