This project is a sentiment analysis web service built with Django, MySQL, and OpenAI GPT-4o. It allows users to input text and receive real-time sentiment analysis results.
Check out the live demo: https://sa.hsingh.site/
- Docker
- Docker Compose
-
Clone the repository:
git clone https://github.com/siinghd/sentiment-analysis.git cd sentiment-analysis
-
Configure
docker-compose.yml
with correctENV VARS
. Look here -
Build the Docker containers:
docker-compose build # or docker compose build
-
Start the containers:
docker-compose up # or docker compose up
This will start the following services:
db
: MySQL database containerweb
: Django web application containermigrate
: Container to run database migrations (run and exit)
-
Access the web application:
Open your web browser and visit
http://localhost:8000
to access the sentiment analysis web service.
If you prefer to run the application without using Docker, follow these steps:
- Python 3.8 or higher
- MySQL Server
- Pip (Python package installer)
-
Set up the MySQL Database:
- Install MySQL on your machine.
- Create a new database and user for the application.
- Grant all privileges on your database to the new user.
-
Clone the repository and enter the directory:
git clone https://github.com/siinghd/sentiment-analysis.git cd sentiment-analysis
-
Install Python dependencies:
- Ensure
pip
is installed and then run:
pip install -r requirements.txt
- Ensure
-
Configure the application:
- Copy the example environment configuration file and modify it with your settings:
cp .env.example .env
- Open the
.env
file and update it with your database credentials and other environment variables such asOPENAI_API_KEY
andDJANGO_SECRET_KEY
.
-
Perform database migrations:
python manage.py migrate
-
Run the Django development server:
python manage.py runserver # or pip install gunicorn and run : gunicorn sentiment_analysis_project.wsgi:application --bind 127.0.0.1:8000
This will start the server on
http://localhost:8000
. -
Access the web application:
- Open your web browser and visit
http://localhost:8000
to access the sentiment analysis web service.
- Open your web browser and visit
- Ensure that your Python environment is correctly set up and that all dependencies listed in
requirements.txt
are installed without errors. - Make sure your MySQL service is running and accessible by the Django application.
- Adjust the
.env
file as necessary to ensure all environment variables are correctly set for your development environment.
The project uses environment variables for configuration. You can modify the following variables in the docker-compose.yml
file:
OPENAI_API_KEY
: Your OpenAI API key for sentiment analysis. Make sure to replace it with your own API key.DJANGO_SECRET_KEY
: Secret key for Django. Change this to a secure value in production.DB_NAME
: Name of the MySQL database.DB_USER
: MySQL database user.DB_PASSWORD
: MySQL database password.DB_HOST
: MySQL database host (set todb
for Docker Compose setup).DB_PORT
: MySQL database port.DEBUG
: Django DEBUG flag, default:false
.
- Open your web browser and visit
http://localhost:8000
. - Enter the text you want to analyze in the provided input field.
- Click the "Analyze Sentiment" button.
- The sentiment analysis result will be displayed on the page.
The project has the following structure:
api/
: Django app containing the API views, models, serializers, and exceptions.sentiment_analysis_project/
: Django project configuration files.templates/
: HTML templates directory.utils/
: Useful utilitiesDockerfile
: Dockerfile for building the web application container.docker-compose.yml
: Docker Compose configuration file.manage.py
: Django management script.requirements.txt
: Python dependencies file.
The project uses the following main dependencies:
- Django: Web framework for building the sentiment analysis web service.
- Django REST Framework: Toolkit for building RESTful APIs in Django.
- MySQL: Database for storing sentiment analysis results.
- OpenAI GPT-4o: API for performing sentiment analysis.
- Gunicorn: WSGI HTTP server for running the Django application.
For a complete list of dependencies, refer to the requirements.txt
file.
For small projects, using other hosting services like Hetzner might be more cost-effective. But if choosing AWS, here are some recommended services:
Amazon EKS
: Use EKS for managing Kubernetes containers. It's a scalable and secure environment.Amazon ECR
: Store Docker images in a fully managed registry using ECR.Amazon RDS
: Use RDS for databases. It makes setting up and scaling databases easier.AWS Elastic Beanstalk
: Deploy apps quickly on Beanstalk without having to manage lots of hosting stuff manually.Amazon EC2
: Run the app on EC2 virtual servers with customizable configurations.
Elastic Load Balancing (ELB)
: Use ELB with Auto Scaling Groups to efficiently distribute traffic across multiple servers like EC2 instances. It can automatically scale resources based on demand.