Download app.py from Amiruzzaman/Deepfake_Image_Classification: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/Amiruzzaman/Deepfake_Image_Classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/Amiruzzaman/Deepfake_Image_Classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Amiruzzaman/Deepfake_Image_Classification/resolve/main/app.py
1.72 kB
| import streamlit as st | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| from PIL import Image | |
| # Load the trained model | |
| model = tf.keras.models.load_model('deepfake_detection.h5') | |
| # Function to load and preprocess the image | |
| def load_and_preprocess_image(uploaded_image): | |
| img = Image.open(uploaded_image) | |
| img = img.resize((150, 150)) # Resize image to match the input size expected by the model | |
| img_array = image.img_to_array(img) # Convert the image to a numpy array | |
| img_array = np.expand_dims(img_array, axis=0) # Expand dimensions to match the input shape (1, 150, 150, 3) | |
| img_array = img_array / 255.0 # Rescale the image array | |
| return img_array | |
| # Function to predict whether the image is real or fake | |
| def predict_image(uploaded_image): | |
| img_array = load_and_preprocess_image(uploaded_image) | |
| prediction = model.predict(img_array) | |
| if prediction < 0.5: | |
| return "Fake" | |
| else: | |
| return "Real" | |
| # Streamlit app layout | |
| st.title("Deepfake Image Classification") | |
| st.write("Upload an image and the model will predict whether it's Real or Fake.") | |
| # Image uploader | |
| uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg"]) | |
| # Prediction button | |
| if uploaded_image is not None: | |
| st.image(uploaded_image, caption="Uploaded Image", use_column_width=True) | |
| st.write("") | |
| if st.button("Predict"): | |
| result = predict_image(uploaded_image) | |
| if result == "Fake": | |
| st.write("The image is **<span style='color:red;'>Fake</span>**", unsafe_allow_html=True) | |
| else: | |
| st.write("The image is **<span style='color:cyan;'>Real</span>**", unsafe_allow_html=True) | |