Muzammil-Mj's picture
Create app.py
2cdc0e4 verified
import gradio as gr
from transformers import pipeline
import tensorflow as tf
import numpy as np
import cv2
from tensorflow.keras.preprocessing.image import img_to_array
# ---- Load AI Models ----
print("Loading AI models...")
fake_news_detector = pipeline("text-classification", model="jy46604790/Fake-News-Bert-Detect")
deepfake_model = tf.keras.models.load_model("your_deepfake_model_path") # Replace with actual model
# ---- Fake News Detection Function ----
def detect_news(text):
prediction = fake_news_detector(text)
label = prediction[0]['label']
confidence = round(prediction[0]['score'], 2)
return f"Prediction: {label} (Confidence: {confidence})"
# ---- Deepfake Image Detection Function ----
def detect_image(image):
img = image.resize((224, 224)) # Resize to match model input size
img = np.array(img) / 255.0 # Normalize
img = np.expand_dims(img, axis=0) # Expand dimensions
prediction = deepfake_model.predict(img)[0][0]
label = "FAKE" if prediction > 0.5 else "REAL"
confidence = round(prediction, 2)
return f"Prediction: {label} (Confidence: {confidence})"
# ---- Deepfake Video Detection Function ----
def detect_video(video_path):
cap = cv2.VideoCapture(video_path)
frame_scores = []
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if frame_count % 5 == 0: # Extract every 5th frame to speed up processing
frame = cv2.resize(frame, (224, 224)) / 255.0 # Resize and normalize
frame = np.expand_dims(frame, axis=0)
prediction = deepfake_model.predict(frame)[0][0]
frame_scores.append(prediction)
frame_count += 1
cap.release()
if not frame_scores:
return "Error: No valid frames detected."
avg_confidence = np.mean(frame_scores)
label = "FAKE" if avg_confidence > 0.5 else "REAL"
return f"Prediction: {label} (Confidence: {round(avg_confidence, 2)})"
# ---- Gradio UI ----
with gr.Blocks() as demo:
gr.Markdown("# πŸ“° Fake News & Deepfake Detection Tool")
# Fake News Section
with gr.Tab("πŸ“„ Fake News Detector"):
gr.Markdown("Enter a news article below to check if it’s **fake or real**.")
text_input = gr.Textbox(label="Enter News Article")
text_output = gr.Textbox(label="Prediction")
text_button = gr.Button("Analyze News")
text_button.click(detect_news, inputs=text_input, outputs=text_output)
# Deepfake Image Section
with gr.Tab("πŸ–ΌοΈ Deepfake Image Detector"):
gr.Markdown("Upload an image to check if it's **real or a deepfake**.")
image_input = gr.Image(type="pil")
image_output = gr.Textbox(label="Prediction")
image_button = gr.Button("Analyze Image")
image_button.click(detect_image, inputs=image_input, outputs=image_output)
# Deepfake Video Section
with gr.Tab("πŸŽ₯ Deepfake Video Detector"):
gr.Markdown("Upload a video to check if it contains **deepfake content**.")
video_input = gr.Video()
video_output = gr.Textbox(label="Prediction")
video_button = gr.Button("Analyze Video")
video_button.click(detect_video, inputs=video_input, outputs=video_output)
# Run Gradio App
demo.launch()