Spaces:
Sleeping
Sleeping
File size: 4,273 Bytes
c18e3b6 1b6fdca c18e3b6 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import gradio as gr
from transformers import pipeline, CLIPProcessor, CLIPModel
from PIL import Image
import torch
import cv2
import os
import tempfile
# Load models
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
news_model = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news")
# AI Image Detection
def analyze_image(image):
inputs = clip_processor(text=["a real photo", "an AI-generated image"], images=image, return_tensors="pt", padding=True)
outputs = clip_model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1).tolist()[0]
prediction = "AI-generated" if probs[1] > probs[0] else "Real"
confidence = round(max(probs) * 100, 2)
explanation = (
"The image strongly suggests AI generation. Look for smooth textures, perfect symmetry, "
"and overly clean details — signs of AI creation."
if prediction == "AI-generated"
else "This image appears authentic based on texture, lighting, and organic imperfections. "
"No major AI traits were detected."
)
return f"Prediction: {prediction} ({confidence}%)", explanation
# Fake News Detection
def analyze_news(text):
result = news_model(text)[0]
label = result["label"]
score = round(result["score"] * 100, 2)
pred = "Fake News" if label == "LABEL_0" else "Real News"
explanation = (
"⚠️ This article likely contains misinformation or fabricated facts."
if label == "LABEL_0"
else "✅ The article seems factual and based on credible information."
)
return f"Prediction: {pred} ({score}%)", explanation
# Video Analysis
def analyze_video(video_file):
temp_dir = tempfile.mkdtemp()
video_path = os.path.join(temp_dir, "input.mp4")
with open(video_path, "wb") as f:
f.write(video_file.read())
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_rate = int(fps) if fps > 0 else 1
results = []
frame_count = 0
while cap.isOpened():
success, frame = cap.read()
if not success:
break
if frame_count % frame_rate == 0:
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
result, _ = analyze_image(pil_image)
results.append(result)
frame_count += 1
cap.release()
if any("AI-generated" in r for r in results):
return "Prediction: AI-generated content detected in video.", (
"The video includes frames that strongly resemble AI-generated imagery. "
"Check for smooth rendering, cartoonish features, or lack of natural flaws."
)
else:
return "Prediction: Video looks real.", (
"No strong signs of AI generation were detected in any frame. "
"Visuals appear natural and consistent with real-world content."
)
# Gradio App
with gr.Blocks() as demo:
gr.Markdown("## 🧠 Fake News & AI Media Detector (Image / News / Video)")
with gr.Row():
with gr.Column():
img_input = gr.Image(label="Upload Image")
img_btn = gr.Button("Analyze Image")
img_result = gr.Textbox(label="Image Result")
img_explanation = gr.Textbox(label="Image Explanation")
with gr.Column():
news_input = gr.Textbox(label="Paste News Headline or Text")
news_btn = gr.Button("Analyze News")
news_result = gr.Textbox(label="News Result")
news_explanation = gr.Textbox(label="News Explanation")
with gr.Row():
video_input = gr.File(label="Upload Short Video (≤ 10 sec, MP4)", file_types=[".mp4"])
video_btn = gr.Button("Analyze Video")
video_result = gr.Textbox(label="Video Result")
video_explanation = gr.Textbox(label="Video Explanation")
img_btn.click(analyze_image, inputs=img_input, outputs=[img_result, img_explanation])
news_btn.click(analyze_news, inputs=news_input, outputs=[news_result, news_explanation])
video_btn.click(analyze_video, inputs=video_input, outputs=[video_result, video_explanation])
demo.launch() |