-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetoxify_api.py
More file actions
63 lines (47 loc) · 1.72 KB
/
detoxify_api.py
File metadata and controls
63 lines (47 loc) · 1.72 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI
from pydantic import BaseModel
import os
import sys
import importlib.util
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
# Global model variable
model = None
def get_or_install_package(package_name):
"""Check if a package is installed, and install it if not."""
if importlib.util.find_spec(package_name) is None:
logger.info(f"Installing {package_name}...")
# Ensure pip install goes to the site packages mount point to preserve across restarts
os.system(f"pip install {package_name} --target=/.cached_packages")
# Add the cached packages directory to the path
if "/.cached_packages" not in sys.path:
sys.path.append("/.cached_packages")
logger.info(f"{package_name} installed.")
else:
logger.info(f"{package_name} already installed.")
def get_model():
"""Lazy load the model only when needed."""
global model
if model is None:
# Check and install necessary packages
get_or_install_package("detoxify")
# Now we can import detoxify
from detoxify import Detoxify
logger.info("Loading detoxify model...")
model = Detoxify('original')
logger.info("Model loaded successfully.")
return model
class Input(BaseModel):
text: str
@app.post("/predict")
async def predict(input: Input):
# Get or load the model
model = get_model()
# Make prediction
raw_result = model.predict(input.text)
# Convert numpy values to Python floats
result = {key: float(value) for key, value in raw_result.items()}
return result