File size: 974 Bytes
30b338b
5fc7800
630f874
5fc7800
 
30b338b
630f874
 
 
 
 
 
 
 
 
 
 
 
 
 
30b338b
 
5fc7800
 
 
630f874
 
 
 
 
 
 
 
 
 
 
5fc7800
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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