Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,74 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import pipeline
|
4 |
+
import requests
|
5 |
|
6 |
+
# Set the title and sidebar
|
7 |
+
st.set_page_config(page_title="Deepfake vs Real Image Detection", page_icon="🤖")
|
8 |
+
|
9 |
+
st.title("Deepfake vs Real Image Detection")
|
10 |
+
st.sidebar.title("Options")
|
11 |
+
|
12 |
+
# Description
|
13 |
+
st.markdown(
|
14 |
+
"""
|
15 |
+
Welcome to the Deepfake vs Real Image Detection app!
|
16 |
+
Upload an image and let our model determine if it is real or a deepfake.
|
17 |
+
"""
|
18 |
+
)
|
19 |
+
|
20 |
+
# Load the pipeline
|
21 |
+
model_name = "vm24bho/net_dfm_myimg"
|
22 |
+
try:
|
23 |
+
pipe = pipeline('image-classification', model=model_name)
|
24 |
+
except Exception as e:
|
25 |
+
st.error(f"Error loading model: {e}")
|
26 |
+
st.stop()
|
27 |
+
|
28 |
+
# Upload image
|
29 |
+
st.sidebar.subheader("Upload Image")
|
30 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
31 |
+
|
32 |
+
# Add a sample image option
|
33 |
+
sample_image = None
|
34 |
+
if st.sidebar.button("Use Sample Image"):
|
35 |
+
sample_url = "https://drive.google.com/file/d/15zh_XjwH9gGAzNdNUEgHraYAzE4GdSRU/view?usp=drive_link" # Replace with a valid sample image URL
|
36 |
+
try:
|
37 |
+
response = requests.get(sample_url, stream=True)
|
38 |
+
sample_image = Image.open(response.raw)
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"Error loading sample image: {e}")
|
41 |
+
st.stop()
|
42 |
+
|
43 |
+
if uploaded_file is not None:
|
44 |
+
image = Image.open(uploaded_file)
|
45 |
+
elif sample_image is not None:
|
46 |
+
image = sample_image
|
47 |
+
else:
|
48 |
+
image = None
|
49 |
+
|
50 |
+
# Display the uploaded image
|
51 |
+
if image is not None:
|
52 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
53 |
+
st.write("")
|
54 |
+
st.write("Classifying...")
|
55 |
+
|
56 |
+
# Apply the model
|
57 |
+
try:
|
58 |
+
result = pipe(image)
|
59 |
+
except Exception as e:
|
60 |
+
st.error(f"Error classifying image: {e}")
|
61 |
+
st.stop()
|
62 |
+
|
63 |
+
# Determine if the image is classified as real or fake
|
64 |
+
if result[0]['label'] == 'REAL':
|
65 |
+
st.write("Output is: Real")
|
66 |
+
else:
|
67 |
+
st.write("Output is: Fake")
|
68 |
+
|
69 |
+
# Display the result
|
70 |
+
st.subheader("Classification Result")
|
71 |
+
for res in result:
|
72 |
+
st.write(f"**{res['label']}**: {res['score']*100:.2f}%")
|
73 |
+
else:
|
74 |
+
st.sidebar.info("Upload an image to get started or use the sample image.")
|