LogicGoInfotechSpaces's picture
Add project files
0ed44c0

Nano Banana Image Edit API

A FastAPI-based REST API for the Qwen Image Edit model that allows you to upload images, edit them with text prompts, and retrieve results.

Features

  • Upload Images: Upload image files via POST request
  • Edit Images: Submit image ID and text prompt to edit images
  • Get Results: Retrieve edited images by task ID
  • Multiple Formats: Get results as files or base64-encoded strings

Installation

  1. Install dependencies:
pip install -r requirements_api.txt
  1. Run the API server:
python api.py

Or using uvicorn directly:

uvicorn api:app --reload --host 0.0.0.0 --port 8000

API Endpoints

1. Upload Image

POST /upload

Upload an image file.

Request:

  • Content-Type: multipart/form-data
  • Body: file (image file)

Response:

{
  "image_id": "uuid-string",
  "message": "Image uploaded successfully",
  "timestamp": "2024-01-01T12:00:00"
}

Example using curl:

curl -X POST "http://localhost:8000/upload" \
  -F "file=@/path/to/image.jpg"

2. Edit Image

POST /edit

Edit an uploaded image using a text prompt.

Request:

  • Content-Type: multipart/form-data
  • Body:
    • image_id: ID of uploaded image
    • prompt: Text description of desired edit

Response:

{
  "task_id": "uuid-string",
  "status": "completed",
  "message": "Image edited successfully",
  "timestamp": "2024-01-01T12:00:00"
}

Example using curl:

curl -X POST "http://localhost:8000/edit" \
  -F "image_id=your-image-id" \
  -F "prompt=make the sky more blue"

3. Get Result

GET /result/{task_id}

Get the status and result information for an editing task.

Response:

{
  "task_id": "uuid-string",
  "status": "completed",
  "result_image_id": "uuid-string",
  "result_image_url": "/result/image/uuid-string",
  "timestamp": "2024-01-01T12:00:00"
}

4. Get Result Image (File)

GET /result/image/{result_image_id}

Download the edited image as a file.

Response: Image file (PNG)

5. Get Result Image (Base64)

GET /result/image/{result_image_id}/base64

Get the edited image as a base64-encoded string.

Response:

{
  "result_image_id": "uuid-string",
  "image_base64": "base64-encoded-string",
  "format": "png"
}

6. Health Check

GET /health

Check API health and model status.

Response:

{
  "status": "healthy",
  "model_loaded": true,
  "model_available": true
}

Usage Example (Python)

import requests

# Upload image
with open("image.jpg", "rb") as f:
    response = requests.post(
        "http://localhost:8000/upload",
        files={"file": f}
    )
upload_data = response.json()
image_id = upload_data["image_id"]

# Edit image
response = requests.post(
    "http://localhost:8000/edit",
    data={
        "image_id": image_id,
        "prompt": "make the background sunset orange"
    }
)
edit_data = response.json()
task_id = edit_data["task_id"]

# Get result
response = requests.get(f"http://localhost:8000/result/{task_id}")
result_data = response.json()
result_image_id = result_data["result_image_id"]

# Download edited image
response = requests.get(f"http://localhost:8000/result/image/{result_image_id}")
with open("edited_image.png", "wb") as f:
    f.write(response.content)

Notes

  • The current implementation uses in-memory storage for tasks. For production, consider using:

    • Redis or database for task storage
    • S3, Azure Blob Storage, or similar for image storage
    • Proper authentication and rate limiting
    • Async task processing with Celery or similar
  • The model loading code is a placeholder. You'll need to adjust it based on the actual Qwen-Image-Edit model interface and requirements.

API Documentation

Once the server is running, visit:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc