News / app.py
jkg012's picture
Create app.py
2055022 verified
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()