jkg012's picture
Create app.py
1dbd4c1 verified
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})