Spaces:
Runtime error
Runtime error
fix theme
Browse files
app.py
CHANGED
|
@@ -249,19 +249,75 @@ def show_sample_predictions(num_samples: int = 10) -> str:
|
|
| 249 |
|
| 250 |
|
| 251 |
# Determine model directory (for HF Spaces, check environment variable or use default)
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
# Load model and data on startup
|
| 255 |
print("Initializing model and dataset...")
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
|
| 263 |
# Create Gradio interface
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
gr.Markdown(
|
| 266 |
"""
|
| 267 |
# 🔒 Prompt Injection Detection Classifier
|
|
|
|
| 249 |
|
| 250 |
|
| 251 |
# Determine model directory (for HF Spaces, check environment variable or use default)
|
| 252 |
+
# For HF Spaces, models are typically in the root directory or a subdirectory
|
| 253 |
+
MODEL_DIR = os.getenv("MODEL_DIR", None)
|
| 254 |
+
|
| 255 |
+
# Try common locations for models in HF Spaces
|
| 256 |
+
if MODEL_DIR is None:
|
| 257 |
+
possible_paths = [
|
| 258 |
+
"./model", # Common HF Spaces location
|
| 259 |
+
"./models",
|
| 260 |
+
"/model",
|
| 261 |
+
]
|
| 262 |
+
for path in possible_paths:
|
| 263 |
+
if os.path.exists(path) and os.path.isdir(path):
|
| 264 |
+
MODEL_DIR = path
|
| 265 |
+
break
|
| 266 |
+
|
| 267 |
+
# If still None, try to use a Hugging Face model identifier
|
| 268 |
+
if MODEL_DIR is None:
|
| 269 |
+
# Use environment variable if set, otherwise use default Hugging Face model
|
| 270 |
+
MODEL_DIR = os.getenv("HF_MODEL_ID", "Tameem7/Prompt-Classifier")
|
| 271 |
|
| 272 |
# Load model and data on startup
|
| 273 |
print("Initializing model and dataset...")
|
| 274 |
+
model_loaded = False
|
| 275 |
+
if MODEL_DIR:
|
| 276 |
+
try:
|
| 277 |
+
load_model_and_data(MODEL_DIR)
|
| 278 |
+
model_loaded = True
|
| 279 |
+
except Exception as e:
|
| 280 |
+
print(f"Error loading model: {e}")
|
| 281 |
+
print("Please ensure the model directory is correct or set MODEL_DIR environment variable.")
|
| 282 |
+
print("The app will still launch, but model functionality will be disabled.")
|
| 283 |
+
else:
|
| 284 |
+
print("No model directory specified. Please set MODEL_DIR environment variable.")
|
| 285 |
+
print("The app will still launch, but model functionality will be disabled.")
|
| 286 |
|
| 287 |
|
| 288 |
# Create Gradio interface
|
| 289 |
+
# Handle theme parameter compatibility with different Gradio versions
|
| 290 |
+
# Try to create Blocks with theme, fallback if not supported
|
| 291 |
+
try:
|
| 292 |
+
# Check if themes module exists and try to use it
|
| 293 |
+
if hasattr(gr, 'themes') and hasattr(gr.themes, 'Soft'):
|
| 294 |
+
app = gr.Blocks(title="Prompt Injection Detector", theme=gr.themes.Soft())
|
| 295 |
+
else:
|
| 296 |
+
app = gr.Blocks(title="Prompt Injection Detector")
|
| 297 |
+
except (TypeError, AttributeError):
|
| 298 |
+
# Fallback: theme parameter not supported in this Gradio version
|
| 299 |
+
try:
|
| 300 |
+
app = gr.Blocks(title="Prompt Injection Detector")
|
| 301 |
+
except TypeError:
|
| 302 |
+
# Even title might not be supported in very old versions
|
| 303 |
+
app = gr.Blocks()
|
| 304 |
+
|
| 305 |
+
with app:
|
| 306 |
+
# Show warning if model is not loaded
|
| 307 |
+
if not model_loaded:
|
| 308 |
+
gr.Markdown(
|
| 309 |
+
"""
|
| 310 |
+
## ⚠️ Warning: Model Not Loaded
|
| 311 |
+
|
| 312 |
+
The model could not be loaded. Please ensure:
|
| 313 |
+
- The model is uploaded to this Space, or
|
| 314 |
+
- Set the `MODEL_DIR` environment variable to the model path, or
|
| 315 |
+
- Set the `HF_MODEL_ID` environment variable to a Hugging Face model identifier
|
| 316 |
+
|
| 317 |
+
The app will still run, but classification features will be disabled.
|
| 318 |
+
"""
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
gr.Markdown(
|
| 322 |
"""
|
| 323 |
# 🔒 Prompt Injection Detection Classifier
|