Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,20 @@
|
|
1 |
"""
|
2 |
-
SchoolSpirit AI –
|
3 |
-
|
4 |
-
• Loads Meta
|
5 |
-
•
|
6 |
-
•
|
7 |
-
• Gracefully handles model‑load or inference errors.
|
8 |
"""
|
9 |
|
10 |
import gradio as gr
|
11 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
12 |
from transformers.utils import logging as hf_logging
|
13 |
|
14 |
-
hf_logging.set_verbosity_error()
|
15 |
|
16 |
MODEL_ID = "meta-llama/Llama-3.2-3B-Instruct"
|
17 |
-
MAX_TURNS = 6
|
18 |
-
MAX_TOKENS = 220
|
19 |
SYSTEM_MSG = (
|
20 |
"You are SchoolSpirit AI, the friendly digital mascot for a company that "
|
21 |
"provides on‑prem AI chat mascots, fine‑tuning services, and turnkey GPU "
|
@@ -24,55 +23,71 @@ SYSTEM_MSG = (
|
|
24 |
"personal data."
|
25 |
)
|
26 |
|
27 |
-
#
|
28 |
try:
|
29 |
-
|
30 |
-
model
|
31 |
-
MODEL_ID,
|
32 |
-
device_map="auto", # auto‑detect CPU / GPU if available
|
33 |
-
torch_dtype="auto"
|
34 |
)
|
35 |
gen = pipeline(
|
36 |
"text-generation",
|
37 |
model=model,
|
38 |
-
tokenizer=
|
39 |
max_new_tokens=MAX_TOKENS,
|
40 |
do_sample=True,
|
41 |
temperature=0.7,
|
42 |
)
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
# ---------------- Chat handler ------------------------------------------
|
49 |
-
def chat(history, user_msg):
|
50 |
-
"""Gradio ChatInterface callback."""
|
51 |
-
# Trim history to last N turns
|
52 |
-
if len(history) > MAX_TURNS:
|
53 |
-
history = history[-MAX_TURNS:]
|
54 |
|
55 |
-
# Build prompt
|
56 |
-
prompt = SYSTEM_MSG + "\n"
|
57 |
-
for u, a in history:
|
58 |
-
prompt += f"User: {u}\nAI: {a}\n"
|
59 |
-
prompt += f"User: {user_msg}\nAI:"
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
|
73 |
-
# ---------------- UI ---------------------------------------------------------
|
74 |
gr.ChatInterface(
|
75 |
chat,
|
76 |
-
title="SchoolSpirit
|
77 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
|
|
78 |
).launch()
|
|
|
1 |
"""
|
2 |
+
SchoolSpirit AI – Llama‑3 3 B public chatbot Space
|
3 |
+
-------------------------------------------------
|
4 |
+
• Loads Meta Llama‑3.2‑3B‑Instruct.
|
5 |
+
• Keeps only last 6 turns to fit context.
|
6 |
+
• Handles model‑load or generation failures gracefully.
|
|
|
7 |
"""
|
8 |
|
9 |
import gradio as gr
|
10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
11 |
from transformers.utils import logging as hf_logging
|
12 |
|
13 |
+
hf_logging.set_verbosity_error()
|
14 |
|
15 |
MODEL_ID = "meta-llama/Llama-3.2-3B-Instruct"
|
16 |
+
MAX_TURNS = 6
|
17 |
+
MAX_TOKENS = 220
|
18 |
SYSTEM_MSG = (
|
19 |
"You are SchoolSpirit AI, the friendly digital mascot for a company that "
|
20 |
"provides on‑prem AI chat mascots, fine‑tuning services, and turnkey GPU "
|
|
|
23 |
"personal data."
|
24 |
)
|
25 |
|
26 |
+
# ---------------------------------------------------------------------------
|
27 |
try:
|
28 |
+
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
29 |
+
model = AutoModelForCausalLM.from_pretrained(
|
30 |
+
MODEL_ID, device_map="auto", torch_dtype="auto"
|
|
|
|
|
31 |
)
|
32 |
gen = pipeline(
|
33 |
"text-generation",
|
34 |
model=model,
|
35 |
+
tokenizer=tok,
|
36 |
max_new_tokens=MAX_TOKENS,
|
37 |
do_sample=True,
|
38 |
temperature=0.7,
|
39 |
)
|
40 |
+
model_error = None
|
41 |
+
except Exception as exc: # noqa: BLE001
|
42 |
+
model_error = f"Model load error: {exc}"
|
43 |
+
gen = None # ensure var exists
|
44 |
+
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
def chat(history, user_msg):
|
48 |
+
"""Gradio ChatInterface callback using new 'messages' format."""
|
49 |
+
if model_error:
|
50 |
+
history.append(
|
51 |
+
{
|
52 |
+
"role": "assistant",
|
53 |
+
"content": model_error,
|
54 |
+
}
|
55 |
+
)
|
56 |
+
return history
|
57 |
|
58 |
+
# Trim to last MAX_TURNS messages (role+assistant pairs)
|
59 |
+
if len(history) > MAX_TURNS * 2:
|
60 |
+
history = history[-MAX_TURNS * 2 :]
|
61 |
+
|
62 |
+
# Build prompt
|
63 |
+
prompt = SYSTEM_MSG + "\n"
|
64 |
+
for msg in history:
|
65 |
+
role = "User" if msg["role"] == "user" else "AI"
|
66 |
+
prompt += f"{role}: {msg['content']}\n"
|
67 |
+
prompt += f"User: {user_msg}\nAI:"
|
68 |
+
|
69 |
+
try:
|
70 |
+
completion = gen(prompt)[0]["generated_text"]
|
71 |
+
reply = completion.split("AI:", 1)[-1].strip()
|
72 |
+
except Exception as err: # noqa: BLE001
|
73 |
+
reply = (
|
74 |
+
"Sorry, something went wrong on my end. "
|
75 |
+
"Please try again in a few seconds."
|
76 |
+
)
|
77 |
+
hf_logging.get_logger("SchoolSpirit").error(str(err))
|
78 |
+
|
79 |
+
history.extend(
|
80 |
+
[
|
81 |
+
{"role": "user", "content": user_msg},
|
82 |
+
{"role": "assistant", "content": reply},
|
83 |
+
]
|
84 |
+
)
|
85 |
+
return history
|
86 |
|
87 |
|
|
|
88 |
gr.ChatInterface(
|
89 |
chat,
|
90 |
+
title="SchoolSpirit AI Chat",
|
91 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
92 |
+
type="messages", # avoids “tuples” deprecation warning
|
93 |
).launch()
|