Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load the pre-trained AI image detector model
|
| 7 |
+
detector = pipeline(
|
| 8 |
+
"image-classification",
|
| 9 |
+
model="umm-maybe/AI-image-detector",
|
| 10 |
+
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def detect_image(image):
|
| 14 |
+
if image is None:
|
| 15 |
+
return "Please upload an image."
|
| 16 |
+
|
| 17 |
+
# Run inference
|
| 18 |
+
results = detector(image)
|
| 19 |
+
|
| 20 |
+
# Get top prediction
|
| 21 |
+
top_pred = results[0]
|
| 22 |
+
label = top_pred['label']
|
| 23 |
+
prob = top_pred['score'] * 100
|
| 24 |
+
|
| 25 |
+
# Format output
|
| 26 |
+
if label == 'real':
|
| 27 |
+
return f"Real Image (Confidence: {prob:.2f}%)"
|
| 28 |
+
else:
|
| 29 |
+
return f"AI-Generated Image (Confidence: {prob:.2f}%)"
|
| 30 |
+
|
| 31 |
+
# Create Gradio interface
|
| 32 |
+
with gr.Blocks(title="AI Image Detector – CBSE Board Project 2025-26") as demo:
|
| 33 |
+
gr.Markdown("# AI Image Detector by [Dipika Singh, Rashvi Singh, Rupanjali Rai, Khusboo Yadav] – CBSE Board Project 2025-26")
|
| 34 |
+
gr.Markdown("Upload any image → Instantly know if it's Real or AI-Generated!")
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
image_input = gr.Image(type="pil", label="Drop Image Here - or - Click to Upload")
|
| 38 |
+
output = gr.Textbox(label="Detection Result")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
submit_btn = gr.Button("Submit")
|
| 42 |
+
clear_btn = gr.Button("Clear")
|
| 43 |
+
|
| 44 |
+
# Event handlers
|
| 45 |
+
submit_btn.click(fn=detect_image, inputs=image_input, outputs=output)
|
| 46 |
+
clear_btn.click(fn=lambda: (None, "Upload an image to start!"), inputs=None, outputs=[image_input, output])
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|