hopeline-ai / app.py
navindusa's picture
Enhance FastAPI application with text preprocessing and update Dockerfile for pip installation
630f874
raw
history blame contribute delete
974 Bytes
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