Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +65 -0
- requirements.txt +64 -0
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import pipeline
|
6 |
+
import pandas as pd
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
|
10 |
+
# Function to load a random image from a folder
|
11 |
+
def load_random_image(folder_path):
|
12 |
+
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
|
13 |
+
random_image_path = random.choice(images)
|
14 |
+
return Image.open(random_image_path)
|
15 |
+
|
16 |
+
# Path to your images folder
|
17 |
+
folder_path = 'data/'
|
18 |
+
|
19 |
+
# Streamlit app
|
20 |
+
st.title('Image Classifier - Real or Fake')
|
21 |
+
|
22 |
+
# Allow users to upload an image
|
23 |
+
uploaded_image = st.file_uploader("Upload an image for classification", type=["png", "jpg", "jpeg"])
|
24 |
+
|
25 |
+
# Create two columns
|
26 |
+
col1, col2 = st.columns(2)
|
27 |
+
|
28 |
+
# Display the uploaded image or a random image
|
29 |
+
if uploaded_image is not None:
|
30 |
+
image = Image.open(uploaded_image)
|
31 |
+
col1.image(image, caption='Uploaded Image', use_column_width=True)
|
32 |
+
else:
|
33 |
+
# Display a random image from the folder if no image is uploaded
|
34 |
+
if 'image_path' not in st.session_state or st.button('Load Random Image'):
|
35 |
+
st.session_state.image_path = load_random_image(folder_path)
|
36 |
+
col1.image(st.session_state.image_path, caption='Random Image', use_column_width=True)
|
37 |
+
|
38 |
+
# Classify button
|
39 |
+
if st.button('Classify'):
|
40 |
+
# This example uses a fixed classification result.
|
41 |
+
# You can replace this part with your actual model prediction logic.
|
42 |
+
pipe = pipeline("image-classification", model="dima806/deepfake_vs_real_image_detection")
|
43 |
+
|
44 |
+
if uploaded_image is not None:
|
45 |
+
classification_results = pipe(image)
|
46 |
+
else:
|
47 |
+
classification_results = pipe(st.session_state.image_path)
|
48 |
+
|
49 |
+
|
50 |
+
# Convert the classification results to a DataFrame
|
51 |
+
df_results = pd.DataFrame(classification_results)
|
52 |
+
|
53 |
+
# Plotting
|
54 |
+
fig, ax = plt.subplots()
|
55 |
+
ax.bar(df_results['label'], df_results['score'], color=['blue', 'orange'])
|
56 |
+
ax.set_ylabel('Scores')
|
57 |
+
ax.set_title('Classification Scores')
|
58 |
+
plt.tight_layout()
|
59 |
+
|
60 |
+
# Display the bar chart in Streamlit
|
61 |
+
col2.pyplot(fig)
|
62 |
+
|
63 |
+
# Load a new random image for next classification if no image is uploaded
|
64 |
+
if uploaded_image is None:
|
65 |
+
st.session_state.image_path = load_random_image(folder_path)
|
requirements.txt
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
-i https://pypi.org/simple
|
2 |
+
altair==5.2.0 ; python_version >= '3.8'
|
3 |
+
attrs==23.2.0 ; python_version >= '3.7'
|
4 |
+
blinker==1.7.0 ; python_version >= '3.8'
|
5 |
+
cachetools==5.3.3 ; python_version >= '3.7'
|
6 |
+
certifi==2024.2.2 ; python_version >= '3.6'
|
7 |
+
charset-normalizer==3.3.2 ; python_version >= '3.7'
|
8 |
+
click==8.1.7 ; python_version >= '3.7'
|
9 |
+
contourpy==1.2.0 ; python_version >= '3.9'
|
10 |
+
cycler==0.12.1 ; python_version >= '3.8'
|
11 |
+
filelock==3.13.1 ; python_version >= '3.8'
|
12 |
+
fonttools==4.49.0 ; python_version >= '3.8'
|
13 |
+
fsspec==2024.2.0 ; python_version >= '3.8'
|
14 |
+
gitdb==4.0.11 ; python_version >= '3.7'
|
15 |
+
gitpython==3.1.42 ; python_version >= '3.7'
|
16 |
+
huggingface-hub==0.21.2 ; python_version >= '3.8'
|
17 |
+
idna==3.6 ; python_version >= '3.5'
|
18 |
+
importlib-metadata==7.0.1 ; python_version >= '3.8'
|
19 |
+
jinja2==3.1.3 ; python_version >= '3.7'
|
20 |
+
jsonschema==4.21.1 ; python_version >= '3.8'
|
21 |
+
jsonschema-specifications==2023.12.1 ; python_version >= '3.8'
|
22 |
+
kiwisolver==1.4.5 ; python_version >= '3.7'
|
23 |
+
markdown-it-py==3.0.0 ; python_version >= '3.8'
|
24 |
+
markupsafe==2.1.5 ; python_version >= '3.7'
|
25 |
+
matplotlib==3.8.3
|
26 |
+
mdurl==0.1.2 ; python_version >= '3.7'
|
27 |
+
mpmath==1.3.0
|
28 |
+
networkx==3.2.1 ; python_version >= '3.9'
|
29 |
+
numpy==1.26.4 ; python_version >= '3.9'
|
30 |
+
packaging==23.2 ; python_version >= '3.7'
|
31 |
+
pandas==2.2.1 ; python_version >= '3.9'
|
32 |
+
pillow==10.2.0
|
33 |
+
protobuf==4.25.3 ; python_version >= '3.8'
|
34 |
+
pyarrow==15.0.0 ; python_version >= '3.8'
|
35 |
+
pydeck==0.8.1b0 ; python_version >= '3.7'
|
36 |
+
pygments==2.17.2 ; python_version >= '3.7'
|
37 |
+
pyparsing==3.1.1 ; python_full_version >= '3.6.8'
|
38 |
+
python-dateutil==2.8.2 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
39 |
+
pytz==2024.1
|
40 |
+
pyyaml==6.0.1 ; python_version >= '3.6'
|
41 |
+
referencing==0.33.0 ; python_version >= '3.8'
|
42 |
+
regex==2023.12.25 ; python_version >= '3.7'
|
43 |
+
requests==2.31.0 ; python_version >= '3.7'
|
44 |
+
rich==13.7.1 ; python_version >= '3.7'
|
45 |
+
rpds-py==0.18.0 ; python_version >= '3.8'
|
46 |
+
safetensors==0.4.2 ; python_version >= '3.7'
|
47 |
+
six==1.16.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
48 |
+
smmap==5.0.1 ; python_version >= '3.7'
|
49 |
+
streamlit==1.31.1
|
50 |
+
sympy==1.12 ; python_version >= '3.8'
|
51 |
+
tenacity==8.2.3 ; python_version >= '3.7'
|
52 |
+
tokenizers==0.15.2 ; python_version >= '3.7'
|
53 |
+
toml==0.10.2 ; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
54 |
+
toolz==0.12.1 ; python_version >= '3.7'
|
55 |
+
torch==2.2.1
|
56 |
+
tornado==6.4 ; python_version >= '3.8'
|
57 |
+
tqdm==4.66.2 ; python_version >= '3.7'
|
58 |
+
transformers==4.38.1
|
59 |
+
typing-extensions==4.10.0 ; python_version >= '3.8'
|
60 |
+
tzdata==2024.1 ; python_version >= '2'
|
61 |
+
tzlocal==5.2 ; python_version >= '3.8'
|
62 |
+
urllib3==2.2.1 ; python_version >= '3.8'
|
63 |
+
validators==0.22.0 ; python_version >= '3.8'
|
64 |
+
zipp==3.17.0 ; python_version >= '3.8'
|