-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (44 loc) · 1.83 KB
/
Copy pathapp.py
File metadata and controls
59 lines (44 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
import gradio as gr
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
from load_model import load_model
# Load the trained model
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
MODEL = load_model(DEVICE)
MODEL.eval()
# Define image preprocessing (same as training)
inference_transform = transforms.Compose([
transforms.Resize(232),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
def predict(image):
"""Runs inference on an image and returns the prediction."""
image = Image.open(image).convert("RGB")
image = inference_transform(image).unsqueeze(0).to(DEVICE)
with torch.no_grad():
output = MODEL(image).squeeze() # Ensure correct dimensions
print("Raw model output:", output)
probability = torch.sigmoid(output).cpu().numpy().item()
print("Sigmoid probability:", probability)
result = "The image is **AI-Generated** 🖥️" if probability > 0.5 else "The image is **Human-Generated** 🎨"
return result
# Define the Gradio interface
demo = gr.Blocks()
with demo:
gr.Markdown("# 🔍 AI vs Human Image Detector")
gr.Markdown("Upload an image to check if it's **AI-generated** or **human-made**.")
with gr.Row():
image_input = gr.Image(type="filepath")
with gr.Row():
output_text = gr.Markdown("")
submit_button = gr.Button("Analyze Image")
submit_button.click(predict, inputs=image_input, outputs=output_text)
# Footer Section
gr.Markdown("<hr>") # Adds a line separator
gr.Markdown("<p style='text-align: center; font-size: 14px;'>🌟 Developed by <b>Sheema Masood</b> | Built with <b>Gradio</b> 🌟</p>")
# Run the Gradio app
demo.launch()