Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +116 -89
src/streamlit_app.py
CHANGED
|
@@ -1,92 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
import time
|
| 4 |
from core.inference import load_deepfake_model
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
classifier = st.session_state.classifier
|
| 29 |
-
|
| 30 |
-
st.markdown("### 📤 Image Upload")
|
| 31 |
-
|
| 32 |
-
uploaded_file = st.file_uploader(
|
| 33 |
-
"Select or Drag & Drop an Image (JPG, JPEG, PNG)",
|
| 34 |
-
type=["jpg", "jpeg", "png"],
|
| 35 |
-
label_visibility="collapsed"
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
if uploaded_file is not None:
|
| 39 |
-
try:
|
| 40 |
-
image = Image.open(uploaded_file).convert("RGB")
|
| 41 |
-
except Exception as e:
|
| 42 |
-
st.error(f"Image loading error: {e}")
|
| 43 |
-
st.stop()
|
| 44 |
-
|
| 45 |
-
col1, col2, col3 = st.columns([1, 2, 1])
|
| 46 |
-
with col2:
|
| 47 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 48 |
-
|
| 49 |
-
if st.button("Analyze Image"):
|
| 50 |
-
progress_bar = st.progress(0)
|
| 51 |
-
status_text = st.empty()
|
| 52 |
-
|
| 53 |
-
# Fake progress for UX
|
| 54 |
-
status_text.text("Extracting image features...")
|
| 55 |
-
for i in range(0, 50, 10):
|
| 56 |
-
time.sleep(0.1)
|
| 57 |
-
progress_bar.progress(i)
|
| 58 |
-
|
| 59 |
-
status_text.text("Running Deep Neural Network Inference...")
|
| 60 |
-
|
| 61 |
-
try:
|
| 62 |
-
results = classifier(image)
|
| 63 |
-
except Exception as e:
|
| 64 |
-
st.error(f"Inference failed: {e}")
|
| 65 |
-
st.stop()
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
top_result = results[0]
|
| 69 |
-
label = top_result['label'].lower()
|
| 70 |
-
score = top_result['score']
|
| 71 |
-
except Exception as e:
|
| 72 |
-
st.error(f"Result processing error: {e}")
|
| 73 |
-
st.stop()
|
| 74 |
-
|
| 75 |
-
for i in range(50, 100, 10):
|
| 76 |
-
time.sleep(0.1)
|
| 77 |
-
progress_bar.progress(i)
|
| 78 |
-
|
| 79 |
-
progress_bar.progress(100)
|
| 80 |
-
status_text.text("Analysis Complete.")
|
| 81 |
-
time.sleep(0.5)
|
| 82 |
-
|
| 83 |
-
progress_bar.empty()
|
| 84 |
-
status_text.empty()
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
is_fake = "fake" in label
|
| 88 |
-
confidence_percentage = round(score * 100, 2)
|
| 89 |
-
|
| 90 |
-
render_result(is_fake, confidence_percentage)
|
| 91 |
-
|
| 92 |
-
render_footer()
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# from PIL import Image
|
| 3 |
+
# import time
|
| 4 |
+
# from core.inference import load_deepfake_model
|
| 5 |
+
# from ui.styles import apply_custom_styles
|
| 6 |
+
# from ui.components import render_header, render_result, render_footer
|
| 7 |
+
|
| 8 |
+
# st.set_page_config(
|
| 9 |
+
# page_title="Deepfake Image Detector",
|
| 10 |
+
# page_icon="🕵️",
|
| 11 |
+
# layout="centered",
|
| 12 |
+
# initial_sidebar_state="collapsed",
|
| 13 |
+
# )
|
| 14 |
+
|
| 15 |
+
# apply_custom_styles()
|
| 16 |
+
|
| 17 |
+
# render_header()
|
| 18 |
+
|
| 19 |
+
# if "classifier" not in st.session_state:
|
| 20 |
+
# with st.spinner("Initializing Deepfake Detection AI..."):
|
| 21 |
+
# try:
|
| 22 |
+
# st.session_state.classifier = load_deepfake_model()
|
| 23 |
+
# st.success("Model loaded successfully ✅")
|
| 24 |
+
# except Exception as e:
|
| 25 |
+
# st.error(f"Model loading failed: {e}")
|
| 26 |
+
# st.stop()
|
| 27 |
+
|
| 28 |
+
# classifier = st.session_state.classifier
|
| 29 |
+
|
| 30 |
+
# st.markdown("### 📤 Image Upload")
|
| 31 |
+
|
| 32 |
+
# uploaded_file = st.file_uploader(
|
| 33 |
+
# "Select or Drag & Drop an Image (JPG, JPEG, PNG)",
|
| 34 |
+
# type=["jpg", "jpeg", "png"],
|
| 35 |
+
# label_visibility="collapsed"
|
| 36 |
+
# )
|
| 37 |
+
|
| 38 |
+
# if uploaded_file is not None:
|
| 39 |
+
# try:
|
| 40 |
+
# image = Image.open(uploaded_file).convert("RGB")
|
| 41 |
+
# except Exception as e:
|
| 42 |
+
# st.error(f"Image loading error: {e}")
|
| 43 |
+
# st.stop()
|
| 44 |
+
|
| 45 |
+
# col1, col2, col3 = st.columns([1, 2, 1])
|
| 46 |
+
# with col2:
|
| 47 |
+
# st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 48 |
+
|
| 49 |
+
# if st.button("Analyze Image"):
|
| 50 |
+
# progress_bar = st.progress(0)
|
| 51 |
+
# status_text = st.empty()
|
| 52 |
+
|
| 53 |
+
# # Fake progress for UX
|
| 54 |
+
# status_text.text("Extracting image features...")
|
| 55 |
+
# for i in range(0, 50, 10):
|
| 56 |
+
# time.sleep(0.1)
|
| 57 |
+
# progress_bar.progress(i)
|
| 58 |
+
|
| 59 |
+
# status_text.text("Running Deep Neural Network Inference...")
|
| 60 |
+
|
| 61 |
+
# try:
|
| 62 |
+
# results = classifier(image)
|
| 63 |
+
# except Exception as e:
|
| 64 |
+
# st.error(f"Inference failed: {e}")
|
| 65 |
+
# st.stop()
|
| 66 |
+
|
| 67 |
+
# try:
|
| 68 |
+
# top_result = results[0]
|
| 69 |
+
# label = top_result['label'].lower()
|
| 70 |
+
# score = top_result['score']
|
| 71 |
+
# except Exception as e:
|
| 72 |
+
# st.error(f"Result processing error: {e}")
|
| 73 |
+
# st.stop()
|
| 74 |
+
|
| 75 |
+
# for i in range(50, 100, 10):
|
| 76 |
+
# time.sleep(0.1)
|
| 77 |
+
# progress_bar.progress(i)
|
| 78 |
+
|
| 79 |
+
# progress_bar.progress(100)
|
| 80 |
+
# status_text.text("Analysis Complete.")
|
| 81 |
+
# time.sleep(0.5)
|
| 82 |
+
|
| 83 |
+
# progress_bar.empty()
|
| 84 |
+
# status_text.empty()
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# is_fake = "fake" in label
|
| 88 |
+
# confidence_percentage = round(score * 100, 2)
|
| 89 |
+
|
| 90 |
+
# render_result(is_fake, confidence_percentage)
|
| 91 |
+
|
| 92 |
+
# render_footer()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
import streamlit as st
|
| 96 |
from PIL import Image
|
|
|
|
| 97 |
from core.inference import load_deepfake_model
|
| 98 |
+
|
| 99 |
+
st.set_option('server.enableXsrfProtection', False)
|
| 100 |
+
st.set_option('server.enableCORS', False)
|
| 101 |
+
|
| 102 |
+
st.title("Stable Upload Test")
|
| 103 |
+
|
| 104 |
+
@st.cache_resource
|
| 105 |
+
def get_model():
|
| 106 |
+
return load_deepfake_model()
|
| 107 |
+
|
| 108 |
+
classifier = get_model()
|
| 109 |
+
|
| 110 |
+
uploaded_file = st.file_uploader("Upload Image")
|
| 111 |
+
|
| 112 |
+
if uploaded_file:
|
| 113 |
+
img = Image.open(uploaded_file)
|
| 114 |
+
st.image(img)
|
| 115 |
+
|
| 116 |
+
if st.button("Analyze"):
|
| 117 |
+
st.write("Running...")
|
| 118 |
+
result = classifier(img)
|
| 119 |
+
st.write(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|