Team Ai
Apppublic

emanism6/Text-Classification-Fastapi

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
README.md141 linesDownload Raw Back to root
1---2license: apache-2.03title: Text-Classification-fastapi4sdk: docker5emoji: 🚀6colorFrom: indigo7colorTo: green8pinned: true9---10# Text Classification API with FastAPI, Docker, and Hugging Face Transformers11 12This repository contains code for building a RESTful API using FastAPI to perform text classification (sentiment analysis, emotion classification, etc) using a pre-trained Hugging Face Transformer model. The API is containerized using Docker for easy deployment.13 14---15 16## Table of Contents17 18- [Overview](#overview)19- [Dependencies](#dependencies)20- [Models Used](#models-used)21- [Code Explanation](#code-explanation)22- [File Structure](#file-structure)23- [FastAPI Integration](#fastapi-integration)24- [Dockerization](#dockerization)25- [Hugging Face Model Details](#hugging-face-model-details)26- [Hugging Face Space Link](#hugging-face-space-link)27- [Building and Running the Container](#building-and-running-the-container)28- [Interacting with the API](#interacting-with-the-api)29 30---31 32## Overview33 34The main objective of this project is to develop a robust and scalable API for text classification tasks using FastAPI, Docker, and Hugging Face Transformers. The API allows users to submit text data and receive sentiment or emotion analysis results in real-time.35 36---37 38## Dependencies39 40The project relies on the following dependencies:41 42- **FastAPI:** A modern web framework for building APIs with Python.43- **Transformers:** The Hugging Face library for natural language processing (NLP) tasks, including pre-trained models.44- **Pyngrok:** A Python wrapper for ngrok, which exposes local servers over public URLs.45- **Nest_asyncio:** A library for running asyncio nested in other asyncio loops.46- **NLTK:** The Natural Language Toolkit for text processing tasks.47- **Pydantic:** A data validation library for Python.48 49---50 51## Code Explanation52 53The main components of the code include:54 55- **main.py:** This file contains the FastAPI application, including the definition of endpoints, request and response models, and model integration.56- **test.py:** This file contains unit tests for the API endpoints using FastAPI's TestClient.57- **Dockerfile:** The Dockerfile defines the environment and instructions for building the Docker image for the FastAPI application.58- **requirements.txt:** This file lists all the Python dependencies required by the project.59 60---61 62## FastAPI Integration63 64FastAPI is used to develop the RESTful API with the following endpoints:65 66- **GET /:** A welcome endpoint that redirects to the Swagger UI page.67- **POST /analyze/:** The main endpoint for text classification. It accepts JSON input with a text field and returns the sentiment/emotion analysis results.68 69---70 71## Dockerization72 73The project is containerized using Docker for easy deployment. The Dockerfile defines the environment and instructions for building the Docker image, including installing dependencies and running the FastAPI application.74 75---76 77## Hugging Face Model Details78 79The API utilizes the `distilbert-base-multilingual-cased-sentiments-student` model from the Hugging Face model hub. This model is a distilled version of the DistilBERT model trained for sentiment analysis tasks across multiple languages.80 81---82 83## Hugging Face Space Link84 85The trained model and associated resources are hosted on the following Hugging Face Space: [FastAPI-Docker-Huggingface-Text_Sentiment_app](https://huggingface.co/spaces/HussainM899/FastAPI-Docker-Huggingface-Text_Sentiment_app).86 87---88 89## Building and Running the Container90 91To build and run the Docker container, follow these steps:92 931. Ensure you have Docker installed on your system.942. Clone this repository to your local machine.953. Navigate to the project directory in your terminal.964. Run the following command to build the Docker image:97 98```99docker build -t fastapi-test-sentiment-api-0.1.2 .100```101 1025. Once the image is built successfully, run the container with the following command:103 104```105docker run -d -p 8000:8000 fastapi-test-sentiment-api-0.1.2:latest106```107 1086. The API will now be accessible at `http://localhost:8000`.1097.1108.  ---111title: QuadrupedData112 113emoji: :turtle:114 115colorFrom: red116 117colorTo: purple118 119sdk: gradio120 121sdk_version: 3.8.2122 123app_file: app.py124 125pinned: false126---127 128---129 130## Interacting with the API131 132You can interact with the API using tools like cURL, Postman, or by sending HTTP requests programmatically. Alternatively, you can use the Swagger UI provided by FastAPI for easy testing and exploration.133 1341. Open your web browser and navigate to `http://localhost:8000/`.1352. This will open the Swagger UI interface, where you can explore the API endpoints and submit requests with sample data.1363. Click on the `/analyze/` endpoint, and then click on "Try it out" to input your text data.1374. After entering the text data, click on "Execute" to send the request and view the response, which will include the sentiment/emotion analysis results.138 139---140 141Feel free to explore the code and documentation for more details on how the API works and how to interact with it.