This repository contains a simple Flask API that performs sentiment analysis on text input using a quantized DistilBERT model. Quantization helps reduce the model size and inference time, making it more efficient for deployment.
app.py
: Main Flask application file. Defines the API endpoint and handles requests.models/sentiment_model.py
: Handles model loading, quantization, and inference.
└── 📁 models
└── 📄 sentiment_model.py
📄 app.py
📄 requirements.txt
-
Install dependencies:
pip install -r requirements.txt
(Make sure you have a
requirements.txt
file with the necessary packages, or create one with:pip freeze > requirements.txt
) -
Run the Flask API:
flask run
(Or
python app.py
, depending on your setup) -
Send requests to the API:
You can use tools like
curl
orPostman
to send POST requests to the/predict
endpoint.Example using
curl
:curl -X POST \ -H "Content-Type: application/json" \ -d '{"text": "This is a great movie! I highly recommend it."}' \ http://127.0.0.1:5000/predict
Example using Python's
requests
library:import requests api_url = 'http://127.0.0.1:5000/predict' text = "This is a great movie! I highly recommend it." data = {'text': text} response = requests.post(api_url, json=data) if response.status_code == 200: result = response.json() print(result) else: print(f"Request error: {response.status_code}")
The API will respond with a JSON object containing:
{ "text": "This is a great movie! I highly recommend it.", "sentiment": "POSITIVE", "score": 0.9998656630516052 }
- The API currently uses a pre-trained and quantized DistilBERT model. You can replace this with a different model by modifying the
model_name
and loading the appropriate model file inmodels/sentiment_model.py
. - This is a basic example, and you might need to adjust it based on your specific needs and deployment environment. Consider adding error handling, authentication, and other features for a more robust application.