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")):