Skip to main content

Add your description here

Project description

Tools API - Document Processing & AI Services

License: Apache 2.0 Python Version FastAPI LangChain LangGraph Google Cloud Docker uv Test

A comprehensive FastAPI application providing document conversion, translation, email AI classification, paper tracking, and web search capabilities. Built with modern Python tooling and deployed on Google Cloud Platform.

๐Ÿš€ Features

Document Processing

  • Document Conversion: Convert PDFs, DOCs, EPUBs to HTML, JSON, and Markdown formats
  • Document Translation: Translate documents between multiple languages with AI
    • Horizontal Bilingual Format: Side-by-side comparison view (default since v0.1.4)
    • Vertical Bilingual Format: Traditional stacked translation format
    • Non-ASCII Filename Support: Proper RFC 5987 encoding for international characters
  • OCR Support: Extract text from images within documents
  • Chunking: Process large documents by splitting into manageable parts
  • Async Processing: Handle large files with background job processing

AI Services

  • Email Classification: AI-powered email categorization and analysis
  • Paper Tracking: Automated academic paper discovery and summarization
  • Web Search: Multi-engine web search capabilities
  • Prompt Improvement: AI-assisted translation prompt optimization

Web Interface

  • Flutter Web UI: Modern web interface for document processing
  • Background Pattern: Enhanced visual design
  • Terraform Deployment: Infrastructure as Code for UI deployment

Infrastructure

  • Cloud Storage Integration: Google Cloud Storage for file management
  • Background Jobs: Cloud Run Jobs for long-running tasks
  • Database Tracking: MySQL with Alembic migrations for job and paper tracking
  • Email Notifications: Gmail API integration for job completion alerts
  • Terraform Configuration: Infrastructure as Code for all services

๐Ÿ“ Project Structure

app/
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ main.py                     # FastAPI application entry point
โ”œโ”€โ”€ api/                        # API layer
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ v1/                     # API version 1
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ doc_convert.py      # Document conversion endpoints
โ”‚       โ”œโ”€โ”€ doc_translation.py  # Document translation endpoints
โ”‚       โ”œโ”€โ”€ emai_ai.py         # Email AI classification endpoints
โ”‚       โ”œโ”€โ”€ health.py          # Health check endpoints
โ”‚       โ”œโ”€โ”€ paper_tracking.py  # Paper tracking endpoints
โ”‚       โ””โ”€โ”€ general_tool/       # General tools (web search)
โ”‚           โ””โ”€โ”€ web_search.py
โ”œโ”€โ”€ core/                       # Core application logic
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ config/                 # Configuration
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ settings.py         # Application settings
โ”‚   โ”œโ”€โ”€ database.py             # Database connection
โ”‚   โ”œโ”€โ”€ logging.py              # Logging configuration
โ”‚   โ””โ”€โ”€ security/               # Security utilities
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ””โ”€โ”€ auth.py             # API key authentication
โ”œโ”€โ”€ jobs/                       # Background job definitions
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ doc_convert_job.py
โ”‚   โ”œโ”€โ”€ doc_translation_job.py
โ”‚   โ””โ”€โ”€ summary_papers_job.py
โ”œโ”€โ”€ models/                     # Data models
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ entities/               # Database entities
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ job_execution.py    # Job tracking model
โ”‚   โ”‚   โ””โ”€โ”€ paper_tracking.py   # Paper tracking model
โ”‚   โ”œโ”€โ”€ enums/                  # Enumeration definitions
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ email_ai.py
โ”‚   โ”‚   โ””โ”€โ”€ general_tool/
โ”‚   โ”œโ”€โ”€ parses/                 # Response parsers
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ doc_convert.py
โ”‚   โ”‚   โ”œโ”€โ”€ doc_translation.py
โ”‚   โ”‚   โ”œโ”€โ”€ email_ai.py
โ”‚   โ”‚   โ””โ”€โ”€ paper_tracking.py
โ”‚   โ””โ”€โ”€ schemas/                # Pydantic schemas
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ doc_convert.py
โ”‚       โ”œโ”€โ”€ doc_translation.py
โ”‚       โ”œโ”€โ”€ email_ai.py
โ”‚       โ”œโ”€โ”€ general_tool/
โ”‚       โ”œโ”€โ”€ health.py
โ”‚       โ””โ”€โ”€ paper_tracking.py
โ”œโ”€โ”€ prompts/                    # AI prompt templates
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ doc_convert.py
โ”‚   โ”œโ”€โ”€ doc_translation.py
โ”‚   โ”œโ”€โ”€ email_ai.py
โ”‚   โ””โ”€โ”€ paper_tracking.py
โ”œโ”€โ”€ repositories/                # Data access layer
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ paper_tracking_repository.py
โ”œโ”€โ”€ services/                    # Business logic layer
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ doc_convert/            # Document conversion service
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ core_convert_mixin.py
โ”‚   โ”‚   โ”œโ”€โ”€ doc_convert_service.py
โ”‚   โ”‚   โ””โ”€โ”€ post_processing_mixin.py
โ”‚   โ”œโ”€โ”€ doc_translation/        # Document translation service
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ core_translation_mixin.py
โ”‚   โ”‚   โ”œโ”€โ”€ doc_translation_service.py
โ”‚   โ”‚   โ””โ”€โ”€ improve_prompt_mixin.py
โ”‚   โ”œโ”€โ”€ email_ai/               # Email AI service
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ email_ai_service.py
โ”‚   โ”œโ”€โ”€ general_tool/           # General tools
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ web_search/
โ”‚   โ”œโ”€โ”€ health/                 # Health service
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ health_service.py
โ”‚   โ”œโ”€โ”€ paper_tracking/         # Paper tracking service
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ email_template.py
โ”‚   โ”‚   โ”œโ”€โ”€ paper_tracking_service.py
โ”‚   โ”‚   โ””โ”€โ”€ summary_papers_mixin.py
โ”‚   โ””โ”€โ”€ tracking/               # Job tracking service
โ”œโ”€โ”€ tests/                      # Test suite
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ unit/                   # Unit tests
โ”‚   โ”‚   โ””โ”€โ”€ utils/
โ”‚   โ”‚       โ””โ”€โ”€ test_file_helper.py
โ”‚   โ”œโ”€โ”€ integration/            # Integration tests
โ”‚   โ”œโ”€โ”€ api/                    # API tests
โ”‚   โ”œโ”€โ”€ fixtures/               # Test fixtures
โ”‚   โ””โ”€โ”€ e2e/                    # End-to-end tests
โ””โ”€โ”€ utils/                      # Utility functions
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ concurrency.py
    โ”œโ”€โ”€ email.py
    โ”œโ”€โ”€ file_helper.py
    โ”œโ”€โ”€ gcs_helper.py
    โ”œโ”€โ”€ languages.py
    โ”œโ”€โ”€ pdf_helper.py
    โ””โ”€โ”€ time_helper.py

ui/                             # Flutter Web UI (separate deployment)
โ”œโ”€โ”€ lib/                        # Flutter source code
โ”œโ”€โ”€ web/                        # Web-specific files
โ”œโ”€โ”€ pubspec.yaml                # Flutter dependencies
โ””โ”€โ”€ terraform/                  # UI Terraform configuration
    โ”œโ”€โ”€ main.tf
    โ”œโ”€โ”€ variables.tf
    โ””โ”€โ”€ outputs.tf

terraform/                      # Backend Terraform configuration
โ”œโ”€โ”€ main.tf                     # Main infrastructure
โ”œโ”€โ”€ variables.tf                # Configuration variables
โ”œโ”€โ”€ outputs.tf                  # Output values
โ”œโ”€โ”€ cloud_run.tf                # Cloud Run services and jobs
โ”œโ”€โ”€ iam.tf                      # IAM roles and permissions
โ”œโ”€โ”€ secrets.tf                  # Secret Manager configuration
โ””โ”€โ”€ build.tf                    # Cloud Build configuration

๐Ÿ›  Technology Stack

  • Framework: FastAPI with Hypercorn ASGI server
  • Language: Python 3.12+
  • Package Manager: uv (Astral)
  • Database: MySQL with SQLAlchemy ORM
  • Migrations: Alembic
  • AI/ML: Google Gemini AI, LangChain, LangGraph
  • Document Processing: Docling, PyPDF, Pandoc, Playwright
  • Cloud: Google Cloud Platform (Cloud Run, Cloud Storage, Secret Manager)
  • Containerization: Docker with multi-stage builds
  • Infrastructure: Terraform for Infrastructure as Code
  • Deployment: Cloud Build, GKE, Cloud Run Jobs
  • Web UI: Flutter Web with Nginx

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.12+
  • uv package manager
  • MySQL database
  • Google Cloud Platform account
  • Docker (for containerized deployment)
  • Terraform (for infrastructure management)

Local Development

  1. Clone and setup:
git clone <repository-url>
cd tools
uv sync
  1. Environment Configuration: Create a .env file with required variables:
# API Configuration
API_TITLE="Tools API"
API_DESCRIPTION="API for document conversion and processing tools"
API_VERSION="v51"
HOST="0.0.0.0"
PORT=8080
DEBUG=false
CORS_ORIGINS=["*"]

# Authentication
API_KEYS=["your-api-key-here"]

# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=tools_db

# Google Cloud Configuration
GOOGLE_API_KEY=your_google_api_key
UPLOAD_BUCKET=your_gcs_bucket
GOOGLE_APPLICATION_CREDENTIALS=path/to/service-account.json

# Gmail API Configuration
GOOGLE_CREDENTIALS=path/to/google-credentials.json
GOOGLE_TOKEN=path/to/token.json

# Search APIs
GOOGLE_CUSTOM_SEARCH_KEY=your_search_key
GOOGLE_CUSTOM_SEARCH_CSE_ID=your_cse_id
BRAVE_API_KEY=your_brave_key
TAVILY_API_KEY=your_tavily_key
  1. Database Setup:
# Start MySQL service
brew services start mysql  # macOS
# or
sudo systemctl start mysql  # Linux

# Create database
mysql -u root -p
CREATE DATABASE tools_db;
  1. Run Database Migrations:
alembic upgrade head
  1. Start the Server:
python -m app.main
  1. Access the API:

๐Ÿ“š API Endpoints

Health Check

  • GET /health/ - Comprehensive health check with system metrics
  • GET /health/simple - Simple health check
  • GET / - Root endpoint with API information

Document Conversion (/doc-convert)

  • POST / - Convert uploaded document to HTML, JSON, and Markdown
  • POST /download-zip - Convert document and return as ZIP archive
  • POST /convert-epub - Convert EPUB files to HTML or PDF
  • POST /post-processing-html - Post-process HTML with formatting options
  • POST /generate-upload-url - Generate GCS signed URL for direct upload
  • GET /convert-from-storage - Convert document from GCS storage
  • GET /convert-from-storage/download-zip - Convert from GCS and return ZIP
  • POST /trigger-large-file-conversion - Trigger async conversion for large files

Document Translation (/doc-translation)

  • POST / - Translate uploaded document with bilingual format options
    • bilingual_format: Choose between horizontal (default) or vertical layout
  • POST /translate-converted-zip - Translate documents from conversion ZIP
  • POST /translate-pdf - Translate PDF by converting then translating
  • POST /translate-epub - Translate EPUB files
  • POST /trigger-large-file-translation - Trigger async translation for large files
  • POST /improve-prompt - Get improved translation prompt suggestions

Email AI (/email-ai)

  • POST /classify-email - Classify single email using AI
  • POST /classify-email/batch - Classify multiple emails in batch
  • GET /classify-email/categories - Get available classification categories

Paper Tracking (/paper-tracking)

  • POST /fetch-papers - Fetch academic papers by date and type
  • GET /summary-papers - Summarize papers with AI
  • POST /trigger-summary-job - Trigger background job for paper summarization

Web Search (/search-web)

  • GET / - Search the web using multiple search engines

๐Ÿ—„ Database Schema

Job Executions Table

Tracks background job executions with token usage and timing information:

  • Job types: DOC_TRANSLATION, DOC_CONVERT, DOC_TRANSLATION_V2, IMPROVED_TRANSLATION_PROMPT, EMAIL_CLASSIFICATION
  • Status tracking: PENDING, RUNNING, COMPLETED, FAILED
  • Token usage monitoring for AI services
  • Email notifications and error handling

Paper Tracking Tables

  • paper_tracking: Stores academic paper metadata
  • paper_tracking_types: Links papers to tracking types (daily, weekly, monthly)

๐Ÿ”ง Database Migrations

Create Migration

# Auto-generate from model changes
alembic revision --autogenerate -m "Description of changes" --rev-id="N"

# Create empty migration
alembic revision -m "Description of changes" --rev-id="N"

Apply Migrations

# Apply all pending migrations
alembic upgrade head

# Apply specific migration
alembic upgrade N

# Rollback to previous migration
alembic downgrade -1

# Rollback to specific migration
alembic downgrade N

Migration Management

# Check current migration status
alembic current

# Check migration history
alembic history

# Show SQL for migration (without applying)
alembic upgrade head --sql

๐Ÿš€ Deployment

Terraform (Recommended)

The project uses Terraform for Infrastructure as Code to manage all cloud resources.

Backend Services

  1. Initialize Terraform:
cd terraform
terraform init
  1. Configure Variables: Create terraform.tfvars:
project_id = "your-gcp-project-id"
region = "us-east1"
api_keys = ["your-api-key"]
  1. Deploy Infrastructure:
# Plan deployment
terraform plan

# Apply changes
terraform apply

# Deploy with auto-approve
terraform apply -auto-approve
  1. Access Services:
  • Backend API: Output from terraform output tools_api_url
  • Cloud Run Jobs: Configured for document conversion and translation

Web UI

  1. Build and Deploy UI:
cd ui/terraform
terraform init
terraform apply
  1. Access UI:
  • Web Interface: Output from terraform output ui_url

Google Cloud Run (Legacy)

  1. Build and Deploy:
# Deploy API service
./deploy.sh

# Deploy all services (API + Cloud Run Jobs)
./deploy-all.sh

# Deploy with tests
./deploy-all.sh --test
  1. Configuration:
  • Update version.env for version management
  • Configure env.sh for environment variables and secrets
  • Set up Google Cloud Secret Manager for sensitive data

Google Kubernetes Engine (GKE)

  1. Prerequisites:
gcloud auth login
gcloud config set project your-project-id
gcloud services enable artifactregistry.googleapis.com container.googleapis.com cloudbuild.googleapis.com
  1. Deploy:
# Build and deploy to GKE
gcloud builds submit --region=us-east1 --config=cloudbuild.yaml --substitutions=_TAG=v1
  1. Access:
gcloud container clusters get-credentials tools-cluster --region=us-east1
kubectl get svc tools-api -w

Docker

# Build image
docker build -t tools-api .

# Run container
docker run -p 8080:8080 --env-file .env tools-api

๐Ÿ” Security

  • API Key Authentication: All endpoints require valid API keys
  • CORS Configuration: Configurable cross-origin resource sharing
  • Secret Management: Google Cloud Secret Manager integration
  • Input Validation: Pydantic schemas for request/response validation
  • Error Handling: Comprehensive error handling and logging
  • RFC 5987 Encoding: Proper handling of international characters in filenames

๐Ÿ“Š Monitoring & Logging

  • Health Checks: Comprehensive system health monitoring
  • Structured Logging: Loguru-based logging with JSON output
  • Job Tracking: Database-backed job execution monitoring
  • Performance Metrics: CPU, memory, and disk usage tracking
  • Error Tracking: Detailed error logging with stack traces

๐Ÿงช Testing

# Run unit tests
pytest tests/unit

# Run all tests
pytest

# Manual API testing
curl -X GET "http://localhost:8080/health/" \
  -H "X-API-Key: your-api-key"

๐Ÿ“– Documentation

๐Ÿ†• What's New

Version 0.1.4 (November 2025)

  • โœจ Horizontal Bilingual Translation: New side-by-side comparison format (now default)
  • ๐ŸŒ Non-ASCII Filename Support: RFC 5987 encoding for international characters
  • ๐ŸŽจ Flutter Web UI: Modern web interface with Terraform deployment
  • ๐Ÿ“‹ Enhanced Changelog: Improved release notes generation
  • ๐Ÿ› Bug Fixes: Fixed backtick preservation in release notes

Version 0.1.3 (October 2025)

  • ๐Ÿ”ง RFC 5987 Encoding: Utility for Content-Disposition headers
  • ๐Ÿงช Test Organization: Restructured test suite (unit/integration/api/e2e)
  • ๐Ÿ”„ EPUB Support: Convert and translate EPUB files
  • โšก Performance: Improved translation speed and reliability

๐Ÿค Contributing

  1. Follow the established layer architecture pattern
  2. Add new features by extending the appropriate service layer
  3. Update API documentation and schemas
  4. Add database migrations for schema changes
  5. Write tests for new functionality
  6. Update CHANGELOG.md for significant changes
  7. Test thoroughly before deployment

๐Ÿ“„ License

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

Apache License 2.0 is a permissive open-source license that allows you to:

  • โœ… Use the software for any purpose
  • โœ… Modify and distribute the software
  • โœ… Include in proprietary software
  • โœ… Patent use and distribution
  • โœ… Commercial use

Requirements:

  • ๐Ÿ“‹ Include the original license and copyright notice
  • ๐Ÿ“‹ State any changes made to the original software
  • ๐Ÿ“‹ Include the NOTICE file if present

Version: v54.1
Last Updated: 2025-11-08
Maintainer: Cao Minh Tien (iamcaominhtien@gmail.com)

Project details


Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page