|
import gradio as gr |
|
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification |
|
from PIL import Image |
|
import pytesseract |
|
|
|
|
|
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) |
|
|
|
|
|
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)" |
|
|
|
|
|
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}" |
|
|
|
|
|
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)") |
|
|
|
|
|
app = gr.TabbedInterface([text_input, image_input], ["Text Input", "Image Upload"]) |
|
|
|
if __name__ == "__main__": |
|
app.launch() |
|
|