Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconNLP with Transformers: Advanced Techniques and Multimodal Applications
NLP with Transformers: Advanced Techniques and Multimodal Applications

Project 3: Sentiment Analysis API with Fine-Tuned Transformer

Step 4: Build the FastAPI Application

Develop a FastAPI-based API to serve the fine-tuned model. FastAPI is a modern, fast web framework for building APIs with Python that offers several advantages for our sentiment analysis service:

  • Automatic API documentation using OpenAPI (Swagger) and ReDoc
  • Built-in request validation and type checking
  • High performance with async support
  • Easy integration with machine learning models

Our API will handle incoming requests containing text data, process it through our fine-tuned sentiment analysis model, and return structured responses with sentiment predictions and confidence scores.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline

# Initialize FastAPI
app = FastAPI()

# Load fine-tuned model and pipeline
sentiment_pipeline = pipeline("sentiment-analysis", model="./results")

# Define request model
class SentimentRequest(BaseModel):
    text: str

# Define API endpoint
@app.post("/predict")
def predict_sentiment(request: SentimentRequest):
    try:
        prediction = sentiment_pipeline(request.text)
        return {"text": request.text, "sentiment": prediction[0]["label"], "confidence": prediction[0]["score"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Let's break down this code:

1. Imports and Setup

  • The code imports FastAPI for creating the API, HTTPException for error handling, BaseModel from pydantic for request validation, and the transformers pipeline for sentiment analysis

2. API Initialization

  • Creates a new FastAPI application instance
  • Initializes the sentiment analysis pipeline using the fine-tuned model from the "./results" directory

3. Request Model Definition

  • Defines a SentimentRequest class using Pydantic's BaseModel
  • Specifies that requests must contain a 'text' field as a string

4. API Endpoint

  • Creates a POST endpoint at "/predict"
  • Takes a SentimentRequest object as input
  • Returns three pieces of information:
  • The original input text
  • The predicted sentiment label
  • A confidence score for the prediction

5. Error Handling

  • Implements try-catch error handling
  • Returns a 500 status code with error details if something goes wrong

This API can be tested locally using the uvicorn server, and you can send test requests using tools like curl or Postman.

Step 4: Build the FastAPI Application

Develop a FastAPI-based API to serve the fine-tuned model. FastAPI is a modern, fast web framework for building APIs with Python that offers several advantages for our sentiment analysis service:

  • Automatic API documentation using OpenAPI (Swagger) and ReDoc
  • Built-in request validation and type checking
  • High performance with async support
  • Easy integration with machine learning models

Our API will handle incoming requests containing text data, process it through our fine-tuned sentiment analysis model, and return structured responses with sentiment predictions and confidence scores.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline

# Initialize FastAPI
app = FastAPI()

# Load fine-tuned model and pipeline
sentiment_pipeline = pipeline("sentiment-analysis", model="./results")

# Define request model
class SentimentRequest(BaseModel):
    text: str

# Define API endpoint
@app.post("/predict")
def predict_sentiment(request: SentimentRequest):
    try:
        prediction = sentiment_pipeline(request.text)
        return {"text": request.text, "sentiment": prediction[0]["label"], "confidence": prediction[0]["score"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Let's break down this code:

1. Imports and Setup

  • The code imports FastAPI for creating the API, HTTPException for error handling, BaseModel from pydantic for request validation, and the transformers pipeline for sentiment analysis

2. API Initialization

  • Creates a new FastAPI application instance
  • Initializes the sentiment analysis pipeline using the fine-tuned model from the "./results" directory

3. Request Model Definition

  • Defines a SentimentRequest class using Pydantic's BaseModel
  • Specifies that requests must contain a 'text' field as a string

4. API Endpoint

  • Creates a POST endpoint at "/predict"
  • Takes a SentimentRequest object as input
  • Returns three pieces of information:
  • The original input text
  • The predicted sentiment label
  • A confidence score for the prediction

5. Error Handling

  • Implements try-catch error handling
  • Returns a 500 status code with error details if something goes wrong

This API can be tested locally using the uvicorn server, and you can send test requests using tools like curl or Postman.

Step 4: Build the FastAPI Application

Develop a FastAPI-based API to serve the fine-tuned model. FastAPI is a modern, fast web framework for building APIs with Python that offers several advantages for our sentiment analysis service:

  • Automatic API documentation using OpenAPI (Swagger) and ReDoc
  • Built-in request validation and type checking
  • High performance with async support
  • Easy integration with machine learning models

Our API will handle incoming requests containing text data, process it through our fine-tuned sentiment analysis model, and return structured responses with sentiment predictions and confidence scores.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline

# Initialize FastAPI
app = FastAPI()

# Load fine-tuned model and pipeline
sentiment_pipeline = pipeline("sentiment-analysis", model="./results")

# Define request model
class SentimentRequest(BaseModel):
    text: str

# Define API endpoint
@app.post("/predict")
def predict_sentiment(request: SentimentRequest):
    try:
        prediction = sentiment_pipeline(request.text)
        return {"text": request.text, "sentiment": prediction[0]["label"], "confidence": prediction[0]["score"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Let's break down this code:

1. Imports and Setup

  • The code imports FastAPI for creating the API, HTTPException for error handling, BaseModel from pydantic for request validation, and the transformers pipeline for sentiment analysis

2. API Initialization

  • Creates a new FastAPI application instance
  • Initializes the sentiment analysis pipeline using the fine-tuned model from the "./results" directory

3. Request Model Definition

  • Defines a SentimentRequest class using Pydantic's BaseModel
  • Specifies that requests must contain a 'text' field as a string

4. API Endpoint

  • Creates a POST endpoint at "/predict"
  • Takes a SentimentRequest object as input
  • Returns three pieces of information:
  • The original input text
  • The predicted sentiment label
  • A confidence score for the prediction

5. Error Handling

  • Implements try-catch error handling
  • Returns a 500 status code with error details if something goes wrong

This API can be tested locally using the uvicorn server, and you can send test requests using tools like curl or Postman.

Step 4: Build the FastAPI Application

Develop a FastAPI-based API to serve the fine-tuned model. FastAPI is a modern, fast web framework for building APIs with Python that offers several advantages for our sentiment analysis service:

  • Automatic API documentation using OpenAPI (Swagger) and ReDoc
  • Built-in request validation and type checking
  • High performance with async support
  • Easy integration with machine learning models

Our API will handle incoming requests containing text data, process it through our fine-tuned sentiment analysis model, and return structured responses with sentiment predictions and confidence scores.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline

# Initialize FastAPI
app = FastAPI()

# Load fine-tuned model and pipeline
sentiment_pipeline = pipeline("sentiment-analysis", model="./results")

# Define request model
class SentimentRequest(BaseModel):
    text: str

# Define API endpoint
@app.post("/predict")
def predict_sentiment(request: SentimentRequest):
    try:
        prediction = sentiment_pipeline(request.text)
        return {"text": request.text, "sentiment": prediction[0]["label"], "confidence": prediction[0]["score"]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Let's break down this code:

1. Imports and Setup

  • The code imports FastAPI for creating the API, HTTPException for error handling, BaseModel from pydantic for request validation, and the transformers pipeline for sentiment analysis

2. API Initialization

  • Creates a new FastAPI application instance
  • Initializes the sentiment analysis pipeline using the fine-tuned model from the "./results" directory

3. Request Model Definition

  • Defines a SentimentRequest class using Pydantic's BaseModel
  • Specifies that requests must contain a 'text' field as a string

4. API Endpoint

  • Creates a POST endpoint at "/predict"
  • Takes a SentimentRequest object as input
  • Returns three pieces of information:
  • The original input text
  • The predicted sentiment label
  • A confidence score for the prediction

5. Error Handling

  • Implements try-catch error handling
  • Returns a 500 status code with error details if something goes wrong

This API can be tested locally using the uvicorn server, and you can send test requests using tools like curl or Postman.