""" Creating a REST API for predicting phishing probability of URLs. Exposes POST /predict endpoint: - Accepts JSON: {"url": ""} - Returns JSON: {"phishing": } Features: - Interactive API docs available at /docs (Swagger UI) and /redoc (ReDoc) - Automatic input validation - Returns 400 if "url" is missing or invalid - Returns 500 for unexpected errors Usage: 1. Run the server: uvicorn app:app --reload 2. Open browser to http://127.0.0.1:8000/docs to test endpoints interactively """ import sys from pathlib import Path sys.path.append(str(Path(__file__).resolve().parent.parent)) import torch from flask import Flask, request, jsonify from data.tokenizer import url_to_ids app = Flask(__name__) # Loading TorchScript Model and setting it to evaluation model = torch.jit.load("./models/phish_model_ts.pt") model.eval() @app.route("/", methods=["GET"]) def home(): return """ PhishGuard API

PhishGuard API

This server predicts how phishy a URL is.
Send a POST request to /predict with JSON:

{"url": "https://example.com"}

Tip: Replace https://example.com with any URL you want to check.

❶ Test in CMD / Terminal (5 seconds)

Copy → paste → Enter:
curl -X POST http://127.0.0.1:8000/predict \
     -H "Content-Type: application/json" \
     -d "{\"url\": \"https://example.com\"}"
    

Replace the URL with the one you want to test.

② GUI inside VS Code

Using Thunder Client (free):
1. Install "Thunder Client" from Extensions.
2. New Request → POST
3. URL: http://127.0.0.1:8000/predict
4. Body → Raw → JSON
5. Paste:
{"url": "https://example.com"}
6. Send.

③ Postman (any OS)

1. New Request → POST
2. URL: http://127.0.0.1:8000/predict
3. Body → Raw → JSON
4. Paste payload → Send.

What the number means

That's it — happy testing!

""", 200 @app.route("/predict", methods = ["POST"]) def predict(): """Processing POST requests and returning phishing score""" try: # Getting URL from request JSON safely data = request.get_json(force = True) url = data.get("url") # Converting URL to token IDs ids = torch.tensor([url_to_ids(url)], dtype= torch.long) # Running model inference without gradients with torch.no_grad(): score = float(model(ids)[0]) # Returning phishing probability as JSON return jsonify({"phishing" : score}) except Exception as e: return jsonify({"error" : str(e)}), 500 if __name__ == "__main__": # Running flask server on port 5000 # print(url_to_ids("http://secure-login-paypal.com.verify-account-update.co/login")) app.run(port=5000, debug = False)