Spaces:
Sleeping
Sleeping
Fix import-time crash: lazy safety classifier, no hard raise on missing API key
Browse files- .gitignore +2 -0
- app.py +21 -7
- requirements.txt +2 -2
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
app.py
CHANGED
|
@@ -16,19 +16,32 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 16 |
# 环境变量加载
|
| 17 |
load_dotenv()
|
| 18 |
API_KEY = os.getenv("WAVESPEED_API_KEY")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
# 安全分类配置
|
| 23 |
MODEL_URL = "TostAI/nsfw-text-detection-large"
|
| 24 |
CLASS_NAMES = {0: "✅ SAFE", 1: "⚠️ QUESTIONABLE", 2: "🚫 UNSAFE"}
|
| 25 |
|
| 26 |
# 加载安全模型
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# 会话管理
|
|
@@ -101,6 +114,7 @@ def create_error_image(message):
|
|
| 101 |
|
| 102 |
@torch.no_grad()
|
| 103 |
def classify_prompt(prompt):
|
|
|
|
| 104 |
"""安全分类"""
|
| 105 |
inputs = tokenizer(prompt,
|
| 106 |
return_tensors="pt",
|
|
|
|
| 16 |
# 环境变量加载
|
| 17 |
load_dotenv()
|
| 18 |
API_KEY = os.getenv("WAVESPEED_API_KEY")
|
| 19 |
+
# Missing credentials are reported when a request is made, not at import.
|
| 20 |
+
# Raising here took the whole Space down with nothing but a traceback.
|
| 21 |
|
| 22 |
# 安全分类配置
|
| 23 |
MODEL_URL = "TostAI/nsfw-text-detection-large"
|
| 24 |
CLASS_NAMES = {0: "✅ SAFE", 1: "⚠️ QUESTIONABLE", 2: "🚫 UNSAFE"}
|
| 25 |
|
| 26 |
# 加载安全模型
|
| 27 |
+
# The safety classifier is loaded on first use rather than at import. Loading
|
| 28 |
+
# it at import meant a multi-hundred-MB download had to finish before the app
|
| 29 |
+
# could bind its port, so the Space was killed as unhealthy before it ever
|
| 30 |
+
# served a request. If the model cannot be loaded we raise, so a classifier
|
| 31 |
+
# failure never silently leaves prompts unscreened.
|
| 32 |
+
tokenizer = None
|
| 33 |
+
model = None
|
| 34 |
+
_classifier_lock = threading.Lock()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _ensure_classifier():
|
| 38 |
+
global tokenizer, model
|
| 39 |
+
if tokenizer is not None and model is not None:
|
| 40 |
+
return
|
| 41 |
+
with _classifier_lock:
|
| 42 |
+
if tokenizer is None or model is None:
|
| 43 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_URL)
|
| 44 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_URL)
|
| 45 |
|
| 46 |
|
| 47 |
# 会话管理
|
|
|
|
| 114 |
|
| 115 |
@torch.no_grad()
|
| 116 |
def classify_prompt(prompt):
|
| 117 |
+
_ensure_classifier()
|
| 118 |
"""安全分类"""
|
| 119 |
inputs = tokenizer(prompt,
|
| 120 |
return_tensors="pt",
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
gradio
|
| 2 |
aiohttp
|
| 3 |
-
plotly
|
| 4 |
python-dotenv
|
| 5 |
pydantic==2.8.2
|
| 6 |
torch
|
| 7 |
-
transformers==4.37.2
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
aiohttp
|
|
|
|
| 3 |
python-dotenv
|
| 4 |
pydantic==2.8.2
|
| 5 |
torch
|
| 6 |
+
transformers==4.37.2
|
| 7 |
+
requests
|