Spaces:
Sleeping
Sleeping
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() |