|
from fastapi import FastAPI, File, UploadFile
|
|
import uvicorn
|
|
import numpy as np
|
|
import tensorflow as tf
|
|
from PIL import Image
|
|
import io
|
|
|
|
|
|
model = tf.keras.models.load_model("deepfake_model_best.h5")
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def preprocess_image(image: Image.Image):
|
|
image = image.resize((224, 224))
|
|
image = np.array(image) / 255.0
|
|
image = np.expand_dims(image, axis=0)
|
|
return image
|
|
|
|
@app.post("/predict")
|
|
async def predict(file: UploadFile = File(...)):
|
|
try:
|
|
|
|
contents = await file.read()
|
|
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
|
|
|
|
|
image = preprocess_image(image)
|
|
|
|
|
|
prediction = model.predict(image)
|
|
|
|
|
|
return {"prediction": prediction.tolist()}
|
|
except Exception as e:
|
|
return {"error": str(e)}
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|