Skip to content

innomatrix/gemini-api

Repository files navigation

Gemini API

A FastAPI-based API that interacts with Google's Gemini AI models for text and document analysis, with asynchronous task processing using Celery.

Setup

  1. Install dependencies:
pip install -r requirements.txt
  1. Install and start Redis (required for Celery):
  1. Configure environment variables in .env:
GOOGLE_API_KEY=your_api_key
GEMINI_MODEL=gemini-1.5-flash
GEMINI_VISION_MODEL=gemini-1.5-pro
DB_ENGINE=mysql
DB_HOST=localhost
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=gemini-api
REDIS_HOST=localhost
REDIS_PORT=51837
  1. Initialize the database:
python init_db.py
  1. Start the Celery worker:
celery -A tasks worker --loglevel=info --pool=solo
  1. Run the API:
uvicorn main:app --reload

Setup and Initialization

1. Certificate Generation

Before starting the application, generate the SSL/TLS certificates using:

Docker Deployment

The application is containerized using Docker and can be easily deployed using Docker Compose. The setup includes a secure networking configuration with isolated services.

Prerequisites

  • Docker
  • Docker Compose

Environment Variables

Copy the .env.example file to .env and configure the variables:

cp .env.example .env

Required environment variables:

  • DATABASE_URL: MySQL database connection URL
  • GOOGLE_API_KEY: Google API key for Gemini
  • API_PORT: Port for the FastAPI application (default: 8088)
  • REDIS_URL: Redis connection URL (automatically configured in the internal network)
  • FLOWER_PORT: Port for Flower monitoring (default: 5555)

Docker Network Architecture

The application uses a secure networking setup with the following features:

  • Isolated gemini-net network (subnet: 172.28.0.0/16)
  • Fixed internal IPs for reliable service communication:
    • API Service: 172.28.0.2
    • Redis: 172.28.0.3
    • Celery Worker: 172.28.0.4
    • Flower: 172.28.0.5
  • Only necessary ports exposed to host:
    • API port (default: 8088)
    • Flower monitoring port (default: 5555)
  • Internal services (Redis, Celery) are not exposed to the host network

Running with Docker Compose

  1. Build and start all services:
docker compose up -d --build
  1. Access the services:
  1. View logs:
docker compose logs -f
  1. Stop all services:
docker compose down

Database Initialization and Data Import

The application automatically initializes the database if it is not already in place. When you run:

API Endpoints

POST /api/task

Process a text-based task using Gemini AI.

Parameters:

  • prompt (required): JSON object containing the prompt
  • data (optional): Additional JSON data
  • callback_url (optional): URL to receive the task result when completed

Response:

{
    "task_id": "task-uuid",
    "status": "processing",
    "message": "Task dispatched to worker"
}

GET /api/stats

Get usage statistics for the API.

Returns:

  • Total number of API calls
  • Total tokens used
  • Statistics per endpoint

POST /api/analyze_document

Analyze a document using Gemini Vision AI.

Parameters:

  • prompt (required): JSON object containing the prompt
  • file (required): Document file (PDF, DOC, DOCX, or RTF)
  • callback_url (optional): URL to receive the analysis result when completed

Response:

{
    "task_id": "task-uuid",
    "status": "processing",
    "message": "Document analysis task dispatched to worker"
}

GET /api/task/{task_id}

Get the status and result of a specific task.

Response:

{
    "task_id": "task-uuid",
    "status": "completed",
    "result": {
        "response": "Task result here"
    }
}

API Usage Examples

Here are some examples of how to use the API endpoints using curl:

1. Submit a Text Task

curl -X POST http://localhost:8088/api/task \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": {
      "text": "What is the capital of France?"
    }
  }'

with callback_url (test_callback_server.py)

curl -X POST -H "Content-Type: application/json" -d '{"prompt": {"text": "What is the capital of France?"}, "callback_url": "http://localhost:8777"}' http://localhost:8088/api/task

Response:

{
    "task_id": "bf73ddb0-bf91-450e-b782-2c155a925f66",
    "record_id": 3,
    "status": "processing",
    "message": "Task dispatched to worker"
}

2. Check Task Status

curl http://localhost:8088/api/task/<task_id>

Response:

{
    "task_id": "bf73ddb0-bf91-450e-b782-2c155a925f66",
    "status": "completed",
    "result": {
        "response": "Paris"
    }
}

3. Upload and Analyze a Document

curl -X POST http://localhost:8088/api/document \
  -H "Content-Type: multipart/form-data" \
  -F "file=@/path/to/your/document.pdf" \
  -F "prompt={\"text\":\"Summarize this document\"}"

4. Get API Usage Statistics

curl http://localhost:8088/api/stats

Callback URLs

When providing a callback_url, the API will make a POST request to that URL once the task is completed. The request body will contain the task result in JSON format.

Monitoring

You can monitor tasks and their status using the Flower monitoring interface:

  • URL: http://localhost:5555
  • Default credentials: admin/admin
  • Customize credentials using FLOWER_USER and FLOWER_PASSWORD environment variables

The monitoring interface provides:

  • Real-time task execution monitoring
  • Worker status and statistics
  • Task history and results
  • Performance metrics

Database

The API uses MySQL to store usage metrics and API call data. The database schema is automatically created when running the application for the first time.

About

Async FastAPI wrapper for Google Gemini AI — supports text tasks, document analysis (PDF/DOCX), Celery task queuing, callback URLs, usage stats, and Docker deployment with Traefik/Caddy reverse proxy support.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors