File size: 2,162 Bytes
7b8f7cd
 
 
 
9f4b075
7b8f7cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
070afda
 
7b8f7cd
070afda
7b8f7cd
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
from PIL import Image
from transformers import pipeline
import requests

# Set the title and sidebar
st.set_page_config(page_title="Deepfake vs Real Image Detection", page_icon="🤖")

st.title("Deepfake vs Real Image Detection")
st.sidebar.title("Options")

# Description
st.markdown(
    """
    Welcome to the Deepfake vs Real Image Detection app! 
    Upload an image and let our model determine if it is real or a deepfake.
    """
)

# Load the pipeline
model_name = "vm24bho/net_dfm_myimg"
try:
    pipe = pipeline('image-classification', model=model_name)
except Exception as e:
    st.error(f"Error loading model: {e}")
    st.stop()

# Upload image
st.sidebar.subheader("Upload Image")
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

# Add a sample image option
sample_image = None
if st.sidebar.button("Use Sample Image"):
    sample_url = "https://drive.google.com/file/d/15zh_XjwH9gGAzNdNUEgHraYAzE4GdSRU/view?usp=drive_link"  # Replace with a valid sample image URL
    try:
        response = requests.get(sample_url, stream=True)
        sample_image = Image.open(response.raw)
    except Exception as e:
        st.error(f"Error loading sample image: {e}")
        st.stop()

if uploaded_file is not None:
    image = Image.open(uploaded_file)
elif sample_image is not None:
    image = sample_image
else:
    image = None

# Display the uploaded image
if image is not None:
    st.image(image, caption='Uploaded Image', use_column_width=True)
    st.write("")
    st.write("Classifying...")

    # Apply the model
    try:
        result = pipe(image)
    except Exception as e:
        st.error(f"Error classifying image: {e}")
        st.stop()

    # Determine if the image is classified as real or fake
    if result[0]['label'] == 'FAKE':
        st.write("This Image is: Fake")
    else:
        st.write("This Image is: Real")

    # Display the result
    st.subheader("Classification Result")
    for res in result:
        st.write(f"**{res['label']}**: {res['score']*100:.2f}%")
else:
    st.sidebar.info("Upload an image to get started or use the sample image.")