phanerozoic commited on
Commit
46ffd61
·
verified ·
1 Parent(s): a9573e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -44
app.py CHANGED
@@ -1,21 +1,20 @@
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 "
@@ -24,55 +23,71 @@ SYSTEM_MSG = (
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()
 
1
  """
2
+ SchoolSpirit AI – Llama‑3 3 B public chatbot Space
3
+ -------------------------------------------------
4
+ • Loads Meta Llama‑3.23B‑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()