Spaces:
Running
Running
ZeyadMostafa22 commited on
Commit ·
ef5c75c
1
Parent(s): 10d1750
final commit
Browse files- app.py +62 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Step 1: Download the model from the Hugging Face Hub
|
| 9 |
+
model_path = hf_hub_download(repo_id="Zeyadd-Mostaffa/my_tensorflow_model", filename="my_model.h5")
|
| 10 |
+
|
| 11 |
+
# Step 2: Load the TensorFlow model
|
| 12 |
+
model = tf.keras.models.load_model(model_path)
|
| 13 |
+
|
| 14 |
+
# Step 3: Function to preprocess the input image
|
| 15 |
+
def load_and_preprocess_image(img, target_size=(256, 256)):
|
| 16 |
+
# Resize the image to the model's expected input size
|
| 17 |
+
img = img.resize(target_size)
|
| 18 |
+
|
| 19 |
+
# Convert to array and normalize
|
| 20 |
+
img_array = np.array(img) / 255.0
|
| 21 |
+
|
| 22 |
+
# Expand dimensions to match the input shape of the model
|
| 23 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 24 |
+
|
| 25 |
+
return img_array
|
| 26 |
+
|
| 27 |
+
# Step 4: Function to make predictions
|
| 28 |
+
def predict_image(img):
|
| 29 |
+
# Preprocess the image
|
| 30 |
+
img_array = load_and_preprocess_image(img)
|
| 31 |
+
|
| 32 |
+
# Make a prediction
|
| 33 |
+
prediction = model.predict(img_array)[0][0]
|
| 34 |
+
|
| 35 |
+
# Confidence scores
|
| 36 |
+
real_confidence = prediction * 100
|
| 37 |
+
fake_confidence = (1 - prediction) * 100
|
| 38 |
+
|
| 39 |
+
# Determine label
|
| 40 |
+
result_label = "Real" if real_confidence > fake_confidence else "Fake"
|
| 41 |
+
|
| 42 |
+
# Return results as text and an explanation
|
| 43 |
+
result_text = f"The model predicts this image is '{result_label}' with {max(real_confidence, fake_confidence):.2f}% confidence."
|
| 44 |
+
explanation = f"Real Confidence: {real_confidence:.2f}% | Fake Confidence: {fake_confidence:.2f}%"
|
| 45 |
+
|
| 46 |
+
return result_text, explanation
|
| 47 |
+
|
| 48 |
+
# Step 5: Define the Gradio interface
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=predict_image,
|
| 51 |
+
inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
|
| 52 |
+
outputs=[
|
| 53 |
+
gr.outputs.Textbox(label="Prediction Result"),
|
| 54 |
+
gr.outputs.Textbox(label="Confidence Scores")
|
| 55 |
+
],
|
| 56 |
+
title="Deepfake Image Detector",
|
| 57 |
+
description="Upload an image, and the model will classify whether it is a 'real' or 'fake' image using deep learning."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Step 6: Launch the app
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
gradio
|
| 3 |
+
huggingface_hub
|