Spaces:
Running on Zero
Running on Zero
Commit ·
594abbc
1
Parent(s): 77c5607
runtime weights download from ScamShield-model + zero-gpu
Browse files- app.py +15 -0
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -3,10 +3,23 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), "smishing_detector"))
|
|
| 3 |
|
| 4 |
import json
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
from predictor import SmishingPredictor
|
| 7 |
from explainability.shap_explainer import SmishingExplainer
|
| 8 |
|
| 9 |
CHECKPOINT = os.path.join(os.path.dirname(__file__), "smishing_detector", "best_model.pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
print("Loading ScamShield model...")
|
| 12 |
predictor = SmishingPredictor(CHECKPOINT)
|
|
@@ -14,6 +27,7 @@ print("Loading SHAP explainer...")
|
|
| 14 |
explainer = SmishingExplainer(predictor)
|
| 15 |
print("Model loaded. Ready.")
|
| 16 |
|
|
|
|
| 17 |
def analyze(message):
|
| 18 |
if not message or not message.strip():
|
| 19 |
return "⚠️ Please enter a message.", None, None
|
|
@@ -133,6 +147,7 @@ with gr.Blocks(title="ScamShield — Smishing Detector") as demo:
|
|
| 133 |
"**Features:** 9 URL signals + 8 text signals | **F1:** 0.98")
|
| 134 |
|
| 135 |
|
|
|
|
| 136 |
def api_predict(message: str) -> str:
|
| 137 |
return json.dumps(predictor.predict(message), ensure_ascii=False)
|
| 138 |
|
|
|
|
| 3 |
|
| 4 |
import json
|
| 5 |
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
from predictor import SmishingPredictor
|
| 8 |
from explainability.shap_explainer import SmishingExplainer
|
| 9 |
|
| 10 |
CHECKPOINT = os.path.join(os.path.dirname(__file__), "smishing_detector", "best_model.pt")
|
| 11 |
+
MODEL_REPO = os.getenv("SCAMSHIELD_MODEL_REPO", "ExistedYear/ScamShield-model")
|
| 12 |
+
|
| 13 |
+
if not os.path.exists(CHECKPOINT):
|
| 14 |
+
print(f"Checkpoint not found locally, downloading from {MODEL_REPO}...")
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
|
| 17 |
+
os.makedirs(os.path.dirname(CHECKPOINT), exist_ok=True)
|
| 18 |
+
downloaded = hf_hub_download(repo_id=MODEL_REPO, filename="best_model.pt")
|
| 19 |
+
import shutil
|
| 20 |
+
|
| 21 |
+
shutil.copyfile(downloaded, CHECKPOINT)
|
| 22 |
+
print(f"Weights downloaded to {CHECKPOINT}")
|
| 23 |
|
| 24 |
print("Loading ScamShield model...")
|
| 25 |
predictor = SmishingPredictor(CHECKPOINT)
|
|
|
|
| 27 |
explainer = SmishingExplainer(predictor)
|
| 28 |
print("Model loaded. Ready.")
|
| 29 |
|
| 30 |
+
@spaces.GPU
|
| 31 |
def analyze(message):
|
| 32 |
if not message or not message.strip():
|
| 33 |
return "⚠️ Please enter a message.", None, None
|
|
|
|
| 147 |
"**Features:** 9 URL signals + 8 text signals | **F1:** 0.98")
|
| 148 |
|
| 149 |
|
| 150 |
+
@spaces.GPU
|
| 151 |
def api_predict(message: str) -> str:
|
| 152 |
return json.dumps(predictor.predict(message), ensure_ascii=False)
|
| 153 |
|
requirements.txt
CHANGED
|
@@ -9,3 +9,4 @@ python-dotenv>=1.0.0
|
|
| 9 |
gradio>=5.0.0
|
| 10 |
pandas>=2.0.0
|
| 11 |
shap>=0.43.0
|
|
|
|
|
|
| 9 |
gradio>=5.0.0
|
| 10 |
pandas>=2.0.0
|
| 11 |
shap>=0.43.0
|
| 12 |
+
huggingface_hub>=0.20.0
|