-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
34 lines (23 loc) · 916 Bytes
/
application.py
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
from flask import Flask, request, jsonify
import pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
application = Flask(__name__)
def load_model():
with open('basic_classifier.pkl', 'rb') as fid:
loaded_model = pickle.load(fid)
with open('count_vectorizer.pkl', 'rb') as vd:
vectorizer = pickle.load(vd)
return loaded_model, vectorizer
loaded_model, vectorizer = load_model()
@application.route('/predict', methods=['POST'])
def predict():
data = request.json
news_text = data.get('text')
if not news_text:
return jsonify({'error': 'No text provided'}), 400
prediction = loaded_model.predict(vectorizer.transform([news_text]))[0]
result = 'Fake News' if prediction == 1 else 'Real News'
return jsonify({'prediction': result})
if __name__ == "__main__":
application.run()