Skip to content

hanifekaptan/ai-agent-for-aspect-based-sentiment-analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Agent for Aspect Based Sentiment Analysis

AI Agent for ABSA is a full-featured AI agent that detects product or service features (aspects) in texts and scores the sentiment (positive, negative, neutral) for each feature. It has a powerful backend developed with FastAPI and a user-friendly interface created with Streamlit. It performs semantic analysis in the background using large language models (LLMs) like Google Gemini.

🌐 Links

✨ Features

  • Advanced Analysis: Processes comments from text inputs or CSV files to perform aspect-based sentiment analysis.
  • RESTful API: A FastAPI backend offering comprehensive endpoints and automatic OpenAPI documentation.
  • Modern Interface: A responsive Streamlit frontend that supports modes like single text, CSV file upload, and sample data analysis.
  • Scalability: High performance through concurrent LLM calls and asynchronous operations.
  • Error Handling: Robust error management in the API for situations like quota exceeded and connection errors.
  • Docker Support: Easily deployable structure with a Dockerfile and entrypoint.sh script.
  • Testing: Verification of basic API functions with smoke tests written using pytest.

πŸš€ Quick Start (Local Development)

Prerequisites

  • Python 3.12
  • pip and venv

Installation

  1. Clone the repository:

    git clone https://github.com/hanifekaptan/ai-agent-for-aspect-based-sentiment-analysis.git
    cd ai-agent-for-aspect-based-sentiment-analysis
  2. Create and activate a virtual environment:

    # Windows
    python -m venv .venv
    .venv\Scripts\activate
    
    # Linux/Mac
    source .venv/bin/activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Environment Variables: Create a file named .env in the project root directory and add your Google Gemini API key:

    GOOGLE_API_KEY="YOUR_GEMINI_API_KEY"
    MODEL_NAME="gemini-1.5-flash-latest" 

Running the Application

  1. Run the Backend (FastAPI):

    uvicorn app.main:app --reload --host 127.0.0.1 --port 8000

    The API will be accessible at http://localhost:8000, and the interactive documentation at /docs.

  2. Run the Frontend (Streamlit) (in a new terminal):

    streamlit run frontend/app.py --server.port 8501

    The application interface will be displayed at http://localhost:8501.

πŸ“‚ Project Structure

aspect-based-senetiment-analyzer-agent/
β”œβ”€β”€ app/                          # FastAPI backend application
β”‚   β”œβ”€β”€ api/                      # API routes and endpoints
β”‚   β”‚   β”œβ”€β”€ analyze.py            # Analysis endpoint
β”‚   β”‚   └── health.py             # Health check endpoint
β”‚   β”œβ”€β”€ core/                     # Core configuration and helpers
β”‚   β”‚   └── logging.py            # Logging setup
β”‚   β”œβ”€β”€ llm/                      # LLM client logic
β”‚   β”‚   └── client.py             # Gemini API calls
β”‚   β”œβ”€β”€ prompting/                # Prompt management
β”‚   β”‚   └── manager.py            # Loading and rendering prompt templates
β”‚   β”œβ”€β”€ prompts/                  # Prompt templates (YAML)
β”‚   β”œβ”€β”€ schemas/                  # Pydantic data models
β”‚   β”œβ”€β”€ services/                 # Business logic services
β”‚   β”‚   └── absa_service.py       # ABSA analysis logic
β”‚   β”œβ”€β”€ utils/                    # Utility functions
β”‚   └── main.py                   # FastAPI application entry point
β”œβ”€β”€ frontend/                     # Streamlit frontend application
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── client.py             # Backend API client
β”‚   β”œβ”€β”€ components/               # Reusable UI components
β”‚   β”œβ”€β”€ streamlit_app/
β”‚   └── app.py                    # Streamlit application entry point
β”œβ”€β”€ docker/                       # Docker configurations
β”‚   β”œβ”€β”€ entrypoint.sh             # Script to start both backend and frontend
β”‚   └── space.Dockerfile          # Combined Dockerfile for HuggingFace
β”œβ”€β”€ tests/                        # Test suite
β”‚   └── test_smoke.py             # API smoke tests
β”œβ”€β”€ Dockerfile                    # Main Dockerfile for HuggingFace Space
β”œβ”€β”€ requirements.txt              # Python dependencies
└── README.md

πŸ—οΈ Architecture Overview

Backend (FastAPI)

  • Analysis Engine: Makes API calls to Google's Gemini model using langchain-google-genai.
  • Asynchronous Operations: Manages concurrent LLM requests using asyncio and Semaphore, which improves performance.
  • Data Parsing: Uses pandas to process data from single text, CSV, or other formats.
  • Service Layer: Provides a clean separation between API routes, business logic, and LLM calls.
  • API Documentation: Automatically generated OpenAPI (Swagger) documentation available at /docs.

Frontend (Streamlit)

  • Component-Based: Modular UI components for search, file upload, and result visualization.
  • API Client: A requests-based HTTP client with error handling to communicate with the backend.
  • State Management: Uses st.session_state to manage analysis results and API quota errors.
  • User-Friendly Design: A clean, understandable, and interactive interface.

Data Flow

  1. The user enters text or uploads a CSV file via the Streamlit interface.
  2. The frontend sends the request to the /analyze endpoint.
  3. The backend receives the input, cleans it, and divides it into appropriate batches for analysis.
  4. The absa_service generates prompts to be sent to the LLM for each batch.
  5. The LLM analyzes the features and sentiments in the texts and returns a response in a structured format.
  6. The backend parses the LLM response and sends it to the frontend in JSON format.
  7. The frontend visualizes the results with metrics, charts, and tables.

πŸ§ͺ Testing

The project includes basic smoke tests written with pytest:

# Run all tests
pytest
  • Smoke Tests: Verify the basic functionality of the API by testing the /health and mocked /analyze endpoints.

🐳 Deployment with Docker

This project is configured to run both the backend and frontend in a single container. This simplifies deployment, especially on platforms like HuggingFace Spaces.

  1. Build the Docker Image:

    docker build -t aspect-based-sentiment-analyzer .
  2. Run the Container:

    docker run -p 8000:8000 -p 8501:7860 -e GOOGLE_API_KEY="YOUR_GEMINI_API_KEY" aspect-based-sentiment-analyzer
    • The backend API will be accessible at http://localhost:8000.
    • The Streamlit interface will be accessible at http://localhost:8501.

HuggingFace Spaces

The Dockerfile in the project root is compatible with HuggingFace Spaces. You just need to link your repository to a Space and add GOOGLE_API_KEY as a secret.

πŸ› οΈ Technology Stack

  • Backend:
    • FastAPI
    • Uvicorn
    • langchain-google-genai
    • Pandas, NumPy
    • Pydantic
  • Frontend:
    • Streamlit
    • Requests
  • Language: Python 3.12
  • Testing:
    • pytest
    • pytest-asyncio
  • DevOps:
    • Docker
    • HuggingFace Spaces

πŸ“ API Endpoints

GET /health

Used to check the status of the service.

  • Response:
    {
      "status": "ok",
      "timestamp": 1678886400.0
    }

POST /analyze

Performs analysis on text or a CSV file.

  • Request (Text): text field in multipart/form-data.

  • Request (File): upload_file field in multipart/form-data.

  • Successful Response:

    {
      "items_submitted": 5,
      "batches_sent": 1,
      "results": [
        {
          "id": "1",
          "aspects": [
            { "term": "screen", "sentiment": "positive" },
            { "term": "battery", "sentiment": "negative" }
          ]
        }
      ],
      "duration_seconds": 5.43
    }
  • Error Response (Quota Exceeded):

    {
      "detail": {
        "error": "Upstream quota exceeded or rate-limit received",
        "message": "An error related to the quota was returned from the model provider. Please try again later.",
        "upstream_error": "429 Quota exceeded for model..."
      }
    }

πŸ“„ License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

πŸ“§ Contact

Hanife Kaptan - hanifekaptan.dev@gmail.com

Project Link: https://github.com/hanifekaptan/ai-agent-for-aspect-based-sentiment-analysis

⭐ Don't forget to star this repository if you find it useful!

About

A full-stack AI agent for performing granular Aspect-Based Sentiment Analysis. It uses LLMs like Google Gemini to extract specific features and their sentiment from text or CSV files. The system is served via a scalable FastAPI backend and demonstrated with a user-friendly Streamlit interface, complete with Docker support for easy deployment.

Topics

Resources

License

Stars

Watchers

Forks

Contributors