Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from transformers import pipeline
|
| 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=["*"],
|
| 14 |
+
allow_methods=["*"],
|
| 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 |
+
print("Phishing model loaded!")
|
| 25 |
+
|
| 26 |
+
class URLRequest(BaseModel):
|
| 27 |
+
url: str
|
| 28 |
+
|
| 29 |
+
class PolicyRequest(BaseModel):
|
| 30 |
+
text: str
|
| 31 |
+
|
| 32 |
+
@app.get("/")
|
| 33 |
+
def root():
|
| 34 |
+
return {"status": "ok", "message": "PrivAware API is running"}
|
| 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):
|
| 42 |
+
if not request.url:
|
| 43 |
+
raise HTTPException(status_code=400, detail="URL is required")
|
| 44 |
+
try:
|
| 45 |
+
result = phishing_classifier(request.url[:512])[0]
|
| 46 |
+
label = result["label"]
|
| 47 |
+
confidence = round(result["score"] * 100, 2)
|
| 48 |
+
risk_score = round(confidence) if label == "PHISHING" else round(100 - confidence)
|
| 49 |
+
return {
|
| 50 |
+
"url": request.url,
|
| 51 |
+
"label": label,
|
| 52 |
+
"confidence": confidence,
|
| 53 |
+
"risk_score": risk_score,
|
| 54 |
+
"is_phishing": label == "PHISHING"
|
| 55 |
+
}
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 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 |
+
"label": "PENDING",
|
| 66 |
+
"message": "Privacy policy model coming Day 4",
|
| 67 |
+
"risk_score": 50
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|