File size: 4,967 Bytes
5b8270b
 
 
 
 
 
8c1d120
5b8270b
cd5cb90
5b8270b
 
 
8c1d120
5b8270b
 
cd5cb90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b07f8ef
 
 
cd5cb90
 
 
 
 
5b8270b
 
a2a7641
 
 
 
 
cd5cb90
a2a7641
cd5cb90
 
a2a7641
 
 
 
 
cd5cb90
a2a7641
cd5cb90
 
a2a7641
 
 
f00ffec
 
 
 
 
 
5b8270b
cd5cb90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b8270b
c1b5a61
dbfc881
cd5cb90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f00ffec
cd5cb90
 
 
d830515
d68b183
cd5cb90
5b8270b
 
cd5cb90
 
 
d4cae39
 
cd5cb90
 
 
5b8270b
 
b07f8ef
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
import gradio as gr
import numpy as np
import spaces
import torch
import random
from PIL import Image
from diffusers import FluxKontextPipeline
from diffusers.utils import load_image
import requests

MAX_SEED = np.iinfo(np.int32).max

pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")

@spaces.GPU
def translate_albanian_to_english(text):
    """Translate Albanian to English using sepioo-facebook-translation API."""
    if not text.strip():
        return ""
    for attempt in range(2):
        try:
            response = requests.post(
                "https://hal1993-mdftranslation1234567890abcdef1234567890-fc073a6.hf.space/v1/translate",
                json={"from_language": "sq", "to_language": "en", "input_text": text},
                headers={"accept": "application/json", "Content-Type": "application/json"},
                timeout=5
            )
            response.raise_for_status()
            translated = response.json().get("translate", "")
            return translated
        except Exception as e:
            if attempt == 1:
                raise gr.Error(f"Përkthimi dështoi: {str(e)}")
    raise gr.Error("Përkthimi dështoi. Ju lutem provoni përsëri.")

@spaces.GPU
def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5, steps=12, progress=gr.Progress(track_tqdm=True)):
    """
    Perform image editing using the FLUX.1 Kontext pipeline.
    """
    # Translate Albanian prompt to English
    final_prompt = translate_albanian_to_english(prompt.strip()) if prompt.strip() else ""
    if not final_prompt:
        return None, seed, gr.Button(visible=False)

    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    
    if input_image:
        input_image = input_image.convert("RGB")
        image = pipe(
            image=input_image, 
            prompt=final_prompt,
            guidance_scale=guidance_scale,
            width=input_image.size[0],
            height=input_image.size[1],
            num_inference_steps=steps,
            generator=torch.Generator().manual_seed(seed),
        ).images[0]
    else:
        image = pipe(
            prompt=final_prompt,
            guidance_scale=guidance_scale,
            width=1024,
            height=1024,
            num_inference_steps=steps,
            generator=torch.Generator().manual_seed(seed),
        ).images[0]
    return image, seed, gr.Button(visible=True)

@spaces.GPU
def infer_example(input_image, prompt):
    image, seed, _ = infer(input_image, prompt)
    return image, seed

with gr.Blocks() as demo:
    gr.HTML("""
    <style>
    body::before {
        content: "";
        display: block;
        height: 320px;
        background-color: var(--body-background-fill);
    }
    button[aria-label="Fullscreen"], button[aria-label="Fullscreen"]:hover {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        pointer-events: none !important;
    }
    button[aria-label="Share"], button[aria-label="Share"]:hover {
        display: none !important;
    }
    button[aria-label="Download"] {
        transform: scale(3);
        transform-origin: top right;
        margin: 0 !important;
        padding: 6px !important;
    }
    </style>
    """)

    gr.Markdown("# Modifiko imazhet")
    gr.Markdown("Modifiko imazhet ne menyre universale ne baze te pershkrimit")

    with gr.Column():
        input_image = gr.Image(label="Ngarko Imazhin për Editim", type="pil")
        prompt = gr.Textbox(
            label="Përshkrimi",
            placeholder="Shkruani përshkrimin këtu",
            lines=3
        )
        run_button = gr.Button(value="Gjenero")
        reuse_button = gr.Button("Rivendos këtë imazh", visible=False)
        # Hidden advanced settings
        seed = gr.Slider(
            minimum=0,
            maximum=MAX_SEED,
            step=1,
            value=0,
            visible=False
        )
        randomize_seed = gr.Checkbox(value=True, visible=False)
        guidance_scale = gr.Slider(
            minimum=1,
            maximum=10,
            step=0.1,
            value=2.5,
            visible=False
        )
        steps = gr.Slider(
            minimum=1,
            maximum=30,
            value=12,
            step=1,
            visible=False
        )

    with gr.Row():
        result = gr.Image(label="Imazhi i Gjeneruar", interactive=False)
    with gr.Row():
        reuse_button = gr.Button("Përdor imazhin e gjeneruar", visible=False)         

    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[input_image, prompt, seed, randomize_seed, guidance_scale, steps],
        outputs=[result, seed, reuse_button]
    )
    reuse_button.click(
        fn=lambda image: image,
        inputs=[result],
        outputs=[input_image]
    )

demo.launch(mcp_server=True)