Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,9 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
# • Gradio Blocks UI (textbox + Send button)
|
8 |
-
##############################################################################
|
9 |
-
|
10 |
-
import os, re, time, datetime, traceback, torch, json, math
|
11 |
import gradio as gr
|
12 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
13 |
from transformers.utils import logging as hf_logging
|
@@ -48,7 +44,7 @@ SYSTEM_MSG = (
|
|
48 |
"• Friendly, concise (≤4 sentences unless prompted).\n"
|
49 |
"• No personal data collection; no medical/legal/financial advice.\n"
|
50 |
"• If uncertain, admit it & suggest human follow‑up.\n"
|
51 |
-
"•
|
52 |
)
|
53 |
WELCOME_MSG = "Welcome to SchoolSpirit AI! Do you have any questions?"
|
54 |
|
@@ -84,6 +80,7 @@ try:
|
|
84 |
max_new_tokens=MAX_NEW_TOKENS,
|
85 |
do_sample=True,
|
86 |
temperature=TEMPERATURE,
|
|
|
87 |
)
|
88 |
MODEL_ERR = None
|
89 |
log("Model loaded ✔")
|
@@ -152,10 +149,9 @@ def chat_fn(user_msg: str, display_history: list, state: dict):
|
|
152 |
# --- Generate
|
153 |
try:
|
154 |
start = time.time()
|
155 |
-
|
156 |
-
reply = strip(
|
157 |
-
|
158 |
-
log(f"Reply in {time.time()-start:.2f}s ({len(reply)} chars)")
|
159 |
except Exception:
|
160 |
log("❌ Inference error:\n" + traceback.format_exc())
|
161 |
reply = "Apologies—an internal error occurred. Please try again."
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import time
|
4 |
+
import datetime
|
5 |
+
import traceback
|
6 |
+
import torch
|
|
|
|
|
|
|
|
|
7 |
import gradio as gr
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
9 |
from transformers.utils import logging as hf_logging
|
|
|
44 |
"• Friendly, concise (≤4 sentences unless prompted).\n"
|
45 |
"• No personal data collection; no medical/legal/financial advice.\n"
|
46 |
"• If uncertain, admit it & suggest human follow‑up.\n"
|
47 |
+
"• avoid profanity, politics, mature themes."
|
48 |
)
|
49 |
WELCOME_MSG = "Welcome to SchoolSpirit AI! Do you have any questions?"
|
50 |
|
|
|
80 |
max_new_tokens=MAX_NEW_TOKENS,
|
81 |
do_sample=True,
|
82 |
temperature=TEMPERATURE,
|
83 |
+
return_full_text=False, # ← only return the newly generated text
|
84 |
)
|
85 |
MODEL_ERR = None
|
86 |
log("Model loaded ✔")
|
|
|
149 |
# --- Generate
|
150 |
try:
|
151 |
start = time.time()
|
152 |
+
result = generator(prompt)[0]
|
153 |
+
reply = strip(result["generated_text"])
|
154 |
+
log(f"Reply in {time.time() - start:.2f}s ({len(reply)} chars)")
|
|
|
155 |
except Exception:
|
156 |
log("❌ Inference error:\n" + traceback.format_exc())
|
157 |
reply = "Apologies—an internal error occurred. Please try again."
|