File size: 3,943 Bytes
0ed44c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 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:
```bash
pip install -r requirements_api.txt
```

2. Run the API server:
```bash
python api.py
```

Or using uvicorn directly:
```bash
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:**
```json
{
  "image_id": "uuid-string",
  "message": "Image uploaded successfully",
  "timestamp": "2024-01-01T12:00:00"
}
```

**Example using curl:**
```bash
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:**
```json
{
  "task_id": "uuid-string",
  "status": "completed",
  "message": "Image edited successfully",
  "timestamp": "2024-01-01T12:00:00"
}
```

**Example using curl:**
```bash
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:**
```json
{
  "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:**
```json
{
  "result_image_id": "uuid-string",
  "image_base64": "base64-encoded-string",
  "format": "png"
}
```

### 6. Health Check
**GET** `/health`

Check API health and model status.

**Response:**
```json
{
  "status": "healthy",
  "model_loaded": true,
  "model_available": true
}
```

## Usage Example (Python)

```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`