How to beginner · 3 min read

How to deploy text classifier as API

Quick answer
Use a Python web framework like FastAPI to wrap your text classification logic powered by an AI model such as gpt-4o from the openai SDK. Create an endpoint that accepts text input, calls the AI model for classification, and returns the result as JSON.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0 fastapi uvicorn

Setup

Install the required Python packages and set your OpenAI API key as an environment variable.

  • Install packages: openai for AI calls, fastapi for API server, and uvicorn as ASGI server.
  • Set environment variable OPENAI_API_KEY with your API key.
bash
pip install openai fastapi uvicorn
output
Collecting openai
Collecting fastapi
Collecting uvicorn
Successfully installed openai fastapi uvicorn

Step by step

Create a FastAPI app with a POST endpoint /classify that accepts JSON input with text, sends it to the gpt-4o model for classification, and returns the predicted label.

python
import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import OpenAI

app = FastAPI()
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

class TextInput(BaseModel):
    text: str

@app.post("/classify")
async def classify_text(input: TextInput):
    prompt = f"Classify the sentiment of this text as Positive, Negative, or Neutral:\n\nText: {input.text}\nSentiment:"  
    try:
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=10,
            temperature=0
        )
        label = response.choices[0].message.content.strip()
        return {"label": label}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# To run: uvicorn filename:app --reload
output
INFO:     Started server process [12345]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

# Example request:
# curl -X POST "http://127.0.0.1:8000/classify" -H "Content-Type: application/json" -d '{"text": "I love this product!"}'
# Response: {"label": "Positive"}

Common variations

  • Use async calls with await if preferred.
  • Change model to gpt-4o-mini for faster, cheaper classification.
  • Implement streaming responses for real-time feedback.
  • Use other frameworks like Flask or FastAPI with background tasks.
python
import os
from fastapi import FastAPI
from pydantic import BaseModel
from openai import OpenAI

app = FastAPI()
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

class TextInput(BaseModel):
    text: str

@app.post("/classify-async")
async def classify_text_async(input: TextInput):
    prompt = f"Classify the sentiment of this text as Positive, Negative, or Neutral:\n\nText: {input.text}\nSentiment:"  
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=10,
        temperature=0
    )
    label = response.choices[0].message.content.strip()
    return {"label": label}
output
INFO:     Started server process [12346]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

# Example request:
# curl -X POST "http://127.0.0.1:8000/classify-async" -H "Content-Type: application/json" -d '{"text": "I love this product!"}'
# Response: {"label": "Positive"}

Troubleshooting

  • If you get 401 Unauthorized, verify your OPENAI_API_KEY environment variable is set correctly.
  • For Timeout errors, increase request timeout or check network connectivity.
  • If classification results are inconsistent, try lowering temperature to 0 for deterministic output.

Key Takeaways

  • Use FastAPI to quickly deploy AI text classifiers as REST APIs.
  • Call the gpt-4o model via the openai SDK for reliable classification.
  • Set temperature=0 for consistent classification labels.
  • Async API endpoints improve scalability for high request volumes.
  • Always handle exceptions and validate API keys to avoid runtime errors.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗