Spaces:
Paused
Paused
Create app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
SchoolSpirit AI – minimal public chatbot Space
|
3 |
+
---------------------------------------------
|
4 |
+
• Loads Meta’s Llama‑3 3 B‑Instruct (fits HF CPU Space).
|
5 |
+
• Uses Hugging Face transformers + Gradio; no external deps.
|
6 |
+
• Keeps prompt short and trims history to fit the model context.
|
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() # keep Space logs clean
|
15 |
+
|
16 |
+
MODEL_ID = "meta-llama/Llama-3.2-3B-Instruct"
|
17 |
+
MAX_TURNS = 6 # retain last N exchanges
|
18 |
+
MAX_TOKENS = 220 # response length
|
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 "
|
22 |
+
"hardware for schools. Keep answers concise, upbeat, and age‑appropriate. "
|
23 |
+
"If you don’t know, say so and suggest contacting a human. Never request "
|
24 |
+
"personal data."
|
25 |
+
)
|
26 |
+
|
27 |
+
# ---------------- Model ------------------------------------------------------
|
28 |
+
try:
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
30 |
+
model = AutoModelForCausalLM.from_pretrained(
|
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=tokenizer,
|
39 |
+
max_new_tokens=MAX_TOKENS,
|
40 |
+
do_sample=True,
|
41 |
+
temperature=0.7,
|
42 |
+
)
|
43 |
+
except Exception as e: # noqa: BLE001
|
44 |
+
# Fatal startup failure – expose error in UI
|
45 |
+
def chat(history, user_msg):
|
46 |
+
return history + [(user_msg, f"Model load error: {e}")], ""
|
47 |
+
else:
|
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 |
+
# Generate
|
62 |
+
try:
|
63 |
+
completion = gen(prompt)[0]["generated_text"]
|
64 |
+
reply = completion.split("AI:", 1)[-1].strip()
|
65 |
+
except Exception as err: # noqa: BLE001
|
66 |
+
reply = "Sorry, something went wrong. Please try again later."
|
67 |
+
hf_logging.get_logger("SchoolSpirit").error(str(err))
|
68 |
+
|
69 |
+
history.append((user_msg, reply))
|
70 |
+
return history, ""
|
71 |
|
|
|
|
|
72 |
|
73 |
+
# ---------------- UI ---------------------------------------------------------
|
74 |
+
gr.ChatInterface(
|
75 |
+
chat,
|
76 |
+
title="SchoolSpirit AI Chat",
|
77 |
+
theme=gr.themes.Soft(primary_hue="blue"), # light‑blue chat UI
|
78 |
+
).launch()
|