from fastapi import FastAPI from transformers import pipeline import re pipe = pipeline("text-classification", model="JungleLee/bert-toxic-comment-classification") app = FastAPI( title="Hopeline - AI Inference API", description="API for detecting toxic comments", version="0.1" ) def preprocess_text(text: str) -> str: # Remove special characters and extra whitespace text = re.sub(r'[^\w\s]', '', text) # Convert to lowercase text = text.lower() # Remove extra whitespaces text = ' '.join(text.split()) return text @app.get("/") async def welcome(): return "Welcome to Hopeline - AI Inference API" @app.post('/predict') async def predict_post(request_body: dict): text = request_body.get('text', '') if not text: return {"error": "No text provided"} # Preprocess text processed_text = preprocess_text(text) # Get prediction prediction = pipe(processed_text) return prediction