import gradio as gr from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification from PIL import Image import pytesseract # Load model and tokenizer MODEL_NAME = "jy46604790/Fake-News-Bert-Detect" tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) classifier = pipeline("text-classification", model=model, tokenizer=tokenizer) # Text-only prediction def predict_text(text): if not text.strip(): return "āŒ Please enter some text." result = classifier(text)[0] label = "🟢 Real News" if result["label"] == "LABEL_1" else "šŸ”“ Fake News" confidence = round(result["score"] * 100, 2) return f"{label} ({confidence}% confidence)" # Image-to-text + prediction def predict_image(image): if image is None: return "āŒ Please upload an image." text = pytesseract.image_to_string(image) if not text.strip(): return "āš ļø No text found in image." result = classifier(text)[0] label = "🟢 Real News" if result["label"] == "LABEL_1" else "šŸ”“ Fake News" confidence = round(result["score"] * 100, 2) return f"{label} ({confidence}% confidence)\n\nšŸ“œ Extracted text:\n{text}" # Gradio Interface text_input = gr.Interface(fn=predict_text, inputs=gr.Textbox(lines=4, label="Enter News Text"), outputs="text", title="šŸ“° Fake News Detection (Text)") image_input = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text", title="šŸ–¼ļø Fake News Detection (Image)") # Combine in Tabs app = gr.TabbedInterface([text_input, image_input], ["Text Input", "Image Upload"]) if __name__ == "__main__": app.launch()