Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load model and processor
|
| 8 |
+
model_name = "haywoodsloan/ai-image-detector-deploy"
|
| 9 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
def analyze_video(video):
|
| 14 |
+
cap = cv2.VideoCapture(video.name)
|
| 15 |
+
results = []
|
| 16 |
+
frame_num = 0
|
| 17 |
+
frame_interval = 10 # analyze every 10th frame
|
| 18 |
+
|
| 19 |
+
while cap.isOpened():
|
| 20 |
+
ret, frame = cap.read()
|
| 21 |
+
if not ret:
|
| 22 |
+
break
|
| 23 |
+
|
| 24 |
+
if frame_num % frame_interval == 0:
|
| 25 |
+
# Convert to PIL image
|
| 26 |
+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 27 |
+
pil_image = Image.fromarray(rgb)
|
| 28 |
+
|
| 29 |
+
# Run inference
|
| 30 |
+
inputs = processor(images=pil_image, return_tensors="pt")
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model(**inputs)
|
| 33 |
+
pred = outputs.logits.argmax(-1).item()
|
| 34 |
+
label = model.config.id2label[pred]
|
| 35 |
+
|
| 36 |
+
results.append(f"Frame {frame_num}: {label}")
|
| 37 |
+
|
| 38 |
+
frame_num += 1
|
| 39 |
+
|
| 40 |
+
cap.release()
|
| 41 |
+
return "\n".join(results)
|
| 42 |
+
|
| 43 |
+
# Gradio UI
|
| 44 |
+
gr.Interface(
|
| 45 |
+
fn=analyze_video,
|
| 46 |
+
inputs=gr.Video(label="Upload a video"),
|
| 47 |
+
outputs=gr.Textbox(label="Detection Results"),
|
| 48 |
+
title="AI Frame Detector",
|
| 49 |
+
description="Detects whether frames in a video are AI-generated or real."
|
| 50 |
+
).launch()
|