File size: 1,496 Bytes
1dbd4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
from flask import Flask, request, jsonify
from flask_cors import CORS
from transformers import pipeline
from PIL import Image
import pytesseract
import base64
import io

app = Flask(__name__)
CORS(app)

# Load pretrained BERT model
classifier = pipeline("text-classification", model="jy46604790/Fake-News-Bert-Detect", tokenizer="jy46604790/Fake-News-Bert-Detect")

@app.route("/predict", methods=["POST"])
def predict_text():
    data = request.json
    text = data.get("text", "")
    if not text.strip():
        return jsonify({"error": "No text provided"}), 400

    result = classifier(text)[0]
    label = "Real" if result['label'] == 'LABEL_1' else "Fake"
    return jsonify({"label": label, "confidence": round(result["score"] * 100, 2)})

@app.route("/predict-image", methods=["POST"])
def predict_image():
    data = request.json
    image_b64 = data.get("image")

    if not image_b64:
        return jsonify({"error": "No image provided"}), 400

    try:
        img_bytes = base64.b64decode(image_b64)
        img = Image.open(io.BytesIO(img_bytes))
        text = pytesseract.image_to_string(img)
    except Exception as e:
        return jsonify({"error": "Invalid image data"}), 400

    if not text.strip():
        return jsonify({"error": "No text found in image"}), 400

    result = classifier(text)[0]
    label = "Real" if result['label'] == 'LABEL_1' else "Fake"
    return jsonify({"label": label, "confidence": round(result["score"] * 100, 2), "extracted_text": text})