Create a FastAPI service that utilizes a pre-trained Hugging Face NLP model to perform text summarization on user-provided text inputs. The service should expose an API to receive text input and return a summary in a JSON response.
-
Environment Setup:
- Set up a Python environment with the necessary dependencies (FastAPI, Hugging Face Transformers, or any other library you find appropriate)
-
Model Selection:
- Choose a pre-trained NLP model from the Hugging Face Model Hub that is suitable for text summarization.
-
API Endpoint:
- Implement an endpoint
/summarizethat:- Accepts a JSON input containing the user-provided text and a desired summary length.
- Returns a JSON response with the summarized text.
Example Request:
{ "text": "This is a long text input that needs to be summarized. The FastAPI service should be able to handle lengthy inputs and generate a coherent summary based on the user-defined summary length...", "summary_length": 50 }Example Response:
{ "summary": "A brief summary of the lengthy input text, generated by the pre-trained Hugging Face model." } - Implement an endpoint
-
Containerization:
- Containerize the FastAPI service using Docker.
- Provide a
Dockerfileand instructions on how to build and run the Docker container.
-
Documentation:
- Include a
READMEfile with clear instructions on how to set up and run the service.
- Include a
For candidates who wish to further demonstrate their technical abilities, implement the following additional functionality:
-
Compressed Summary Endpoint:
- Add a POST endpoint
/compress_summarythat:- Accepts the same input as
/summarize. - Returns a compressed version of the summarized text.
- Accepts the same input as
- Add a POST endpoint
-
Compression Algorithm:
- For each group of consecutive repeating characters in the summarized text:
- If the group's length is 1, append the character to the compressed string.
- Otherwise, append the character followed by the group's length.
Example:
- Original Summary:
This committee meets annually to assess the effectiveness of policies on environmental protection. - Compressed Summary:
This com2it2e2 me2ts an2ual2y to as2es2 the ef2ectivenes2 of policies on environmental protection. - Sample Output:
{ "summary": "This com2it2e2 me2ts an2ual2y to as2es2 the ef2ectivenes2 of policies on environmental protection." } - For each group of consecutive repeating characters in the summarized text: