Deploy your NLP model with FastAPI—discover a step-by-step guide to quickly launch your AI-powered web app now!

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Import FastAPI and the NLP pipeline from transformers
from fastapi import FastAPI, HTTPException
from transformers import pipeline
// Initialize your FastAPI app
app = FastAPI()
// Load NLP model globally (for example, a sentiment analysis pipeline)
nlp\_pipeline = pipeline("sentiment-analysis")
// Define an endpoint for NLP inference
@app.post("/predict")
async def predict(text: str):
// Validate input text
if not text:
raise HTTPException(status\_code=400, detail="Input text is required")
// Perform model inference
try:
result = nlp\_pipeline(text)
except Exception as e:
// Handle potential inference errors
raise HTTPException(status\_code=500, detail=f"Model inference failed: {str(e)}")
// Return model's prediction result
return {"result": result}
import asyncio
from concurrent.futures import ThreadPoolExecutor
// Create a ThreadPoolExecutor for potentially blocking tasks
executor = ThreadPoolExecutor(max\_workers=4)
// Wrap the model call to run it in the thread pool
def run\_inference(text: str):
return nlp\_pipeline(text)
@app.post("/async-predict")
async def async\_predict(text: str):
if not text:
raise HTTPException(status\_code=400, detail="Input text is required")
try:
// Run the blocking task in a separate thread
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(executor, run\_inference, text)
except Exception as e:
raise HTTPException(status\_code=500, detail=f"Inference error: {str(e)}")
return {"result": result}
def preprocess(text: str) -> str:
// Example pre-processing: trim whitespace and lower the text
return text.strip().lower()
def postprocess(result) -> dict:
// Example post-processing: format the output with clear keys
return {"label": result\[0]\["label"], "score": result\[0]\["score"]}
@app.post("/process-predict")
async def process\_predict(text: str):
if not text:
raise HTTPException(status\_code=400, detail="Input text is required")
try:
// Preprocess the text
preprocessed\_text = preprocess(text)
result = nlp_pipeline(preprocessed_text)
// Postprocess the result to a neat format
processed\_result = postprocess(result)
except Exception as e:
raise HTTPException(status\_code=500, detail=f"Processing error: {str(e)}")
return {"result": processed\_result}
import logging
// Set up logging configuration
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(**name**)
@app.post("/predict-logged")
async def predict\_logged(text: str):
if not text:
logger.error("No text provided")
raise HTTPException(status\_code=400, detail="Input text is required")
try:
logger.info("Starting inference")
result = nlp\_pipeline(text)
logger.info("Inference completed successfully")
except Exception as e:
logger.exception("Inference failed")
raise HTTPException(status\_code=500, detail=f"Inference error: {str(e)}")
return {"result": result}
// To run the server with uvicorn, execute the following command:
// Command in terminal might look like: uvicorn your_app_filename:app --host 0.0.0.0 --port 8000
// Example command line usage is not part of the code but essential for deployment.
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.