Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| # 1. Load your model | |
| model = tf.keras.models.load_model("face_forgery_detector.keras") | |
| # 2. Define your inference function | |
| def detect_forgery(image): | |
| # Preprocess the image to match your model’s input requirements | |
| img = tf.image.resize(image, (160, 160)) # Example size; adjust for your model | |
| img = tf.expand_dims(img, axis=0) | |
| img = img / 255.0 # Example normalization; adapt as needed | |
| # Run inference | |
| predictions = model.predict(img)[0] # e.g., [Real_prob, Fake_prob] | |
| # Suppose predictions = [prob_real, prob_fake] | |
| prob_real = predictions[0] | |
| prob_fake = predictions[1] | |
| # Format output | |
| # You can return a dictionary or a string. For example: | |
| if prob_fake > prob_real: | |
| return f"Forged (Fake) with confidence {prob_fake:.2f}" | |
| else: | |
| return f"Real with confidence {prob_real:.2f}" | |
| # 3. Build your Gradio interface | |
| demo = gr.Interface( | |
| fn=detect_forgery, | |
| inputs=gr.Image(type="numpy"), # 'type="numpy"' gives a NumPy array | |
| outputs="text", | |
| title="Face Forgery Detector", | |
| description="Upload a face image to check if it's likely forged or real." | |
| ) | |
| # 4. Launch the app (Gradio handles the rest) | |
| if __name__ == "__main__": | |
| demo.launch() | |