File size: 16,490 Bytes
003d203
 
 
dc95e97
0e6c023
5919468
aa16383
dcfda89
3c62285
9c7d015
3aa0053
ad44b76
3aa0053
dc95e97
 
 
 
 
3aa0053
 
 
 
 
 
 
0e6c023
003d203
dc95e97
 
 
 
 
 
aa16383
dc95e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aa0053
 
 
 
 
 
 
 
 
aa16383
 
 
dcfda89
 
 
 
aa16383
dcfda89
 
 
 
 
aa16383
dcfda89
5fb3d3c
 
 
 
 
aa16383
 
 
ba5420f
aa16383
dcfda89
 
 
 
 
30b17a2
 
aa16383
 
dcfda89
aa16383
dc95e97
 
 
 
 
 
 
 
 
 
 
 
aa16383
9a3b780
aa16383
 
ba5420f
 
 
 
 
 
 
 
 
dc95e97
 
 
 
 
 
 
 
 
 
 
 
ba5420f
 
 
 
ccd0549
dc95e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aa0053
 
 
0e6c023
 
 
dc95e97
 
 
 
 
 
53ac342
2621247
 
dc95e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a181ad3
2ca2e3d
 
dc95e97
 
aa16383
dc95e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa16383
dc95e97
aa16383
2ca2e3d
c74d7e5
dc95e97
 
 
c74d7e5
dc95e97
 
 
2ca2e3d
dc95e97
9702cba
aa16383
 
 
dcfda89
 
dc95e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcfda89
dc95e97
 
 
dcfda89
dc95e97
6039455
aa16383
0e6c023
ba5420f
 
 
dc95e97
 
 
 
 
 
 
 
 
 
 
 
ba5420f
dc95e97
 
 
ba5420f
dc95e97
6039455
ba5420f
 
9c7d015
dc95e97
9c7d015
dc95e97
 
 
 
 
 
 
9c7d015
dc95e97
 
 
9c7d015
dc95e97
9c7d015
 
 
5919468
dc95e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e6c023
5919468
 
 
 
 
dc95e97
 
5919468
 
dc95e97
 
 
 
 
5919468
 
dc95e97
0e6c023
 
 
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import gradio as gr
import spaces
import torch
from loadimg import load_img  # Assuming loadimg.py exists with load_img function
from torchvision import transforms
from transformers import AutoModelForImageSegmentation, pipeline
from diffusers import FluxFillPipeline
from PIL import Image, ImageOps
import numpy as np
from simple_lama_inpainting import SimpleLama
from contextlib import contextmanager
import gc

# --- Add Translation Imports ---
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast


# --- Utility Functions ---
@contextmanager
def float32_high_matmul_precision():
    torch.set_float32_matmul_precision("high")
    try:
        yield
    finally:
        torch.set_float32_matmul_precision("highest")


# --- Model Loading ---
# Use context manager for precision during model loading if needed
with float32_high_matmul_precision():
    pipe = FluxFillPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16
    ).to("cuda")

    birefnet = AutoModelForImageSegmentation.from_pretrained(
        "ZhengPeng7/BiRefNet", trust_remote_code=True
    ).to("cuda")

simple_lama = SimpleLama()  # Initialize Lama globally if used often

# --- Translation Model and Tokenizer Loading ---
translation_model_name = "facebook/mbart-large-50-many-to-many-mmt"
try:
    translation_model = MBartForConditionalGeneration.from_pretrained(
        translation_model_name
    ).to("cuda")  # Move to GPU
    translation_tokenizer = MBart50TokenizerFast.from_pretrained(translation_model_name)
except Exception as e:
    print(f"Error loading translation model/tokenizer: {e}")
    # Consider exiting or disabling the translation tab if loading fails
    translation_model = None
    translation_tokenizer = None

# --- Image Processing Functions ---

transform_image = transforms.Compose(
    [
        transforms.Resize((1024, 1024)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ]
)


def prepare_image_and_mask(
    image,
    padding_top=0,
    padding_bottom=0,
    padding_left=0,
    padding_right=0,
):
    image = load_img(image).convert("RGB")
    background = ImageOps.expand(
        image,
        border=(padding_left, padding_top, padding_right, padding_bottom),
        fill="white",
    )
    mask = Image.new("RGB", image.size, "black")
    mask = ImageOps.expand(
        mask,
        border=(padding_left, padding_top, padding_right, padding_bottom),
        fill="white",
    )
    return background, mask


def outpaint(
    image,
    padding_top=0,
    padding_bottom=0,
    padding_left=0,
    padding_right=0,
    prompt="",
    num_inference_steps=28,
    guidance_scale=50,
):
    background, mask = prepare_image_and_mask(
        image, padding_top, padding_bottom, padding_left, padding_right
    )
    with (
        float32_high_matmul_precision()
    ):  # Apply precision context if needed for inference
        result = pipe(
            prompt=prompt,
            height=background.height,
            width=background.width,
            image=background,
            mask_image=mask,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
        ).images[0]
    result = result.convert("RGBA")
    return result


def inpaint(
    image,
    mask,
    prompt="",
    num_inference_steps=28,
    guidance_scale=50,
):
    background = image.convert("RGB")
    mask = mask.convert("L")
    with (
        float32_high_matmul_precision()
    ):  # Apply precision context if needed for inference
        result = pipe(
            prompt=prompt,
            height=background.height,
            width=background.width,
            image=background,
            mask_image=mask,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
        ).images[0]
    result = result.convert("RGBA")
    return result


def rmbg(image=None, url=None):
    if image is None and url:
        # Basic check for URL format, improve as needed
        if not url.startswith(("http://", "https://")):
            return "Invalid URL provided."
        image = url  # load_img should handle URLs if configured correctly
    elif image is None:
        return "Please provide an image or a URL."

    try:
        image_pil = load_img(image).convert("RGB")
    except Exception as e:
        return f"Error loading image: {e}"

    image_size = image_pil.size
    input_images = transform_image(image_pil).unsqueeze(0).to("cuda")
    with float32_high_matmul_precision():
        with torch.no_grad():
            preds = birefnet(input_images)[-1].sigmoid().cpu()
    pred = preds[0].squeeze()
    pred_pil = transforms.ToPILImage()(pred)
    mask = pred_pil.resize(image_size)
    image_pil.putalpha(mask)
    # Clean up GPU memory if needed
    del input_images, preds, pred
    torch.cuda.empty_cache()
    gc.collect()
    return image_pil


def erase(image=None, mask=None):
    if image is None or mask is None:
        return "Please provide both an image and a mask."
    try:
        image_pil = load_img(image)
        mask_pil = load_img(mask).convert("L")
        result = simple_lama(image_pil, mask_pil)
        # Clean up
        gc.collect()
        return result
    except Exception as e:
        return f"Error during erase operation: {e}"


# --- Translation Functionality ---

# Language Mapping
lang_data = {
    "Arabic": "ar_AR",
    "Czech": "cs_CZ",
    "German": "de_DE",
    "English": "en_XX",
    "Spanish": "es_XX",
    "Estonian": "et_EE",
    "Finnish": "fi_FI",
    "French": "fr_XX",
    "Gujarati": "gu_IN",
    "Hindi": "hi_IN",
    "Italian": "it_IT",
    "Japanese": "ja_XX",
    "Kazakh": "kk_KZ",
    "Korean": "ko_KR",
    "Lithuanian": "lt_LT",
    "Latvian": "lv_LV",
    "Burmese": "my_MM",
    "Nepali": "ne_NP",
    "Dutch": "nl_XX",
    "Romanian": "ro_RO",
    "Russian": "ru_RU",
    "Sinhala": "si_LK",
    "Turkish": "tr_TR",
    "Vietnamese": "vi_VN",
    "Chinese": "zh_CN",
    "Afrikaans": "af_ZA",
    "Azerbaijani": "az_AZ",
    "Bengali": "bn_IN",
    "Persian": "fa_IR",
    "Hebrew": "he_IL",
    "Croatian": "hr_HR",
    "Indonesian": "id_ID",
    "Georgian": "ka_GE",
    "Khmer": "km_KH",
    "Macedonian": "mk_MK",
    "Malayalam": "ml_IN",
    "Mongolian": "mn_MN",
    "Marathi": "mr_IN",
    "Polish": "pl_PL",
    "Pashto": "ps_AF",
    "Portuguese": "pt_XX",
    "Swedish": "sv_SE",
    "Swahili": "sw_KE",
    "Tamil": "ta_IN",
    "Telugu": "te_IN",
    "Thai": "th_TH",
    "Tagalog": "tl_XX",
    "Ukrainian": "uk_UA",
    "Urdu": "ur_PK",
    "Xhosa": "xh_ZA",
    "Galician": "gl_ES",
    "Slovene": "sl_SI",
}
language_names = sorted(list(lang_data.keys()))


def translate_text(text_to_translate, source_language_name, target_language_name):
    """
    Translates text using the loaded mBART model.
    """
    if translation_model is None or translation_tokenizer is None:
        return "Translation model not loaded. Cannot perform translation."
    if not text_to_translate:
        return "Please enter text to translate."
    if not source_language_name:
        return "Please select a source language."
    if not target_language_name:
        return "Please select a target language."

    try:
        source_lang_code = lang_data[source_language_name]
        target_lang_code = lang_data[target_language_name]

        translation_tokenizer.src_lang = source_lang_code
        encoded_text = translation_tokenizer(text_to_translate, return_tensors="pt").to(
            "cuda"
        )  # Move input to GPU
        target_lang_id = translation_tokenizer.lang_code_to_id[target_lang_code]

        # Generate translation on GPU
        with torch.no_grad():  # Use no_grad for inference
            generated_tokens = translation_model.generate(
                **encoded_text, forced_bos_token_id=target_lang_id, max_length=200
            )

        translated_text = translation_tokenizer.batch_decode(
            generated_tokens, skip_special_tokens=True
        )

        # Clean up GPU memory
        del encoded_text, generated_tokens
        torch.cuda.empty_cache()
        gc.collect()

        return translated_text[0]

    except KeyError as e:
        return f"Error: Language code not found for {e}. Check language mappings."
    except Exception as e:
        print(f"Translation error: {e}")
        # Clean up GPU memory on error too
        torch.cuda.empty_cache()
        gc.collect()
        return f"An error occurred during translation: {e}"


# --- Main Function Router (for image tasks) ---
# Note: Translation uses its own function directly
@spaces.GPU(duration=120)  # Keep GPU decorator if needed for image tasks
def main(*args):
    api_num = args[0]
    args = args[1:]
    gc.collect()  # Try to collect garbage before starting task
    torch.cuda.empty_cache()  # Clear cache before starting task

    result = None
    try:
        if api_num == 1:
            result = rmbg(*args)
        elif api_num == 2:
            result = outpaint(*args)
        elif api_num == 3:
            result = inpaint(*args)
        # elif api_num == 4: # Keep commented out as in original
        #     return mask_generation(*args)
        elif api_num == 5:
            result = erase(*args)
        else:
            result = "Invalid API number."
    except Exception as e:
        print(f"Error in main task routing (api_num={api_num}): {e}")
        result = f"An error occurred: {e}"
    finally:
        # Ensure memory cleanup happens even if there's an error
        gc.collect()
        torch.cuda.empty_cache()

    return result


# --- Define Gradio Interfaces for Each Tab ---

# Image Task Tabs
rmbg_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(1, interactive=False, visible=False),  # Hide API number
        gr.Image(label="Input Image", type="pil", sources=["upload", "clipboard"]),
        gr.Text(label="Or Image URL (optional)"),
    ],
    outputs=gr.Image(label="Output Image", type="pil"),
    title="Remove Background",
    description="Upload an image or provide a URL to remove its background.",
    api_name="rmbg",
    # examples=[[1, "./assets/sample_rmbg.png", ""]], # Update example path if needed
    cache_examples=False,
)

outpaint_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(2, interactive=False, visible=False),
        gr.Image(label="Input Image", type="pil", sources=["upload", "clipboard"]),
        gr.Number(value=0, label="Padding Top (pixels)"),
        gr.Number(value=0, label="Padding Bottom (pixels)"),
        gr.Number(value=0, label="Padding Left (pixels)"),
        gr.Number(value=0, label="Padding Right (pixels)"),
        gr.Text(
            label="Prompt (optional)",
            info="Describe what to fill the extended area with",
        ),
        gr.Slider(
            minimum=10, maximum=100, step=1, value=28, label="Inference Steps"
        ),  # Use slider for steps
        gr.Slider(
            minimum=1, maximum=100, step=1, value=50, label="Guidance Scale"
        ),  # Use slider for guidance
    ],
    outputs=gr.Image(label="Outpainted Image", type="pil"),
    title="Outpainting",
    description="Extend an image by adding padding and filling the new area using a diffusion model.",
    api_name="outpainting",
    # examples=[[2, "./assets/rocket.png", 100, 0, 0, 0, "", 28, 50]], # Update example path
    cache_examples=False,
)

inpaint_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(3, interactive=False, visible=False),
        gr.Image(label="Input Image", type="pil", sources=["upload", "clipboard"]),
        gr.Image(
            label="Mask Image (White=Inpaint Area)",
            type="pil",
            sources=["upload", "clipboard"],
        ),
        gr.Text(
            label="Prompt (optional)", info="Describe what to fill the masked area with"
        ),
        gr.Slider(minimum=10, maximum=100, step=1, value=28, label="Inference Steps"),
        gr.Slider(minimum=1, maximum=100, step=1, value=50, label="Guidance Scale"),
    ],
    outputs=gr.Image(label="Inpainted Image", type="pil"),
    title="Inpainting",
    description="Fill in the white areas of a mask applied to an image using a diffusion model.",
    api_name="inpaint",
    # examples=[[3, "./assets/rocket.png", "./assets/Inpainting_mask.png", "", 28, 50]], # Update example paths
    cache_examples=False,
)

erase_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(5, interactive=False, visible=False),
        gr.Image(label="Input Image", type="pil", sources=["upload", "clipboard"]),
        gr.Image(
            label="Mask Image (White=Erase Area)",
            type="pil",
            sources=["upload", "clipboard"],
        ),
    ],
    outputs=gr.Image(label="Result Image", type="pil"),
    title="Erase Object (LAMA)",
    description="Erase objects from an image based on a mask using the LaMa inpainting model.",
    api_name="erase",
    # examples=[[5, "./assets/rocket.png", "./assets/Inpainting_mask.png"]], # Update example paths
    cache_examples=False,
)


# --- Define Translation Tab using gr.Blocks ---
with gr.Blocks() as translation_tab:
    gr.Markdown(
        """
        ## Multilingual Translation (mBART-50)
        Translate text between 50 different languages.
        Select the source and target languages, enter your text, and click Translate.
        """
    )
    with gr.Row():
        with gr.Column(scale=1):
            source_lang_dropdown = gr.Dropdown(
                label="Source Language",
                choices=language_names,
                info="Select the language of your input text.",
            )
            target_lang_dropdown = gr.Dropdown(
                label="Target Language",
                choices=language_names,
                info="Select the language you want to translate to.",
            )
        with gr.Column(scale=2):
            input_textbox = gr.Textbox(
                label="Text to Translate",
                lines=6,  # Increased lines
                placeholder="Enter text here...",
            )
            translate_button = gr.Button(
                "Translate", variant="primary"
            )  # Added variant
            output_textbox = gr.Textbox(
                label="Translated Text",
                lines=6,  # Increased lines
                interactive=False,  # Make output read-only
            )

    # Connect Components to the translation function directly
    translate_button.click(
        fn=translate_text,
        inputs=[input_textbox, source_lang_dropdown, target_lang_dropdown],
        outputs=output_textbox,
        api_name="translate",  # Add API name for the translation endpoint
    )

    # Add Translation Examples
    gr.Examples(
        examples=[
            [
                "संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है",
                "Hindi",
                "French",
            ],
            [
                "الأمين العام للأمم المتحدة يقول إنه لا يوجد حل عسكري في سوريا.",
                "Arabic",
                "English",
            ],
            [
                "Le chef de l'ONU affirme qu'il n'y a pas de solution militaire en Syrie.",
                "French",
                "German",
            ],
            ["Hello world! How are you today?", "English", "Spanish"],
            ["Guten Tag!", "German", "Japanese"],
            ["これはテストです", "Japanese", "English"],
        ],
        inputs=[input_textbox, source_lang_dropdown, target_lang_dropdown],
        outputs=output_textbox,
        fn=translate_text,
        cache_examples=False,
    )

# --- Combine all tabs ---
demo = gr.TabbedInterface(
    [
        rmbg_tab,
        outpaint_tab,
        inpaint_tab,
        erase_tab,
        translation_tab,  # Add the translation tab
        #  sam2_tab, # Keep commented out
    ],
    [
        "Remove Background",  # Tab title
        "Outpainting",  # Tab title
        "Inpainting",  # Tab title
        "Erase (LAMA)",  # Tab title
        "Translate",  # Tab title for translation
        #  "sam2",
    ],
    title="Image & Text Utilities (GPU)",  # Updated title
)

demo.launch()