Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconNLP con Transformers, técnicas avanzadas y aplicaciones multimodales
NLP con Transformers, técnicas avanzadas y aplicaciones multimodales

Project 4: Named Entity Recognition (NER) Pipeline with Custom Fine-Tuning

Step 6: Deploy the NER Pipeline as an API

Build an API using FastAPI to serve the NER pipeline. FastAPI is a modern, fast web framework for building APIs with Python that offers automatic API documentation, request validation, and excellent performance.

By wrapping our NER pipeline in an API, we can make it accessible over HTTP, allowing other applications and services to easily integrate with our named entity recognition system. The API will accept text input through HTTP POST requests and return the identified entities in a structured JSON format.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

# Initialize FastAPI
app = FastAPI()

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

# Define API endpoint
@app.post("/ner")
def predict_entities(request: NERRequest):
    try:
        entities = ner_pipeline(request.text)
        return {"text": request.text, "entities": entities}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Here's a detailed breakdown:

1. Imports and Setup

  • Imports FastAPI and HTTPException for creating the web service and handling errors
  • Imports BaseModel from pydantic for request data validation
  • Initializes a new FastAPI application instance

2. Request Model Definition

  • Creates a NERRequest class that inherits from BaseModel
  • Defines a single required field 'text' of type string that will contain the input text for entity recognition

3. API Endpoint

  • Creates a POST endpoint at "/ner"
  • Takes a NERRequest object as input
  • Processes the text using the previously defined ner_pipeline
  • Returns a JSON response containing:
    • The original input text
    • The identified entities
  • Includes error handling that returns a 500 status code if processing fails

When deployed, this API can be accessed by other applications to perform NER tasks through HTTP requests, making it easy to integrate the NER functionality into other systems and services.

Run the API locally:

uvicorn app:app --reload

Test the API:

curl -X POST "http://127.0.0.1:8000/ner" \
-H "Content-Type: application/json" \
-d '{"text": "Barack Obama was born in Hawaii."}'

Let's break it down:

Command Structure:

  • The command uses cURL to send a POST request to a local API endpoint (http://127.0.0.1:8000/ner)
  • It includes a Content-Type header specifying that the request body is JSON
  • The request body contains a JSON object with a "text" field containing the sample text "Barack Obama was born in Hawaii."

Expected Response:

The API will return a JSON response containing:

  • The original input text
  • An "entities" array listing each identified entity with:
  • The detected word or phrase
  • The entity type (e.g., "PER" for person, "LOC" for location)
  • A confidence score indicating the model's certainty

This endpoint is part of a FastAPI service that processes text through a NER pipeline to identify and classify named entities in the input text.

Response:

{
  "text": "Barack Obama was born in Hawaii.",
  "entities": [
    {"word": "Barack Obama", "entity_group": "PER", "score": 0.98},
    {"word": "Hawaii", "entity_group": "LOC", "score": 0.95}
  ]
}

This code shows the JSON response from a Named Entity Recognition (NER) API endpoint. Let's break it down:

Structure:

  • The response contains two main fields:
    • "text": Shows the original input text that was analyzed ("Barack Obama was born in Hawaii.")
    • "entities": An array containing the identified named entities

Identified Entities:

  • Two entities were detected:
    • 1. "Barack Obama" - classified as "PER" (Person) with 98% confidence
    • 2. "Hawaii" - classified as "LOC" (Location) with 95% confidence

This is the output format you can expect when making requests to the NER API endpoint, which processes text through a NER pipeline to identify and classify named entities.

Step 6: Deploy the NER Pipeline as an API

Build an API using FastAPI to serve the NER pipeline. FastAPI is a modern, fast web framework for building APIs with Python that offers automatic API documentation, request validation, and excellent performance.

By wrapping our NER pipeline in an API, we can make it accessible over HTTP, allowing other applications and services to easily integrate with our named entity recognition system. The API will accept text input through HTTP POST requests and return the identified entities in a structured JSON format.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

# Initialize FastAPI
app = FastAPI()

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

# Define API endpoint
@app.post("/ner")
def predict_entities(request: NERRequest):
    try:
        entities = ner_pipeline(request.text)
        return {"text": request.text, "entities": entities}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Here's a detailed breakdown:

1. Imports and Setup

  • Imports FastAPI and HTTPException for creating the web service and handling errors
  • Imports BaseModel from pydantic for request data validation
  • Initializes a new FastAPI application instance

2. Request Model Definition

  • Creates a NERRequest class that inherits from BaseModel
  • Defines a single required field 'text' of type string that will contain the input text for entity recognition

3. API Endpoint

  • Creates a POST endpoint at "/ner"
  • Takes a NERRequest object as input
  • Processes the text using the previously defined ner_pipeline
  • Returns a JSON response containing:
    • The original input text
    • The identified entities
  • Includes error handling that returns a 500 status code if processing fails

When deployed, this API can be accessed by other applications to perform NER tasks through HTTP requests, making it easy to integrate the NER functionality into other systems and services.

Run the API locally:

uvicorn app:app --reload

Test the API:

curl -X POST "http://127.0.0.1:8000/ner" \
-H "Content-Type: application/json" \
-d '{"text": "Barack Obama was born in Hawaii."}'

Let's break it down:

Command Structure:

  • The command uses cURL to send a POST request to a local API endpoint (http://127.0.0.1:8000/ner)
  • It includes a Content-Type header specifying that the request body is JSON
  • The request body contains a JSON object with a "text" field containing the sample text "Barack Obama was born in Hawaii."

Expected Response:

The API will return a JSON response containing:

  • The original input text
  • An "entities" array listing each identified entity with:
  • The detected word or phrase
  • The entity type (e.g., "PER" for person, "LOC" for location)
  • A confidence score indicating the model's certainty

This endpoint is part of a FastAPI service that processes text through a NER pipeline to identify and classify named entities in the input text.

Response:

{
  "text": "Barack Obama was born in Hawaii.",
  "entities": [
    {"word": "Barack Obama", "entity_group": "PER", "score": 0.98},
    {"word": "Hawaii", "entity_group": "LOC", "score": 0.95}
  ]
}

This code shows the JSON response from a Named Entity Recognition (NER) API endpoint. Let's break it down:

Structure:

  • The response contains two main fields:
    • "text": Shows the original input text that was analyzed ("Barack Obama was born in Hawaii.")
    • "entities": An array containing the identified named entities

Identified Entities:

  • Two entities were detected:
    • 1. "Barack Obama" - classified as "PER" (Person) with 98% confidence
    • 2. "Hawaii" - classified as "LOC" (Location) with 95% confidence

This is the output format you can expect when making requests to the NER API endpoint, which processes text through a NER pipeline to identify and classify named entities.

Step 6: Deploy the NER Pipeline as an API

Build an API using FastAPI to serve the NER pipeline. FastAPI is a modern, fast web framework for building APIs with Python that offers automatic API documentation, request validation, and excellent performance.

By wrapping our NER pipeline in an API, we can make it accessible over HTTP, allowing other applications and services to easily integrate with our named entity recognition system. The API will accept text input through HTTP POST requests and return the identified entities in a structured JSON format.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

# Initialize FastAPI
app = FastAPI()

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

# Define API endpoint
@app.post("/ner")
def predict_entities(request: NERRequest):
    try:
        entities = ner_pipeline(request.text)
        return {"text": request.text, "entities": entities}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Here's a detailed breakdown:

1. Imports and Setup

  • Imports FastAPI and HTTPException for creating the web service and handling errors
  • Imports BaseModel from pydantic for request data validation
  • Initializes a new FastAPI application instance

2. Request Model Definition

  • Creates a NERRequest class that inherits from BaseModel
  • Defines a single required field 'text' of type string that will contain the input text for entity recognition

3. API Endpoint

  • Creates a POST endpoint at "/ner"
  • Takes a NERRequest object as input
  • Processes the text using the previously defined ner_pipeline
  • Returns a JSON response containing:
    • The original input text
    • The identified entities
  • Includes error handling that returns a 500 status code if processing fails

When deployed, this API can be accessed by other applications to perform NER tasks through HTTP requests, making it easy to integrate the NER functionality into other systems and services.

Run the API locally:

uvicorn app:app --reload

Test the API:

curl -X POST "http://127.0.0.1:8000/ner" \
-H "Content-Type: application/json" \
-d '{"text": "Barack Obama was born in Hawaii."}'

Let's break it down:

Command Structure:

  • The command uses cURL to send a POST request to a local API endpoint (http://127.0.0.1:8000/ner)
  • It includes a Content-Type header specifying that the request body is JSON
  • The request body contains a JSON object with a "text" field containing the sample text "Barack Obama was born in Hawaii."

Expected Response:

The API will return a JSON response containing:

  • The original input text
  • An "entities" array listing each identified entity with:
  • The detected word or phrase
  • The entity type (e.g., "PER" for person, "LOC" for location)
  • A confidence score indicating the model's certainty

This endpoint is part of a FastAPI service that processes text through a NER pipeline to identify and classify named entities in the input text.

Response:

{
  "text": "Barack Obama was born in Hawaii.",
  "entities": [
    {"word": "Barack Obama", "entity_group": "PER", "score": 0.98},
    {"word": "Hawaii", "entity_group": "LOC", "score": 0.95}
  ]
}

This code shows the JSON response from a Named Entity Recognition (NER) API endpoint. Let's break it down:

Structure:

  • The response contains two main fields:
    • "text": Shows the original input text that was analyzed ("Barack Obama was born in Hawaii.")
    • "entities": An array containing the identified named entities

Identified Entities:

  • Two entities were detected:
    • 1. "Barack Obama" - classified as "PER" (Person) with 98% confidence
    • 2. "Hawaii" - classified as "LOC" (Location) with 95% confidence

This is the output format you can expect when making requests to the NER API endpoint, which processes text through a NER pipeline to identify and classify named entities.

Step 6: Deploy the NER Pipeline as an API

Build an API using FastAPI to serve the NER pipeline. FastAPI is a modern, fast web framework for building APIs with Python that offers automatic API documentation, request validation, and excellent performance.

By wrapping our NER pipeline in an API, we can make it accessible over HTTP, allowing other applications and services to easily integrate with our named entity recognition system. The API will accept text input through HTTP POST requests and return the identified entities in a structured JSON format.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

# Initialize FastAPI
app = FastAPI()

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

# Define API endpoint
@app.post("/ner")
def predict_entities(request: NERRequest):
    try:
        entities = ner_pipeline(request.text)
        return {"text": request.text, "entities": entities}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Here's a detailed breakdown:

1. Imports and Setup

  • Imports FastAPI and HTTPException for creating the web service and handling errors
  • Imports BaseModel from pydantic for request data validation
  • Initializes a new FastAPI application instance

2. Request Model Definition

  • Creates a NERRequest class that inherits from BaseModel
  • Defines a single required field 'text' of type string that will contain the input text for entity recognition

3. API Endpoint

  • Creates a POST endpoint at "/ner"
  • Takes a NERRequest object as input
  • Processes the text using the previously defined ner_pipeline
  • Returns a JSON response containing:
    • The original input text
    • The identified entities
  • Includes error handling that returns a 500 status code if processing fails

When deployed, this API can be accessed by other applications to perform NER tasks through HTTP requests, making it easy to integrate the NER functionality into other systems and services.

Run the API locally:

uvicorn app:app --reload

Test the API:

curl -X POST "http://127.0.0.1:8000/ner" \
-H "Content-Type: application/json" \
-d '{"text": "Barack Obama was born in Hawaii."}'

Let's break it down:

Command Structure:

  • The command uses cURL to send a POST request to a local API endpoint (http://127.0.0.1:8000/ner)
  • It includes a Content-Type header specifying that the request body is JSON
  • The request body contains a JSON object with a "text" field containing the sample text "Barack Obama was born in Hawaii."

Expected Response:

The API will return a JSON response containing:

  • The original input text
  • An "entities" array listing each identified entity with:
  • The detected word or phrase
  • The entity type (e.g., "PER" for person, "LOC" for location)
  • A confidence score indicating the model's certainty

This endpoint is part of a FastAPI service that processes text through a NER pipeline to identify and classify named entities in the input text.

Response:

{
  "text": "Barack Obama was born in Hawaii.",
  "entities": [
    {"word": "Barack Obama", "entity_group": "PER", "score": 0.98},
    {"word": "Hawaii", "entity_group": "LOC", "score": 0.95}
  ]
}

This code shows the JSON response from a Named Entity Recognition (NER) API endpoint. Let's break it down:

Structure:

  • The response contains two main fields:
    • "text": Shows the original input text that was analyzed ("Barack Obama was born in Hawaii.")
    • "entities": An array containing the identified named entities

Identified Entities:

  • Two entities were detected:
    • 1. "Barack Obama" - classified as "PER" (Person) with 98% confidence
    • 2. "Hawaii" - classified as "LOC" (Location) with 95% confidence

This is the output format you can expect when making requests to the NER API endpoint, which processes text through a NER pipeline to identify and classify named entities.