from fastapi import FastAPI from type.request.predict import PredictRequest from type.response.predict import PredictResponse from hugging_face import model, dataset from transformer import transformer from preprocess import preprocess hate_speech_model = model.load_hugging_face_model('model_rf.pkl') hate_speech_dataset = dataset.load_dataset('data_clean.csv') tfidf = transformer.create_tfidf(hate_speech_dataset, 'Tweet', 'U') app = FastAPI() @app.get("/") def root(): return {"message": "All system running well :)"} @app.get("/healthz") def healthz(): return {"message": "All system running well :)"} @app.post("/predict") def predict(request: PredictRequest): preprocessed_text = preprocess.preprocess(request.predict_text) predict_text = [preprocessed_text] predict_text = tfidf.transform(predict_text) prediction = hate_speech_model.predict(predict_text) return PredictResponse( predict_text = request.predict_text, is_hate_speech = prediction[0] == 1 )