real-or-fake /
jamiko's picture
11b1efd verified
history blame contribute delete
No virus
2.92 kB
import streamlit as st
import os
import random
from PIL import Image
from transformers import pipeline
import pandas as pd
import matplotlib.pyplot as plt
# Sidebar with author and contact info
st.sidebar.header('About the App')
st.sidebar.write('This Image Classifier app can classify images as Real or Fake.')
st.sidebar.write('This AI is trained on a dataset of real and deepfake images. It uses a pre-trained model to classify images. You can upload an image or use a random image from the dataset to test the classifier.')
st.sidebar.write('Jan Mikolon')
st.sidebar.write('📧 Contact:')
st.sidebar.markdown('[![LinkedIn](](', unsafe_allow_html=True)
# Function to load a random image from a folder
def load_random_image(folder_path):
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
random_image_path = random.choice(images)
# Path to your images folder
folder_path = 'data/'
# Streamlit app
st.title('Image Classifier - Real or Fake')
# Allow users to upload an image
uploaded_image = st.file_uploader("Upload an image for classification", type=["png", "jpg", "jpeg"])
# Create two columns
col1, col2 = st.columns(2)
# Display the uploaded image or a random image
if uploaded_image is not None:
image =
col1.image(image, caption='Uploaded Image', use_column_width=True)
# Display a random image from the folder if no image is uploaded
if 'image_path' not in st.session_state or st.button('Load Random Image'):
st.session_state.image_path = load_random_image(folder_path)
col1.image(st.session_state.image_path, caption='Random Image', use_column_width=True)
# Classify button
if st.button('Classify'):
# This example uses a fixed classification result.
# You can replace this part with your actual model prediction logic.
pipe = pipeline("image-classification", model="dima806/deepfake_vs_real_image_detection")
if uploaded_image is not None:
classification_results = pipe(image)
classification_results = pipe(st.session_state.image_path)
# Convert the classification results to a DataFrame
df_results = pd.DataFrame(classification_results)
# Plotting
fig, ax = plt.subplots()['label'], df_results['score'], color=['blue', 'orange'])
ax.set_title('Classification Scores')
# Display the bar chart in Streamlit
# Load a new random image for next classification if no image is uploaded
if uploaded_image is None:
st.session_state.image_path = load_random_image(folder_path)