jkg012 commited on
Commit
1dbd4c1
·
verified ·
1 Parent(s): 2ceb168

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ from transformers import pipeline
4
+ from PIL import Image
5
+ import pytesseract
6
+ import base64
7
+ import io
8
+
9
+ app = Flask(__name__)
10
+ CORS(app)
11
+
12
+ # Load pretrained BERT model
13
+ classifier = pipeline("text-classification", model="jy46604790/Fake-News-Bert-Detect", tokenizer="jy46604790/Fake-News-Bert-Detect")
14
+
15
+ @app.route("/predict", methods=["POST"])
16
+ def predict_text():
17
+ data = request.json
18
+ text = data.get("text", "")
19
+ if not text.strip():
20
+ return jsonify({"error": "No text provided"}), 400
21
+
22
+ result = classifier(text)[0]
23
+ label = "Real" if result['label'] == 'LABEL_1' else "Fake"
24
+ return jsonify({"label": label, "confidence": round(result["score"] * 100, 2)})
25
+
26
+ @app.route("/predict-image", methods=["POST"])
27
+ def predict_image():
28
+ data = request.json
29
+ image_b64 = data.get("image")
30
+
31
+ if not image_b64:
32
+ return jsonify({"error": "No image provided"}), 400
33
+
34
+ try:
35
+ img_bytes = base64.b64decode(image_b64)
36
+ img = Image.open(io.BytesIO(img_bytes))
37
+ text = pytesseract.image_to_string(img)
38
+ except Exception as e:
39
+ return jsonify({"error": "Invalid image data"}), 400
40
+
41
+ if not text.strip():
42
+ return jsonify({"error": "No text found in image"}), 400
43
+
44
+ result = classifier(text)[0]
45
+ label = "Real" if result['label'] == 'LABEL_1' else "Fake"
46
+ return jsonify({"label": label, "confidence": round(result["score"] * 100, 2), "extracted_text": text})