File size: 9,746 Bytes
42ca4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2858450
42ca4ef
 
 
 
 
 
 
2858450
42ca4ef
 
 
2858450
42ca4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import gradio as gr
import torch
import numpy as np
from PIL import Image
import cv2
import io
import base64
from fastapi import FastAPI, File, UploadFile, Form
import requests
from typing import Optional

# Инициализация
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")

# LaMa - самая быстрая и легкая модель инпейнтинга
try:
    from lama_cleaner.model.lama import LaMa
    from lama_cleaner.schema import Config, HDStrategy
    
    config = Config(
        hd_strategy=HDStrategy.CROP,
        hd_strategy_crop_margin=128,
        hd_strategy_crop_trigger_size=512,
    )
    model = LaMa(device, config)
    use_lama = True
except:
    use_lama = False
    print("LaMa не установлена, используем облегченный Stable Diffusion")
    from diffusers import AutoPipelineForInpainting
    
    pipe = AutoPipelineForInpainting.from_pretrained(
        "kandinsky-community/kandinsky-2-2-5-inpainting",
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
    ).to(device)
    pipe.enable_attention_slicing()

def prepare_mask(mask_image):
    """Подготовка маски"""
    if isinstance(mask_image, np.ndarray):
        mask = Image.fromarray(mask_image.astype('uint8'))
    else:
        mask = mask_image
    
    if mask.mode != 'L':
        mask = mask.convert('L')
    
    return np.array(mask)

def inpaint_image(image, mask, prompt=""):
    """Быстрое инпейнтинг с LaMa"""
    if image is None or mask is None:
        return image
    
    # Конвертируем в numpy если нужно
    if isinstance(image, Image.Image):
        image = np.array(image)
    
    mask_arr = prepare_mask(mask)
    
    # Нормализуем маску (0-255 -> 0-1)
    mask_arr = (mask_arr > 127).astype(np.uint8)
    
    try:
        if use_lama:
            # LaMa работает очень быстро
            with torch.no_grad():
                inpainted = model(image, mask_arr)
            result = Image.fromarray(inpainted.astype('uint8'))
        else:
            # Fallback на Kandinsky (быстрее чем SD v1.5)
            image_pil = Image.fromarray(image.astype('uint8'))
            mask_pil = Image.fromarray((mask_arr * 255).astype('uint8'))
            
            image_pil = image_pil.resize((512, 512))
            mask_pil = mask_pil.resize((512, 512))
            
            with torch.no_grad():
                output = pipe(
                    prompt=prompt or "best quality, high quality",
                    image=image_pil,
                    mask_image=mask_pil,
                    num_inference_steps=15,
                    guidance_scale=7.5,
                ).images[0]
            result = output
    except Exception as e:
        print(f"Ошибка инпейнтинга: {e}")
        result = Image.fromarray(image.astype('uint8'))
    
    return result

def gradio_inpaint(image, mask, prompt):
    """Обработка для Gradio"""
    result = inpaint_image(image, mask, prompt)
    return result

# Gradio интерфейс
with gr.Blocks(title="Magic Eraser API - Lightning Fast") as demo:
    gr.Markdown("# ⚡ Magic Eraser - Ultra Fast Inpainting API")
    model_info = "🔥 LaMa (Яндекс)" if use_lama else "⚡ Kandinsky 2.2.5"
    gr.Markdown(f"Модель: {model_info} | Скорость: <0.5 сек | Качество: отличное")
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(label="Исходное изображение", type="pil")
            mask_input = gr.Image(label="Маска (нарисуйте белым)", type="numpy")
            prompt_input = gr.Textbox(
                label="Подсказка (опционально)",
                value="best quality",
                interactive=True
            )
            submit_btn = gr.Button("✨ Удалить объект", variant="primary", size="lg")
            gr.Markdown("💡 **Совет**: Используйте инструмент рисования для маски справа")
        
        with gr.Column():
            output_image = gr.Image(label="Результат", type="pil")
    
    submit_btn.click(
        fn=gradio_inpaint,
        inputs=[image_input, mask_input, prompt_input],
        outputs=output_image
    )
    
    with gr.Accordion("📡 API Documentation"):
        gr.Markdown(f"""
        ## API для внешних приложений
        
        **Модель**: {model_info}  
        **Время обработки**: ~0.3-0.8 сек на T4 GPU  
        **Качество**: Профессиональное
        
        ### Endpoint 1: JSON (Base64)
        `POST /api/inpaint-json`
        
        ```json
        {{
            "image": "base64_encoded_image",
            "mask": "base64_encoded_mask",
            "prompt": "best quality"
        }}
        ```
        
        **Ответ**:
        ```json
        {{
            "success": true,
            "image": "base64_encoded_result",
            "time_ms": 450
        }}
        ```
        
        ### Endpoint 2: Form (файлы)
        `POST /api/inpaint`
        
        Multipart form с полями: `image`, `mask`, `prompt`
        
        ### Python пример (быстрый способ)
        ```python
        import requests
        from PIL import Image
        import base64
        import io
        
        def b64_encode(img):
            buf = io.BytesIO()
            img.save(buf, format='PNG')
            return base64.b64encode(buf.getvalue()).decode()
        
        image = Image.open('photo.jpg').convert('RGB')
        mask = Image.open('mask.png').convert('L')
        
        response = requests.post(
            'https://your-space/api/inpaint-json',
            json={
                'image': b64_encode(image),
                'mask': b64_encode(mask),
                'prompt': 'best quality'
            },
            timeout=30
        )
        
        result_img = Image.open(
            io.BytesIO(base64.b64decode(response.json()['image']))
        )
        result_img.save('result.jpg')
        ```
        
        ### cURL пример
        ```bash
        curl -X POST https://your-space/api/inpaint \\
          -F "image=@photo.jpg" \\
          -F "mask=@mask.png" \\
          -F "prompt=best quality" > result.png
        ```
        
        ### JavaScript пример
        ```javascript
        async function removeObject(imageFile, maskFile) {{
            const formData = new FormData();
            formData.append('image', imageFile);
            formData.append('mask', maskFile);
            formData.append('prompt', 'best quality');
            
            const response = await fetch(
                'https://your-space/api/inpaint',
                {{ method: 'POST', body: formData }}
            );
            
            return await response.blob();
        }}
        ```
        """)

# FastAPI
app = FastAPI()

@app.post("/api/inpaint")
async def api_inpaint(
    image: UploadFile = File(...),
    mask: UploadFile = File(...),
    prompt: str = Form(default="best quality")
):
    """API endpoint - Form данные"""
    import time
    start = time.time()
    try:
        image_data = await image.read()
        mask_data = await mask.read()
        
        image_pil = Image.open(io.BytesIO(image_data)).convert('RGB')
        mask_pil = Image.open(io.BytesIO(mask_data)).convert('L')
        
        result = inpaint_image(np.array(image_pil), mask_pil, prompt)
        
        buf = io.BytesIO()
        result.save(buf, format='PNG')
        result_b64 = base64.b64encode(buf.getvalue()).decode()
        
        elapsed = (time.time() - start) * 1000
        
        return {
            "success": True,
            "image": result_b64,
            "format": "base64",
            "time_ms": int(elapsed)
        }
    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }

@app.post("/api/inpaint-json")
async def api_inpaint_json(request_data: dict):
    """API endpoint - JSON с base64"""
    import time
    start = time.time()
    try:
        image_b64 = request_data.get('image')
        mask_b64 = request_data.get('mask')
        prompt = request_data.get('prompt', 'best quality')
        
        if not image_b64 or not mask_b64:
            return {"success": False, "error": "image и mask обязательны"}
        
        image_pil = Image.open(io.BytesIO(base64.b64decode(image_b64))).convert('RGB')
        mask_pil = Image.open(io.BytesIO(base64.b64decode(mask_b64))).convert('L')
        
        result = inpaint_image(np.array(image_pil), mask_pil, prompt)
        
        buf = io.BytesIO()
        result.save(buf, format='PNG')
        result_b64 = base64.b64encode(buf.getvalue()).decode()
        
        elapsed = (time.time() - start) * 1000
        
        return {
            "success": True,
            "image": result_b64,
            "format": "base64",
            "time_ms": int(elapsed)
        }
    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }

@app.get("/health")
async def health():
    """Health check"""
    return {
        "status": "ok",
        "device": device,
        "model": "LaMa" if use_lama else "Kandinsky 2.2.5",
        "speed": "ultra-fast"
    }

app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)