File size: 9,260 Bytes
e547b24
 
 
 
 
 
09e6956
79927aa
e547b24
 
adb4ddb
e547b24
a843e21
fe61d0b
ad36cb3
 
8a9d912
fe61d0b
 
 
 
 
 
 
59998fa
fe61d0b
 
 
0dd6c57
fe61d0b
 
0ced833
 
 
a8022cc
 
ad36cb3
e547b24
 
 
 
149734c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103daba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3079d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad36cb3
6f5a32e
e547b24
 
 
 
9be63af
e547b24
 
 
087f7f1
d474366
07fd842
e547b24
 
ad36cb3
 
 
 
fe61d0b
 
ad36cb3
 
e547b24
6f5a32e
 
e547b24
 
 
 
 
 
 
e92be6e
07fd842
100c04b
4ed3443
27aa29e
 
100c04b
 
07fd842
087f7f1
 
 
27aa29e
087f7f1
 
 
398d75e
100c04b
e92be6e
6f5a32e
e547b24
 
6f5a32e
e547b24
 
 
02f8cfa
bc84ac0
02f8cfa
 
73f7edc
e547b24
 
8fbab9b
 
 
3079d5a
8886d40
4a22dd6
02f8cfa
 
 
4ed3443
 
d474366
4a188f9
 
 
ad36cb3
130e6a7
02f8cfa
 
 
 
e547b24
1d97628
e547b24
d82c582
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
import gradio as gr
import requests
import io
import random
import os
import time
import numpy as np
from PIL import Image, ImageEnhance
from deep_translator import GoogleTranslator
import json
from random import randint

# API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
API_URL = "black-forest-labs/FLUX.1-schnell"

mod_list = {
    "FLUX.1 Schnell": API_URL,
    "FLUX.1 Schnell | Face Realism": "prithivMLmods/Canopus-LoRA-Flux-FaceRealism",
    "FLUX.1 Schnell | Midjourney": "Jovie/Midjourney_Schnell",
    "FLUX.1 Dev": "black-forest-labs/FLUX.1-dev",
    "FLUX.1 Schnell | realism": "hugovntr/flux-schnell-realism",
    "FLUX.1 Schnell | MJ v6": "strangerzonehf/Flux-Midjourney-Mix2-LoRA",
    "FLUX.1 Dev | Flux Realism": "XLabs-AI/flux-RealismLora",
    "FLUX.1 Redux": "black-forest-labs/FLUX.1-Redux-dev",
    "FLUX.1 Dev | UltraRealism 2.0": "prithivMLmods/Canopus-LoRA-Flux-UltraRealism-2.0",
    "Lumina Image 2.0": "Alpha-VLLM/Lumina-Image-2.0",
    "FLUX.1 Dev | Vector Journey": "Shakker-Labs/FLUX.1-dev-LoRA-Vector-Journey",
    "FLUX.1 Dev | Deep Blue": "fffiloni/deep-blue-v2",
    "Realism Engine v1.0": "digiplay/RealismEngine_v1",
    "Absolute Reality v1.8.1": "digiplay/AbsoluteReality_v1.8.1",
    "FLUX.1 Dev | Midjourney Anime": "brushpenbob/flux-midjourney-anime",
    "FLUX.1 Dev | Pencil v2": "brushpenbob/flux-pencil-v2",
    "FLUX.1 Dev | Add Details": "Shakker-Labs/FLUX.1-dev-LoRA-add-details",
    "FLUX.1 Dev | Pastel Anime": "Raelina/Flux-Pastel-Anime",
    "FLUX.1 Dev | CartoonStyle": "Norod78/CartoonStyle-flux-lora",
    "Kwai Kolors": "Kwai-Kolors/Kolors"
}
API_TOKEN = os.getenv("HF_READ_TOKEN")
headers = {"Authorization": f"Bearer {API_TOKEN}"}
timeout = 100

def add_noise(image, intensity=25):
    """
    Добавляет цифровой шум к изображению.
    :param image: Изображение (PIL.Image)
    :param intensity: Интенсивность шума (от 0 до 255)
    :return: Изображение с шумом (PIL.Image)
    """
    # Преобразуем изображение в массив NumPy
    img_array = np.array(image)
    
    # Генерируем шум
    noise = np.random.randint(-intensity, intensity, img_array.shape, dtype=np.int32)
    
    # Добавляем шум к изображению
    noisy_array = np.clip(img_array + noise, 0, 255).astype(np.uint8)
    
    # Преобразуем массив обратно в изображение
    noisy_image = Image.fromarray(noisy_array)
    return noisy_image

def resize_and_crop(image, target_width=768, target_height=1024):
    """
    Подгоняет изображение под размер target_width x target_height с сохранением пропорций.
    Если изображение не соответствует соотношению сторон, обрезает его по бокам.
    :param image: Изображение (PIL.Image)
    :param target_width: Целевая ширина
    :param target_height: Целевая высота
    :return: Изображение с измененным размером (PIL.Image)
    """
    # Сохраняем исходные пропорции
    original_width, original_height = image.size
    target_ratio = target_width / target_height
    original_ratio = original_width / original_height

    # Масштабируем изображение с сохранением пропорций
    if original_ratio > target_ratio:
        # Если изображение шире, чем нужно, обрезаем по бокам
        new_height = target_height
        new_width = int(original_width * (target_height / original_height))
        resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
        left = (new_width - target_width) / 2
        top = 0
        right = (new_width + target_width) / 2
        bottom = target_height
    else:
        # Если изображение уже, чем нужно, обрезаем сверху и снизу
        new_width = target_width
        new_height = int(original_height * (target_width / original_width))
        resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
        left = 0
        top = (new_height - target_height) / 2
        right = target_width
        bottom = (new_height + target_height) / 2

    # Обрезаем изображение до целевого размера
    cropped_image = resized_image.crop((left, top, right, bottom))
    return cropped_image

def compress_image(image, quality=50):
    """
    Сжимает изображение и понижает его качество в формате JPEG.
    :param image: Изображение (PIL.Image)
    :param quality: Качество JPEG (от 1 до 100)
    :return: Сжатое изображение (PIL.Image)
    """
    # Сохраняем изображение в буфер с пониженным качеством
    buffer = io.BytesIO()
    image.save(buffer, format="JPEG", quality=quality)
    buffer.seek(0)
    
    # Загружаем изображение обратно из буфера
    compressed_image = Image.open(buffer)
    return compressed_image

def query(prompt, is_realistic, num_inference_steps, width, height, mod = None):
    if prompt == "" or prompt == None:
        return None

    key = random.randint(0, 999)
    
    API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    
    payload = {
        "inputs": prompt,
        "seed": random.randint(1, 1000000000),
        "parameters": {"num_inference_steps": num_inference_steps, "width": 1024, "height": 1024}
    }

    model = API_URL
    for mod_name, mod_link in mod_list.items():
        if (mod == mod_name):
            model = mod_link

    model = "https://api-inference.huggingface.co/models/" + model;
    
    response = requests.post(model, headers=headers, json=payload, timeout=timeout)
    if response.status_code != 200:
        print(f"Error: Failed to get image. Response status: {response.status_code}")
        print(f"Response content: {response.text}")
        if response.status_code == 503:
            raise gr.Error(f"{response.status_code} : The model is being loaded")
        raise gr.Error(f"{response.status_code}")
    
    try:
        image_bytes = response.content
        image = Image.open(io.BytesIO(image_bytes))

        image = resize_and_crop(image, width, height)

        if (is_realistic == True):
            image = add_noise(image, intensity = randint(50, 100) / 10)
            image = compress_image(image, randint(80, 90))

            enhancer = ImageEnhance.Contrast(image)
            image = enhancer.enhance(randint(75,80) / 100)
            
            # Изменение насыщенности
            enhancer = ImageEnhance.Color(image)
            image = enhancer.enhance(randint(80,90) / 100)
    
            # Изменение экспозиции (яркости)
            enhancer = ImageEnhance.Brightness(image)
            image = enhancer.enhance(randint(70,100) / 100)

        
        print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
        return image
    except Exception as e:
        print(f"Error when trying to open the image: {e}")
        return None

css = """
#app-container {
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
}
"""

test_prompt = """[amateur perspective of a house],
[on this house staying white russian female (if you can see it) with white t-shirt (if you can see it) on her body, with black jeans (if you can see it) on her legs, with lime sneakers (if you can see it) on her feet and looks aside (if you can see it)]
[night melancholic lighting]"""

with gr.Blocks(theme='gstaff/xkcd', css=css) as app:
    gr.HTML("<center><h1>FLUX.1-Schnell</h1></center>")
    with gr.Column(elem_id="app-container"):
        with gr.Row():
            with gr.Column(elem_id="prompt-container"):
                text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input", value=test_prompt)
                is_realistic = gr.Checkbox(label="Realistic filter", value=True)
                num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=4)
                with gr.Row():
                    width = gr.Slider(label="Width", minimum = 128, maximum = 1024, step = 32, value = 480)
                    height = gr.Slider(label="Height", minimum = 128, maximum = 1024, step = 32, value = 640)
                mod_choice = gr.Dropdown(list(mod_list.keys()), label="Mod")
                    
        with gr.Row():
            text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
        with gr.Row():
            image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
        
        text_button.click(query, inputs=[text_prompt, is_realistic, num_inference_steps, width, height, mod_choice], outputs=image_output)

app.launch(show_api=True, share=True)