import gradio as gr from transformers import pipeline, AutoImageProcessor, SiglipForImageClassification import cv2, tempfile, os from PIL import Image # Load models img_model = SiglipForImageClassification.from_pretrained("prithivMLmods/open-deepfake-detection") img_proc = AutoImageProcessor.from_pretrained("prithivMLmods/open-deepfake-detection") news_model = pipeline("text-classification", model="jy46604790/Fake-News-Bert-Detect") # Image analysis def analyze_image(image): inputs = img_proc(images=image, return_tensors="pt") outputs = img_model(**inputs) probs = outputs.logits.softmax(dim=1).tolist()[0] pred = "Fake (AI-generated)" if probs[0] > probs[1] else "Real" conf = round(max(probs)*100,2) expl = ( "⚠️ Likely AI-generated due to visual artifacts or unnatural features." if pred.startswith("Fake") else "✅ Appears authentic with realistic textures and lighting." ) return f"{pred} ({conf}%)", expl # News analysis def analyze_news(text): res = news_model(text)[0] label = res['label'] score = round(res['score']*100,2) pred = "Fake News" if label=="LABEL_0" else "Real News" expl = ( "⚠️ This article likely contains misinformation or misleading claims." if pred=="Fake News" else "✅ The text appears credible and factually consistent." ) return f"{pred} ({score}%)", expl # Video analysis (≤10s) def analyze_video(file): tmp = tempfile.mkdtemp() path = os.path.join(tmp, "vid.mp4") with open(path,"wb") as f: f.write(file.read()) cap = cv2.VideoCapture(path) fps = cap.get(cv2.CAP_PROP_FPS) or 1 frame_idx, ai_detected = 0, False while True: ret, frame = cap.read() if not ret or frame_idx/fps>10: break if frame_idx % int(fps)==0: img = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)) pred, _ = analyze_image(img) if "Fake" in pred: ai_detected = True break frame_idx += 1 cap.release() pred = "Video likely contains AI-generated content" if ai_detected else "Video appears authentic" expl = ( "⚠️ At least one frame shows AI generation traits." if ai_detected else "✅ No strong signs of AI generation across frames." ) return pred, expl with gr.Blocks() as demo: gr.Markdown("## 🤖 Real-Time AI & Fake News Validator") with gr.Row(): with gr.Column(): img = gr.Image(type="pil") btn_img = gr.Button("Analyze Image") out_img, expl_img = gr.Textbox(), gr.Textbox() btn_img.click(analyze_image, inputs=img, outputs=[out_img, expl_img]) with gr.Column(): txt = gr.Textbox(lines=4, placeholder="Paste news or claim text") btn_txt = gr.Button("Analyze News") out_txt, expl_txt = gr.Textbox(), gr.Textbox() btn_txt.click(analyze_news, inputs=txt, outputs=[out_txt, expl_txt]) with gr.Row(): vid = gr.File(file_types=[".mp4"]) btn_vid = gr.Button("Analyze Video") out_vid, expl_vid = gr.Textbox(), gr.Textbox() btn_vid.click(analyze_video, inputs=vid, outputs=[out_vid, expl_vid]) demo.launch()