FastApi-GeminiApi/README.md
2025-11-16 18:30:04 +03:30

110 lines
3.1 KiB
Markdown

# AIFit Try-On Service
A FastAPI microservice that uses Google's Gemini API to generate virtual try-on images. Takes a user mirror selfie and an outfit reference image, then returns an edited image with the outfit applied.
## Setup
1. **Install dependencies** (preferably in a virtual environment):
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
```
2. **Set up environment variables**:
- Create a `.env` file in the project root (same directory as `main.py`)
- Add your Gemini API key with **NO spaces** around the `=` sign:
```
GEMINI_API_KEY=YOUR_REAL_KEY_HERE
```
- **Important**:
- No quotes around the value
- No spaces: `GEMINI_API_KEY=key` ✅ (correct)
- Wrong: `GEMINI_API_KEY = key` ❌ (has spaces)
- Wrong: `GEMINI_API_KEY="key"` ❌ (has quotes)
- The `.env` file should be in the same directory as `docker-compose.yml`
## Running the Service
### Option 1: Direct Python
Start the FastAPI server:
```bash
uvicorn main:app --reload
```
### Option 2: Docker Compose
Build and run with Docker:
```bash
docker-compose up --build
```
The API will be available at:
- API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
- Health check: http://localhost:8000/health
## API Endpoints
### GET `/health`
Health check endpoint for monitoring and Docker healthchecks.
**Response:**
```json
{"status": "ok"}
```
### POST `/api/try-on/`
Generate a try-on image by combining a user photo with an outfit reference.
**Parameters:**
- `user_photo` (file): User mirror/body photo (JPEG or PNG)
- `outfit_photo` (file): Collection/outfit photo (JPEG or PNG)
**Response:**
- Returns the generated image as `image/png`
**Example using curl:**
```bash
curl -X POST "http://localhost:8000/api/try-on/" \
-F "user_photo=@user_selfie.jpg" \
-F "outfit_photo=@outfit_reference.jpg" \
--output result.png
```
**Testing in Postman:**
1. **Health Check:**
- Method: `GET`
- URL: `http://localhost:8000/health`
- No body or headers needed
- Expected response: `{"status": "ok"}`
2. **Try-On Endpoint:**
- Method: `POST`
- URL: `http://localhost:8000/api/try-on/`
- Body tab → Select `form-data`
- Add two fields:
- `user_photo` (type: **File**) → Select your user mirror/body photo
- `outfit_photo` (type: **File**) → Select your collection/outfit photo
- Click **Send**
- Response will be an image (PNG format)
- To save: Click "Save Response" → "Save to a file"
**Import Postman Collection:**
You can import `postman_collection.json` into Postman for pre-configured requests.
## Notes
- The service uses `gemini-2.5-flash-image` model
- CORS is enabled for all origins (tighten in production)
- Images are validated before processing
- The service handles errors gracefully with appropriate HTTP status codes
- Docker healthcheck is configured to use the `/health` endpoint
- For production, remove `--reload` from the Dockerfile CMD and remove volume mounts from docker-compose.yml