File size: 1,325 Bytes
c000df1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import string
import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from fastapi import FastAPI, Request, Query
from pydantic import BaseModel

# Load the logistic regression model
model = joblib.load('models/logistic_regression_model.pkl')

# Load the TfidfVectorizer
vectorization = joblib.load('models/tfidf_vectorizer.pkl')

# Define the wordopt function
def wordopt(text):
    text = text.lower()
    text = re.sub('\[.*?\]', '', text)
    text = re.sub("\\W", " ", text)
    text = re.sub('https?://\S+|www\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)
    return text

# Define the prediction function
def predict_news(text):
    text = wordopt(text)
    text_vector = vectorization.transform([text])
    prediction = model.predict(text_vector)
    return "Fake" if prediction[0] == 1 else "Real"

# FastAPI app
app = FastAPI()

@app.get("/")
def home():
    return {"message": "Hello World"}

@app.get("/predict")
def predict(text: str):
    result = predict_news(text)
    return {"result": result}

# def predict(text: str = Query(..., description="Text to classify as fake or real news")):