Spaces:
Runtime error
Runtime error
File size: 1,548 Bytes
e644b76 ea237a1 e644b76 ea237a1 e644b76 ea237a1 e644b76 ea237a1 e644b76 ea237a1 e644b76 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import numpy as np
import torch
app = FastAPI()
# Check if CUDA is available
if torch.cuda.is_available():
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("kmack/malicious-url-detection")
model = AutoModelForSequenceClassification.from_pretrained("kmack/malicious-url-detection")
model = model.to(device)
# Define the request model
class URLRequest(BaseModel):
url: str
# Prediction function
def get_prediction(input_text: str) -> dict:
label2id = model.config.label2id
inputs = tokenizer(input_text, return_tensors='pt', truncation=True)
inputs = inputs.to(device)
outputs = model(**inputs)
logits = outputs.logits
sigmoid = torch.nn.Sigmoid()
probs = sigmoid(logits.squeeze().cpu())
probs = probs.detach().numpy()
for i, k in enumerate(label2id.keys()):
label2id[k] = probs[i]
label2id = {k: float(v) for k, v in sorted(label2id.items(), key=lambda item: item[1].item(), reverse=True)}
return label2id
# Define the API endpoint for URL prediction
@app.post("/predict")
async def predict(url_request: URLRequest):
url_to_check = url_request.url
result = get_prediction(url_to_check)
return {"prediction": result}
# Health check endpoint
@app.get("/")
async def read_root():
return {"message": "API is up and running"}
|