-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
35 lines (27 loc) · 1001 Bytes
/
predict.py
File metadata and controls
35 lines (27 loc) · 1001 Bytes
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
import sys
import os
import joblib
def predict_text(text, model_path='models/fake_news_model.pkl', vect_path='models/vectorizer.pkl'):
if not os.path.exists(model_path) or not os.path.exists(vect_path):
raise FileNotFoundError('Model or vectorizer not found in models/. Run training first.')
model = joblib.load(model_path)
vectorizer = joblib.load(vect_path)
vec = vectorizer.transform([text])
pred = model.predict(vec)[0]
proba = None
if hasattr(model, 'predict_proba'):
proba = model.predict_proba(vec)[0].max()
label = 'REAL' if pred == 1 else 'FAKE'
return label, proba
def main():
if len(sys.argv) < 2:
print('Usage: python predict.py "Some news text to classify"')
sys.exit(1)
text = sys.argv[1]
label, proba = predict_text(text)
if proba is not None:
print(f"Prediction: {label} (confidence: {proba:.3f})")
else:
print(f"Prediction: {label}")
if __name__ == '__main__':
main()