Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,222 +1,246 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
import gc
|
| 6 |
-
from transformers import AutoProcessor, AutoModelForCausalLM, BitsAndBytesConfig
|
| 7 |
from peft import PeftModel
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
st.set_page_config(
|
| 11 |
-
page_title="Deepfake
|
| 12 |
-
|
| 13 |
-
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
st.title("Deepfake Image Analyzer")
|
| 18 |
st.markdown("Upload an image to analyze it for possible deepfake manipulation")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
| 22 |
-
gc.collect()
|
| 23 |
if torch.cuda.is_available():
|
| 24 |
-
torch.cuda.
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def init_device():
|
| 28 |
-
if torch.cuda.is_available():
|
| 29 |
-
st.sidebar.success("β GPU available: Using CUDA")
|
| 30 |
-
return "cuda"
|
| 31 |
else:
|
| 32 |
-
st.sidebar.warning("β οΈ No GPU detected
|
| 33 |
-
return
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
return None, None
|
| 81 |
|
| 82 |
# Function to fix cross-attention masks
|
| 83 |
-
def
|
| 84 |
-
"""Fix cross-attention mask dimensions if needed"""
|
| 85 |
if 'cross_attention_mask' in inputs and 0 in inputs['cross_attention_mask'].shape:
|
| 86 |
batch_size, seq_len, _, num_tiles = inputs['cross_attention_mask'].shape
|
| 87 |
-
visual_features = 6404 #
|
| 88 |
-
new_mask = torch.ones(
|
| 89 |
-
|
| 90 |
-
device=inputs['cross_attention_mask'].device
|
| 91 |
-
)
|
| 92 |
inputs['cross_attention_mask'] = new_mask
|
| 93 |
-
|
| 94 |
-
return
|
| 95 |
-
|
| 96 |
-
# Create sidebar with options
|
| 97 |
-
with st.sidebar:
|
| 98 |
-
st.header("Options")
|
| 99 |
-
temperature = st.slider("Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.1,
|
| 100 |
-
help="Higher values make output more random, lower values more deterministic")
|
| 101 |
-
max_length = st.slider("Maximum response length", min_value=100, max_value=1000, value=500, step=50)
|
| 102 |
-
|
| 103 |
-
custom_prompt = st.text_area(
|
| 104 |
-
"Custom instruction (optional)",
|
| 105 |
-
value="Analyze this image and determine if it's a deepfake. Provide both technical and non-technical explanations.",
|
| 106 |
-
height=100
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
st.markdown("### About")
|
| 110 |
-
st.markdown("""
|
| 111 |
-
This app uses a fine-tuned Llama 3.2 Vision model to detect and explain deepfakes.
|
| 112 |
-
|
| 113 |
-
The analyzer looks for:
|
| 114 |
-
- Inconsistencies in facial features
|
| 115 |
-
- Unusual lighting or shadows
|
| 116 |
-
- Unnatural blur patterns
|
| 117 |
-
- Artifacts around edges
|
| 118 |
-
- Texture inconsistencies
|
| 119 |
-
|
| 120 |
-
Model by [saakshigupta](https://huggingface.co/saakshigupta/deepfake-explainer-1)
|
| 121 |
-
""")
|
| 122 |
|
| 123 |
-
# Load model
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
try:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
st.exception(e)
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
else:
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
|
|
|
|
|
|
| 5 |
from peft import PeftModel
|
| 6 |
+
from unsloth import FastVisionModel
|
| 7 |
+
import tempfile
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
# App title and description
|
| 11 |
st.set_page_config(
|
| 12 |
+
page_title="Deepfake Analyzer",
|
| 13 |
+
layout="wide",
|
| 14 |
+
page_icon="π"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Main title and description
|
| 18 |
st.title("Deepfake Image Analyzer")
|
| 19 |
st.markdown("Upload an image to analyze it for possible deepfake manipulation")
|
| 20 |
|
| 21 |
+
# Check for GPU availability
|
| 22 |
+
def check_gpu():
|
|
|
|
| 23 |
if torch.cuda.is_available():
|
| 24 |
+
gpu_info = torch.cuda.get_device_properties(0)
|
| 25 |
+
st.sidebar.success(f"β
GPU available: {gpu_info.name} ({gpu_info.total_memory / (1024**3):.2f} GB)")
|
| 26 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
+
st.sidebar.warning("β οΈ No GPU detected. Analysis will be slower.")
|
| 29 |
+
return False
|
| 30 |
|
| 31 |
+
# Sidebar components
|
| 32 |
+
st.sidebar.title("Options")
|
| 33 |
|
| 34 |
+
# Temperature slider
|
| 35 |
+
temperature = st.sidebar.slider(
|
| 36 |
+
"Temperature",
|
| 37 |
+
min_value=0.1,
|
| 38 |
+
max_value=1.0,
|
| 39 |
+
value=0.7,
|
| 40 |
+
step=0.1,
|
| 41 |
+
help="Higher values make output more random, lower values more deterministic"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Max response length slider
|
| 45 |
+
max_tokens = st.sidebar.slider(
|
| 46 |
+
"Maximum Response Length",
|
| 47 |
+
min_value=100,
|
| 48 |
+
max_value=1000,
|
| 49 |
+
value=500,
|
| 50 |
+
step=50,
|
| 51 |
+
help="The maximum number of tokens in the response"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Custom instruction text area in sidebar
|
| 55 |
+
custom_instruction = st.sidebar.text_area(
|
| 56 |
+
"Custom Instructions (Advanced)",
|
| 57 |
+
value="Analyze for facial inconsistencies, lighting irregularities, mismatched shadows, and other signs of manipulation.",
|
| 58 |
+
help="Add specific instructions for the model"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# About section in sidebar
|
| 62 |
+
st.sidebar.markdown("---")
|
| 63 |
+
st.sidebar.subheader("About")
|
| 64 |
+
st.sidebar.markdown("""
|
| 65 |
+
This analyzer looks for:
|
| 66 |
+
- Facial inconsistencies
|
| 67 |
+
- Unnatural movements
|
| 68 |
+
- Lighting issues
|
| 69 |
+
- Texture anomalies
|
| 70 |
+
- Edge artifacts
|
| 71 |
+
- Blending problems
|
| 72 |
+
|
| 73 |
+
**Model**: Fine-tuned Llama 3.2 Vision
|
| 74 |
+
**Creator**: [Saakshi Gupta](https://huggingface.co/saakshigupta)
|
| 75 |
+
""")
|
|
|
|
| 76 |
|
| 77 |
# Function to fix cross-attention masks
|
| 78 |
+
def fix_cross_attention_mask(inputs):
|
|
|
|
| 79 |
if 'cross_attention_mask' in inputs and 0 in inputs['cross_attention_mask'].shape:
|
| 80 |
batch_size, seq_len, _, num_tiles = inputs['cross_attention_mask'].shape
|
| 81 |
+
visual_features = 6404 # Critical dimension
|
| 82 |
+
new_mask = torch.ones((batch_size, seq_len, visual_features, num_tiles),
|
| 83 |
+
device=inputs['cross_attention_mask'].device)
|
|
|
|
|
|
|
| 84 |
inputs['cross_attention_mask'] = new_mask
|
| 85 |
+
st.success("Fixed cross-attention mask dimensions")
|
| 86 |
+
return inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# Load model function
|
| 89 |
+
@st.cache_resource
|
| 90 |
+
def load_model():
|
| 91 |
+
with st.spinner("Loading model... This may take a few minutes. Please be patient..."):
|
| 92 |
try:
|
| 93 |
+
# Check for GPU
|
| 94 |
+
has_gpu = check_gpu()
|
| 95 |
+
|
| 96 |
+
# Load base model and tokenizer using Unsloth
|
| 97 |
+
base_model_id = "unsloth/llama-3.2-11b-vision-instruct"
|
| 98 |
+
model, tokenizer = FastVisionModel.from_pretrained(
|
| 99 |
+
base_model_id,
|
| 100 |
+
load_in_4bit=True,
|
| 101 |
+
)
|
|
|
|
| 102 |
|
| 103 |
+
# Load the adapter
|
| 104 |
+
adapter_id = "saakshigupta/deepfake-explainer-1"
|
| 105 |
+
model = PeftModel.from_pretrained(model, adapter_id)
|
| 106 |
|
| 107 |
+
# Set to inference mode
|
| 108 |
+
FastVisionModel.for_inference(model)
|
| 109 |
+
|
| 110 |
+
return model, tokenizer
|
| 111 |
+
except Exception as e:
|
| 112 |
+
st.error(f"Error loading model: {str(e)}")
|
| 113 |
+
return None, None
|
| 114 |
|
| 115 |
+
# Analyze image function
|
| 116 |
+
def analyze_image(image, question, model, tokenizer, temperature=0.7, max_tokens=500, custom_instruction=""):
|
| 117 |
+
# Combine question with custom instruction if provided
|
| 118 |
+
if custom_instruction.strip():
|
| 119 |
+
full_prompt = f"{question}\n\nAdditional instructions: {custom_instruction}"
|
| 120 |
+
else:
|
| 121 |
+
full_prompt = question
|
| 122 |
|
| 123 |
+
# Format the message
|
| 124 |
+
messages = [
|
| 125 |
+
{"role": "user", "content": [
|
| 126 |
+
{"type": "image"},
|
| 127 |
+
{"type": "text", "text": full_prompt}
|
| 128 |
+
]}
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
# Apply chat template
|
| 132 |
+
input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
|
| 133 |
+
|
| 134 |
+
# Process with image
|
| 135 |
+
inputs = tokenizer(
|
| 136 |
+
image,
|
| 137 |
+
input_text,
|
| 138 |
+
add_special_tokens=False,
|
| 139 |
+
return_tensors="pt",
|
| 140 |
+
).to(model.device)
|
| 141 |
+
|
| 142 |
+
# Fix cross-attention mask if needed
|
| 143 |
+
inputs = fix_cross_attention_mask(inputs)
|
| 144 |
+
|
| 145 |
+
# Generate response
|
| 146 |
+
with st.spinner("Analyzing image... (this may take 15-30 seconds)"):
|
| 147 |
+
with torch.no_grad():
|
| 148 |
+
output_ids = model.generate(
|
| 149 |
+
**inputs,
|
| 150 |
+
max_new_tokens=max_tokens,
|
| 151 |
+
use_cache=True,
|
| 152 |
+
temperature=temperature,
|
| 153 |
+
top_p=0.9
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# Decode the output
|
| 157 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 158 |
+
|
| 159 |
+
# Try to extract just the model's response (after the prompt)
|
| 160 |
+
if full_prompt in response:
|
| 161 |
+
result = response.split(full_prompt)[-1].strip()
|
| 162 |
else:
|
| 163 |
+
result = response
|
| 164 |
+
|
| 165 |
+
return result
|
| 166 |
+
|
| 167 |
+
# Main app
|
| 168 |
+
def main():
|
| 169 |
+
# Create a button to load the model
|
| 170 |
+
if 'model_loaded' not in st.session_state:
|
| 171 |
+
st.session_state.model_loaded = False
|
| 172 |
+
st.session_state.model = None
|
| 173 |
+
st.session_state.tokenizer = None
|
| 174 |
+
|
| 175 |
+
# Load model button
|
| 176 |
+
if not st.session_state.model_loaded:
|
| 177 |
+
if st.button("π₯ Load Deepfake Analysis Model", type="primary"):
|
| 178 |
+
model, tokenizer = load_model()
|
| 179 |
+
if model is not None and tokenizer is not None:
|
| 180 |
+
st.session_state.model = model
|
| 181 |
+
st.session_state.tokenizer = tokenizer
|
| 182 |
+
st.session_state.model_loaded = True
|
| 183 |
+
st.success("β
Model loaded successfully! You can now analyze images.")
|
| 184 |
+
else:
|
| 185 |
+
st.error("β Failed to load model. Please check the logs for errors.")
|
| 186 |
+
else:
|
| 187 |
+
st.success("β
Model loaded successfully! You can now analyze images.")
|
| 188 |
+
|
| 189 |
+
# Image upload section
|
| 190 |
+
st.subheader("Upload an Image")
|
| 191 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 192 |
+
|
| 193 |
+
# Default question with option to customize
|
| 194 |
+
default_question = "Analyze this image and tell me if it's a deepfake. Provide both technical and non-technical explanations."
|
| 195 |
+
question = st.text_area("Question/Prompt:", value=default_question, height=100)
|
| 196 |
+
|
| 197 |
+
if uploaded_file is not None:
|
| 198 |
+
# Display the uploaded image
|
| 199 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 200 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 201 |
+
|
| 202 |
+
# Analyze button - only enabled if model is loaded
|
| 203 |
+
if st.session_state.model_loaded:
|
| 204 |
+
if st.button("π Analyze Image", type="primary"):
|
| 205 |
+
result = analyze_image(
|
| 206 |
+
image,
|
| 207 |
+
question,
|
| 208 |
+
st.session_state.model,
|
| 209 |
+
st.session_state.tokenizer,
|
| 210 |
+
temperature=temperature,
|
| 211 |
+
max_tokens=max_tokens,
|
| 212 |
+
custom_instruction=custom_instruction
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Display results
|
| 216 |
+
st.success("β
Analysis complete!")
|
| 217 |
+
|
| 218 |
+
# Check if the result contains both technical and non-technical explanations
|
| 219 |
+
if "Technical" in result and "Non-Technical" in result:
|
| 220 |
+
# Split the result into technical and non-technical sections
|
| 221 |
+
parts = result.split("Non-Technical")
|
| 222 |
+
technical = parts[0]
|
| 223 |
+
non_technical = "Non-Technical" + parts[1]
|
| 224 |
|
| 225 |
+
# Display in two columns
|
| 226 |
+
col1, col2 = st.columns(2)
|
| 227 |
+
with col1:
|
| 228 |
+
st.subheader("Technical Analysis")
|
| 229 |
+
st.markdown(technical)
|
| 230 |
|
| 231 |
+
with col2:
|
| 232 |
+
st.subheader("Simple Explanation")
|
| 233 |
+
st.markdown(non_technical)
|
| 234 |
+
else:
|
| 235 |
+
# Just display the whole result
|
| 236 |
+
st.subheader("Analysis Result")
|
| 237 |
+
st.markdown(result)
|
| 238 |
+
else:
|
| 239 |
+
st.warning("β οΈ Please load the model first before analyzing images.")
|
| 240 |
+
|
| 241 |
+
# Footer
|
| 242 |
+
st.markdown("---")
|
| 243 |
+
st.caption("Deepfake Image Analyzer")
|
| 244 |
+
|
| 245 |
+
if __name__ == "__main__":
|
| 246 |
+
main()
|