from fastapi import FastAPI, File, UploadFile import uvicorn import numpy as np import tensorflow as tf from PIL import Image import io # Load the Keras model (assuming model.h5 is in the same directory) model = tf.keras.models.load_model("deepfake_model_best.h5") app = FastAPI() # Preprocessing function def preprocess_image(image: Image.Image): image = image.resize((224, 224)) # Resize to model's expected input size image = np.array(image) / 255.0 # Normalize pixel values image = np.expand_dims(image, axis=0) # Add batch dimension return image @app.post("/predict") async def predict(file: UploadFile = File(...)): try: # Read image file contents = await file.read() image = Image.open(io.BytesIO(contents)).convert("RGB") # Preprocess image image = preprocess_image(image) # Make prediction prediction = model.predict(image) # Return result 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)