Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import uvicorn
|
|
| 7 |
|
| 8 |
app = FastAPI(title="PrivAware API", version="2.0")
|
| 9 |
|
| 10 |
-
# Allow Chrome extension to call this API
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
@@ -15,13 +14,19 @@ app.add_middleware(
|
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# Load models once at startup
|
| 19 |
print("Loading phishing model...")
|
| 20 |
phishing_classifier = pipeline(
|
| 21 |
"text-classification",
|
| 22 |
model="V3d4nt7/privaware-phishing-detector"
|
| 23 |
)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
class URLRequest(BaseModel):
|
| 27 |
url: str
|
|
@@ -31,11 +36,11 @@ class PolicyRequest(BaseModel):
|
|
| 31 |
|
| 32 |
@app.get("/")
|
| 33 |
def root():
|
| 34 |
-
return {"status": "ok", "message": "PrivAware API
|
| 35 |
|
| 36 |
@app.get("/health")
|
| 37 |
def health():
|
| 38 |
-
return {"status": "healthy", "models": ["phishing-detector"]}
|
| 39 |
|
| 40 |
@app.post("/scan-url")
|
| 41 |
def scan_url(request: URLRequest):
|
|
@@ -58,13 +63,31 @@ def scan_url(request: URLRequest):
|
|
| 58 |
|
| 59 |
@app.post("/scan-policy")
|
| 60 |
def scan_policy(request: PolicyRequest):
|
| 61 |
-
# Policy model comes Day 4 — for now returns a placeholder
|
| 62 |
if not request.text:
|
| 63 |
raise HTTPException(status_code=400, detail="Text is required")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return {
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
}
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI(title="PrivAware API", version="2.0")
|
| 9 |
|
|
|
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
print("Loading phishing model...")
|
| 18 |
phishing_classifier = pipeline(
|
| 19 |
"text-classification",
|
| 20 |
model="V3d4nt7/privaware-phishing-detector"
|
| 21 |
)
|
| 22 |
+
|
| 23 |
+
print("Loading policy model...")
|
| 24 |
+
policy_classifier = pipeline(
|
| 25 |
+
"text-classification",
|
| 26 |
+
model="V3d4nt7/privaware-policy-classifier"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
print("Both models loaded!")
|
| 30 |
|
| 31 |
class URLRequest(BaseModel):
|
| 32 |
url: str
|
|
|
|
| 36 |
|
| 37 |
@app.get("/")
|
| 38 |
def root():
|
| 39 |
+
return {"status": "ok", "message": "PrivAware API v2 running — both models live"}
|
| 40 |
|
| 41 |
@app.get("/health")
|
| 42 |
def health():
|
| 43 |
+
return {"status": "healthy", "models": ["phishing-detector", "policy-classifier"]}
|
| 44 |
|
| 45 |
@app.post("/scan-url")
|
| 46 |
def scan_url(request: URLRequest):
|
|
|
|
| 63 |
|
| 64 |
@app.post("/scan-policy")
|
| 65 |
def scan_policy(request: PolicyRequest):
|
|
|
|
| 66 |
if not request.text:
|
| 67 |
raise HTTPException(status_code=400, detail="Text is required")
|
| 68 |
+
try:
|
| 69 |
+
result = policy_classifier(request.text[:512])[0]
|
| 70 |
+
label = result["label"]
|
| 71 |
+
confidence = round(result["score"] * 100, 2)
|
| 72 |
+
risk_map = {"SAFE": 10, "RISKY": 55, "DECEPTIVE": 90}
|
| 73 |
+
risk_score = risk_map.get(label, 50)
|
| 74 |
+
return {
|
| 75 |
+
"label": label,
|
| 76 |
+
"confidence": confidence,
|
| 77 |
+
"risk_score": risk_score,
|
| 78 |
+
"is_deceptive": label == "DECEPTIVE"
|
| 79 |
+
}
|
| 80 |
+
except Exception as e:
|
| 81 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 82 |
+
|
| 83 |
+
@app.post("/scan-full")
|
| 84 |
+
def scan_full(request: URLRequest):
|
| 85 |
+
url_result = scan_url(request)
|
| 86 |
+
combined_score = url_result["risk_score"]
|
| 87 |
return {
|
| 88 |
+
"url": request.url,
|
| 89 |
+
"phishing": url_result,
|
| 90 |
+
"combined_risk_score": combined_score
|
| 91 |
}
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|