Skip to content

A sentiment analysis project built with Fast API (python) backend and ReactJS (Vite) frontend using cohere API and deployed on Bare Metal Server (Compute Engine)

License

Notifications You must be signed in to change notification settings

NehaKoppikar/cohere-sentiment-analysis

Repository files navigation

Movie Review Sentiment Analysis

A web application that analyzes the sentiment of movie reviews using Cohere's LLM API. The application consists of a ReactJS frontend and a FastAPI backend, containerized with Docker and deployed on the GCP Compute Engine.


Deployed Application URL
Screenshot image

Architecture Overview

The application follows a microservices architecture with the following components:

  • Frontend: React application that provides the user interface for submitting movie reviews and displaying sentiment analysis results
  • Backend: FastAPI service that interfaces with Cohere's API to perform sentiment analysis
  • API Integration: Cohere's LLM API for natural language processing and sentiment classification
  • Docker: Containerization of both frontend and backend services
  • Nginx: Reverse proxy to handle routing and serve the application image

Prerequisites

  • Docker and Docker Compose
  • Cohere API key (get it from Cohere Dashboard)
  • GCP account with Compute Engine access (for deployment)

Local Development Setup

  1. Clone the repository:
git clone https://github.com/NehaKoppikar/cohere-sentiment-analysis.git
cd cohere-sentiment-analysis
  1. Create a .env file in the root directory:
cohere_api=your_api_key_here
CORS_ORIGINS=http://localhost:5173
  1. Start the application using Docker Compose:
docker compose up -d --build
  1. Access the application:

Backend Endpoints

The backend provides two endpoints:

  1. Health Check:

    • URL: GET /health
    • Response: {"status": "healthy"}
    backend_health_check
  2. Sentiment Analysis:

    • URL: POST /sentiment-analysis
    • Content-Type: application/x-www-form-urlencoded
    • Form Parameter: text (the movie review text)
    • Response Example:
      {
        "sentiment": "Positive",
        "confidence": 95.5,
        "message": "This seems like a positive review."
      }
      backend_sentiment_analysis_prediction

Deployment Steps

  1. SSH into your GCP Compute Engine instance:
gcloud compute ssh your-instance-name
  1. Install Docker and Docker Compose:
# Install Docker
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
  1. Clone the repository and set up the environment:
git clone https://github.com/NehaKoppikar/cohere-sentiment-analysis.git
cd cohere-sentiment-analysis
cp .env.example .env
# Edit .env with your Cohere API key
nano .env
  1. Install and configure Nginx:
sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/app.conf
# Copy the Nginx configuration provided in the repository
sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx
  1. Start the application:
docker compose up -d --build

Troubleshooting

  1. If containers fail to start:

    docker compose logs
  2. Common issues:

    • Verify Cohere API key is correctly set in .env
    • Check if ports 5173 and 8080 are available
    • Ensure Docker service is running
    • Verify all containers are up: docker compose ps
  3. For deployment issues:

    • Check GCP firewall rules for ports 80, 5173, and 8080
    • Verify Nginx configuration
    • Check application logs: docker compose logs frontend or docker compose logs backend

Security Notes

  • The Cohere API key is stored in the .env file (not committed to the repository)
  • CORS is configured to allow only localhost origins for development
  • Nginx is configured with security headers
  • Input validation is implemented on the backend

About

A sentiment analysis project built with Fast API (python) backend and ReactJS (Vite) frontend using cohere API and deployed on Bare Metal Server (Compute Engine)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published