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 import sys 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): predict_text = [request.predict_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 )