Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,32 +2,52 @@ from transformers import ViTForImageClassification, ViTImageProcessor
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
processor = ViTImageProcessor.from_pretrained(model_name)
|
9 |
-
model = ViTForImageClassification.from_pretrained(model_name)
|
10 |
|
11 |
-
|
12 |
-
#
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
return "Real" if predicted_class_idx == 0 else "Fake (Possible Deepfake)"
|
23 |
|
24 |
-
# Create
|
25 |
iface = gr.Interface(
|
26 |
fn=detect_deepfake,
|
27 |
inputs=gr.Image(type="pil"),
|
28 |
outputs="text",
|
29 |
title="Deepfake Detection",
|
30 |
-
|
|
|
|
|
|
|
31 |
)
|
32 |
|
33 |
-
iface.launch()
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
+
import warnings
|
6 |
|
7 |
+
# Suppress warnings (optional)
|
8 |
+
warnings.filterwarnings('ignore')
|
|
|
|
|
9 |
|
10 |
+
try:
|
11 |
+
# Load model (smaller version for better performance)
|
12 |
+
model_name = "google/vit-base-patch16-224"
|
13 |
+
processor = ViTImageProcessor.from_pretrained(model_name)
|
14 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
15 |
|
16 |
+
print("Model loaded successfully!")
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error loading model: {e}")
|
19 |
+
raise
|
20 |
+
|
21 |
+
def detect_deepfake(image):
|
22 |
+
try:
|
23 |
+
# Convert image to RGB
|
24 |
+
if image.mode != 'RGB':
|
25 |
+
image = image.convert('RGB')
|
26 |
+
|
27 |
+
# Process image
|
28 |
+
inputs = processor(images=image, return_tensors="pt")
|
29 |
+
|
30 |
+
# Predict
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model(**inputs)
|
33 |
+
|
34 |
+
# Get result
|
35 |
+
predicted_class = outputs.logits.argmax(-1).item()
|
36 |
+
return "Real" if predicted_class == 0 else "Fake (Possible Deepfake)"
|
37 |
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error processing image: {str(e)}"
|
|
|
40 |
|
41 |
+
# Create interface
|
42 |
iface = gr.Interface(
|
43 |
fn=detect_deepfake,
|
44 |
inputs=gr.Image(type="pil"),
|
45 |
outputs="text",
|
46 |
title="Deepfake Detection",
|
47 |
+
examples=[
|
48 |
+
["real_example.jpg"], # Add your example files
|
49 |
+
["fake_example.jpg"]
|
50 |
+
]
|
51 |
)
|
52 |
|
53 |
+
iface.launch(server_port=7860, share=False) # Disable share for local use
|