Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,85 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 5 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
|
| 6 |
+
# ------------------------------
|
| 7 |
+
# Logging setup
|
| 8 |
+
# ------------------------------
|
| 9 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
logger.info("🚀 Starting Privacy Audit AI Backend...")
|
| 12 |
+
|
| 13 |
+
# ------------------------------
|
| 14 |
+
# Load model and tokenizer
|
| 15 |
+
# ------------------------------
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 17 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 18 |
|
| 19 |
+
# ------------------------------
|
| 20 |
+
# FastAPI app
|
| 21 |
+
# ------------------------------
|
| 22 |
+
app = FastAPI(title="Privacy Audit AI", version="1.0.0")
|
| 23 |
+
|
| 24 |
+
# ------------------------------
|
| 25 |
+
# Request model
|
| 26 |
+
# ------------------------------
|
| 27 |
+
class AnalyzeInput(BaseModel):
|
| 28 |
+
os_apps: list[str] = []
|
| 29 |
+
browser_extensions: list[str] = []
|
| 30 |
+
account_apps: list[str] = []
|
| 31 |
+
|
| 32 |
+
# ------------------------------
|
| 33 |
+
# Utility functions
|
| 34 |
+
# ------------------------------
|
| 35 |
+
def generate_plain_text(input_text: str):
|
| 36 |
+
"""Generate plain-language explanation using the model."""
|
| 37 |
+
logger.info(f"Generating explanation for input: {input_text[:60]}...")
|
| 38 |
+
inputs = tokenizer(f"Explain privacy risks in plain language:\n{input_text}", return_tensors="pt")
|
| 39 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
| 40 |
+
explanation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
return explanation
|
| 42 |
+
|
| 43 |
+
def explain_risks(os_apps, browser_exts, account_apps):
|
| 44 |
+
summary_text = (
|
| 45 |
+
f"OS Apps: {', '.join(os_apps[:10])}, "
|
| 46 |
+
f"Browser Extensions: {', '.join(browser_exts[:5])}, "
|
| 47 |
+
f"Connected Apps: {', '.join(account_apps)}"
|
| 48 |
+
)
|
| 49 |
+
return generate_plain_text(summary_text)
|
| 50 |
|
| 51 |
+
# ------------------------------
|
| 52 |
+
# Endpoints
|
| 53 |
+
# ------------------------------
|
| 54 |
+
@app.get("/ping")
|
| 55 |
+
def ping():
|
| 56 |
+
return {"status": "ok", "message": "Backend is alive!"}
|
| 57 |
|
| 58 |
+
@app.post("/analyze")
|
| 59 |
+
def analyze(data: AnalyzeInput):
|
| 60 |
+
explanation = explain_risks(data.os_apps, data.browser_extensions, data.account_apps)
|
| 61 |
+
return {"plain_language": explanation}
|
| 62 |
|
| 63 |
+
@app.get("/audit")
|
| 64 |
+
def audit_mvp():
|
| 65 |
+
findings = "App X has camera access, App Y has location access, Chrome has 5 extensions"
|
| 66 |
+
explanation = generate_plain_text(findings)
|
| 67 |
+
return {
|
| 68 |
+
"findings": findings,
|
| 69 |
+
"plain_language": explanation,
|
| 70 |
+
"risk_level": "Medium"
|
| 71 |
+
}
|
| 72 |
|
| 73 |
+
@app.post("/audit")
|
| 74 |
+
def audit_mvp_post(data: AnalyzeInput):
|
| 75 |
+
findings_text = (
|
| 76 |
+
f"Detected {len(data.os_apps)} installed apps, "
|
| 77 |
+
f"{len(data.browser_extensions)} browser extensions, and "
|
| 78 |
+
f"{len(data.account_apps)} connected account apps."
|
| 79 |
+
)
|
| 80 |
+
explanation = generate_plain_text(findings_text)
|
| 81 |
+
return {
|
| 82 |
+
"findings": findings_text,
|
| 83 |
+
"plain_language": explanation,
|
| 84 |
+
"risk_level": "Medium"
|
| 85 |
+
}
|