File size: 1,702 Bytes
2055022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()