File size: 3,094 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
---
title: Nano Banana Image Edit API
emoji: 🍌
colorFrom: pink
colorTo: indigo
sdk: docker
pinned: false
short_description: NanoBanana and Descratch Model
---

# 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

## 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"
}
```

### 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"
}
```

### 3. Get Result
**GET** `/result/{task_id}`

Get the status and result information for an editing task.

### 4. Get Result Image (File)
**GET** `/result/image/{result_image_id}`

Download the edited image as a file.

### 5. Get Result Image (Base64)
**GET** `/result/image/{result_image_id}/base64`

Get the edited image as a base64-encoded string.

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

Check API health and model status.

## Interactive API Documentation

Once the Space is running, visit:
- Swagger UI: `/docs`
- ReDoc: `/redoc`

## Usage Example (Python)

```python
import requests

# Upload image
with open("image.jpg", "rb") as f:
    response = requests.post(
        "https://your-space-url/upload",
        files={"file": f}
    )
upload_data = response.json()
image_id = upload_data["image_id"]

# Edit image
response = requests.post(
    "https://your-space-url/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"https://your-space-url/result/{task_id}")
result_data = response.json()
result_image_id = result_data["result_image_id"]

# Download edited image
response = requests.get(f"https://your-space-url/result/image/{result_image_id}")
with open("edited_image.png", "wb") as f:
    f.write(response.content)
```

## Model

This API uses the **Qwen/Qwen-Image-Edit** model from Hugging Face, which is open source under Apache 2.0 license.

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