A FastAPI-based API that interacts with Google's Gemini AI models for text and document analysis, with asynchronous task processing using Celery.
- Install dependencies:
pip install -r requirements.txt- Install and start Redis (required for Celery):
- Windows: Download and install Redis from https://github.com/microsoftarchive/redis/releases
- Start Redis server
- 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- Initialize the database:
python init_db.py- Start the Celery worker:
celery -A tasks worker --loglevel=info --pool=solo- Run the API:
uvicorn main:app --reloadBefore starting the application, generate the SSL/TLS certificates using:
The application is containerized using Docker and can be easily deployed using Docker Compose. The setup includes a secure networking configuration with isolated services.
- Docker
- Docker Compose
Copy the .env.example file to .env and configure the variables:
cp .env.example .envRequired environment variables:
DATABASE_URL: MySQL database connection URLGOOGLE_API_KEY: Google API key for GeminiAPI_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)
The application uses a secure networking setup with the following features:
- Isolated
gemini-netnetwork (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
- Build and start all services:
docker compose up -d --build- Access the services:
- API: http://localhost:8088
- API Documentation: http://localhost:8088/docs
- Flower (Celery monitoring): http://localhost:5555
- View logs:
docker compose logs -f- Stop all services:
docker compose downThe application automatically initializes the database if it is not already in place. When you run:
Process a text-based task using Gemini AI.
Parameters:
prompt(required): JSON object containing the promptdata(optional): Additional JSON datacallback_url(optional): URL to receive the task result when completed
Response:
{
"task_id": "task-uuid",
"status": "processing",
"message": "Task dispatched to worker"
}Get usage statistics for the API.
Returns:
- Total number of API calls
- Total tokens used
- Statistics per endpoint
Analyze a document using Gemini Vision AI.
Parameters:
prompt(required): JSON object containing the promptfile(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 the status and result of a specific task.
Response:
{
"task_id": "task-uuid",
"status": "completed",
"result": {
"response": "Task result here"
}
}Here are some examples of how to use the API endpoints using curl:
curl -X POST http://localhost:8088/api/task \
-H "Content-Type: application/json" \
-d '{
"prompt": {
"text": "What is the capital of France?"
}
}'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/taskResponse:
{
"task_id": "bf73ddb0-bf91-450e-b782-2c155a925f66",
"record_id": 3,
"status": "processing",
"message": "Task dispatched to worker"
}curl http://localhost:8088/api/task/<task_id>Response:
{
"task_id": "bf73ddb0-bf91-450e-b782-2c155a925f66",
"status": "completed",
"result": {
"response": "Paris"
}
}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\"}"curl http://localhost:8088/api/statsWhen 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.
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
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.