File size: 1,237 Bytes
eba6632
 
 
 
 
5742f0d
b982dff
a66d267
eba6632
 
fd746a8
eba6632
 
978871d
eba6632
978871d
eba6632
 
 
 
 
 
 
 
 
 
 
 
 
cdc7c5b
eba6632
 
 
 
 
 
 
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
from fastapi import FastAPI, Query
from nsfw_detector import predict
from PIL import Image
import requests
from io import BytesIO

app = FastAPI()

# Carregar o modelo pré-treinado
model = predict.load_model("nsfw_mobilenet2.224x224.h5")  # Baixe o modelo ao configurar

@app.get("/check-nsfw/")
def check_nsfw(image_url: str = Query(..., description="URL da imagem para análise")):
    """
    Analisa uma imagem a partir de um URL e detecta conteúdo NSFW.
    """
    try:
        # Baixar a imagem a partir da URL
        response = requests.get(image_url)
        response.raise_for_status()  # Lança erro se a URL for inválida
        image = Image.open(BytesIO(response.content))
        
        # Fazer a predição
        predictions = predict.classify(model, {"image": image})
        
        # Resultados
        nsfw_score = predictions["image"]["porn"] + predictions["image"]["sexy"]
        sfw_score = predictions["image"]["neutral"]
        classification = "NSFW" if nsfw_score > sfw_score else "SFW"

        return {
            "image_url": image_url,
            "classification": classification,
            "scores": predictions["image"]
        }
    except Exception as e:
        return {"error": str(e)}