File size: 12,894 Bytes
bbc89f6
 
 
 
025cc15
 
 
 
 
 
 
 
 
bbc89f6
 
 
025cc15
bbc89f6
025cc15
 
bbc89f6
 
025cc15
 
 
 
 
bbc89f6
025cc15
 
 
 
bbc89f6
a1e077b
 
 
 
 
 
 
 
 
 
 
 
 
025cc15
 
bbc89f6
025cc15
bbc89f6
 
 
 
 
 
 
a1e077b
 
 
bbc89f6
025cc15
a1e077b
 
b16249c
 
 
 
025cc15
b16249c
a1e077b
 
bbc89f6
a1e077b
bbc89f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1e077b
 
bbc89f6
 
 
 
 
 
 
 
b16249c
 
 
 
 
 
 
025cc15
bbc89f6
a1e077b
bbc89f6
 
 
a1e077b
 
 
 
 
 
 
 
 
 
 
 
 
 
bbc89f6
a1e077b
 
 
 
 
 
 
 
bbc89f6
 
 
a1e077b
bbc89f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
025cc15
bbc89f6
025cc15
bbc89f6
 
 
 
 
 
 
025cc15
bbc89f6
 
 
 
025cc15
 
bbc89f6
 
025cc15
bbc89f6
 
 
7a6ca6b
bbc89f6
 
 
 
 
 
 
7a6ca6b
025cc15
bbc89f6
 
 
025cc15
bbc89f6
 
7a6ca6b
 
025cc15
 
bbc89f6
 
 
 
 
 
 
 
025cc15
bbc89f6
 
025cc15
 
bbc89f6
 
 
 
87a24f0
025cc15
87a24f0
 
 
 
 
 
 
a1e077b
bbc89f6
025cc15
bbc89f6
 
 
 
 
 
 
 
 
 
 
 
a1e077b
 
 
 
 
 
 
 
bbc89f6
 
 
 
a1e077b
bbc89f6
 
 
025cc15
 
 
bbc89f6
 
 
025cc15
 
 
bbc89f6
025cc15
 
bbc89f6
 
 
 
 
b16249c
bbc89f6
025cc15
bbc89f6
b16249c
bbc89f6
 
a1e077b
025cc15
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
import os
import zipfile
from pathlib import Path

import gradio as gr

from database import (
    get_user_credits,
    update_user_credits,
    get_lora_models_info,
    get_user_lora_models
)

from services.image_generation import generate_image
from services.train_lora import lora_pipeline
from utils.image_utils import url_to_pil_image
from utils.file_utils import load_file_content

LORA_MODELS = get_lora_models_info()
if not isinstance(LORA_MODELS, list):
    raise ValueError("Expected loras_models to be a list of dictionaries.")

BASE_DIR = Path(__file__).parent
LOGIN_CSS_PATH = BASE_DIR / 'static/css/login.css'
MAIN_CSS_PATH = BASE_DIR / 'static/css/main.css'
LANDING_HTML_PATH = BASE_DIR / 'static/html/landing.html'
MAIN_HEADER_PATH = BASE_DIR / 'static/html/main_header.html'

LOGIN_CSS = load_file_content(LOGIN_CSS_PATH)
MAIN_CSS = load_file_content(MAIN_CSS_PATH)
LANDING_PAGE = load_file_content(LANDING_HTML_PATH)
MAIN_HEADER = load_file_content(MAIN_HEADER_PATH)

def load_user_models(request: gr.Request):
    user = request.session.get('user')
    print(user)
    if user:
        user_models = get_user_lora_models(user['id'])
        if user_models:
            return [(item.get("image_url", "assets/logo.jpg"), item["lora_name"]) for item in user_models]
    return []

def update_selection(evt: gr.SelectData, gallery_type: str, width, height):
    if gallery_type == "user":
        selected_lora = {"lora_name": "custom", "trigger_word": "custom"}
    else:
        selected_lora = LORA_MODELS[evt.index]
    new_placeholder = f"Enter a prompt for {selected_lora['lora_name']}"
    trigger_word = selected_lora["trigger_word"]
    updated_text = f"#### Trigger Word: {trigger_word} ✨"
    
    if "aspect" in selected_lora:
        if selected_lora["aspect"] == "portrait":
            width, height = 768, 1024
        elif selected_lora["aspect"] == "landscape":
            width, height = 1024, 768

    return gr.update(placeholder=new_placeholder), updated_text, evt.index, width, height, gallery_type

def compress_and_train(request: gr.Request, files, model_name, trigger_word, train_steps, lora_rank, batch_size, learning_rate):
    if not files:
        return "No Images. Please, upload some images to start training"
    
    user = request.session.get('user')
    
    _, training_credits = get_user_credits(user['id'])
    
    if training_credits <= 0:
        raise gr.Error("You ran out of credtis. Please buy more to continue")
    
    if not user:
        raise gr.Error("User not authenticated. Please log in.")

    user_id = user['id']
    # Create a directory in the user's home folder
    output_dir = os.path.expanduser("~/gradio_training_data")
    os.makedirs(output_dir, exist_ok=True)

    # Create a zip file in the output directory
    zip_path = os.path.join(output_dir, "training_data.zip")
    
    with zipfile.ZipFile(zip_path, 'w') as zipf:
        for file_info in files:
            file_path = file_info[0]  # The first element of the tuple is the file path
            file_name = os.path.basename(file_path)
            zipf.write(file_path, file_name)
    
    print(f"Zip file created at: {zip_path}")
        
    print(f'[INFO] Procesando {trigger_word}')
    # Now call the train_lora function with the zip file path
    result = lora_pipeline(user_id,
                            zip_path, 
                            model_name, 
                            trigger_word=trigger_word, 
                            steps=train_steps, 
                            lora_rank=lora_rank, 
                            batch_size=batch_size, 
                            autocaption=True, 
                            learning_rate=learning_rate)
    
    new_training_credits = training_credits - 1
    update_user_credits(user['id'], user['generation_credits'], new_training_credits)
    
    # Update session data
    user['training_credits'] = new_training_credits
    request.session['user'] = user
    
    return gr.Info("Your model is training. In about 20 minutes, it will be ready for you to test in 'Generation"), new_training_credits
            
def run_lora(request: gr.Request, prompt, cfg_scale, steps, selected_index, selected_gallery, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
    user = request.session.get('user')
    if not user:
        raise gr.Error("User not authenticated. Please log in.")
    lora_models = get_user_lora_models(user['id'])
    print(f'Selected gallery: {selected_gallery}')
    if selected_gallery == "user":
        lora_models = get_user_lora_models(user['id'])
        print('Using user models')
    else:  # public
        lora_models = get_lora_models_info()
        print('Using public models')
    print(f'Selected index: {selected_index}')
    if selected_index is None:
        selected_lora = None
    else:
        selected_lora = lora_models[selected_index]

    generation_credits, _ = get_user_credits(user['id'])
    if selected_lora:
        print(f"Selected Lora: {selected_lora['lora_name']}")
        model_name = selected_lora['lora_name']
        use_default = False
    else:
        model_name = "black-forest-labs/flux-pro"
        print(f"Using default Lora: {model_name}")
        use_default = True
    if generation_credits <= 0:
        raise gr.Error("Ya no tienes creditos disponibles. Compra para continuar.")
    
    image_url = generate_image(model_name, prompt, steps, cfg_scale, width, height, lora_scale, progress, use_default)
    image = url_to_pil_image(image_url)
    
    # Update user's credits
    new_generation_credits = generation_credits - 1
    update_user_credits(user['id'], new_generation_credits, user['train_credits'])
    
    # Update session data
    user['generation_credits'] = new_generation_credits
    request.session['user'] = user
    
    print(f"Generation credits remaining: {new_generation_credits}")
    
    return image, new_generation_credits

def display_credits(request: gr.Request):
    user = request.session.get('user')
    if user:
        generation_credits, train_credits = get_user_credits(user['id'])
        return generation_credits, train_credits
    return 0, 0

def load_greet_and_credits(request: gr.Request):
    greeting = greet(request)
    generation_credits, train_credits = display_credits(request)
    return greeting, generation_credits, train_credits
    
def greet(request: gr.Request):
    user = request.session.get('user')
    if user:
        with gr.Column():  
            with gr.Row():
                greeting = f"Hola 👋 {user['given_name']}!"
            return f"{greeting}\n"
    return "OBTU AI. Please log in."

with gr.Blocks(theme=gr.themes.Soft(), css=LOGIN_CSS) as login_demo:
    with gr.Column(elem_id="google-btn-container", elem_classes="google-btn-container svelte-vt1mxs gap"):
        btn = gr.Button("Sign In with Google", elem_classes="login-with-google-btn")
    _js_redirect = """
    () => {
        url = '/login' + window.location.search;
        window.open(url, '_blank');
    }
    """
    btn.click(None, js=_js_redirect)
    gr.HTML(LANDING_PAGE)
        

header = '<script src="https://cdn.lordicon.com/lordicon.js"></script>'

with gr.Blocks(theme=gr.themes.Soft(), head=header, css=MAIN_CSS) as main_demo:
    title = gr.HTML(MAIN_HEADER)
    
    with gr.Column(elem_id="logout-btn-container"):
        gr.Button("Logout", link="/logout", elem_id="logout_btn")

    
    greetings = gr.Markdown("Loading user information...")
    
    selected_index = gr.State(None)
    
    with gr.Row():
        with gr.Column():
            generation_credits_display = gr.Number(label="Generation Credits", precision=0, interactive=False)
        with gr.Column():
            train_credits_display = gr.Number(label="Training Credits", precision=0, interactive=False)
        with gr.Column():
            gr.Button("Buy Credits 💳", link="/buy_credits")
    

    with gr.Tabs():
        with gr.TabItem('Create'):
            with gr.Row():
                with gr.Column(scale=3):
                    prompt = gr.Textbox(label="Prompt", 
                                        lines=1, 
                                        placeholder="Enter Your Prompt to start creating 📷", 
                                        info='Some public models may experience longer processing times due to server availability and queue management.')
                with gr.Column(scale=1, elem_id="gen_column"):
                    generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
            
            with gr.Row():
                with gr.Column(scale=4):
                    result = gr.Image(label="Imagen Generada")
                
                with gr.Column(scale=3):
                    with gr.Accordion("Public Models"):
                        selected_info = gr.Markdown("")
                        gallery = gr.Gallery(
                            [(item["image_url"], item["model_name"]) for item in LORA_MODELS],
                            label="Public Models",
                            allow_preview=False,
                            columns=3,
                            elem_id="gallery"
                        )
                        
                    with gr.Accordion("Your Models"):
                        user_model_gallery = gr.Gallery(
                            label="Galeria de Modelos",
                            allow_preview=False,
                            columns=3,
                            elem_id="galley"
                        )
                        
            gallery_type = gr.State("Public")

            with gr.Accordion("Advanced Settings", open=False):
                with gr.Row():
                    cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
                    steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
                with gr.Row():
                    width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
                    height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
                with gr.Row():
                    randomize_seed = gr.Checkbox(True, label="Randomize seed")
                    lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=0.95)

            gallery.select(
                update_selection,
                inputs=[gr.State("public"), width, height],
                outputs=[prompt, selected_info, selected_index, width, height, gallery_type]
            )
            
            user_model_gallery.select(
                update_selection,
                inputs=[gr.State("user"), width, height],
                outputs=[prompt, selected_info, selected_index, width, height, gallery_type]
            )
            gr.on(
                triggers=[generate_button.click, prompt.submit],
                fn=run_lora,
                inputs=[prompt, cfg_scale, steps, selected_index, gallery_type, width, height, lora_scale],
                outputs=[result, generation_credits_display]
            )

        with gr.TabItem("Train"):
            gr.Markdown("# Train your own model 🧠")
            gr.Markdown("In this section, you can train your own model using your images.")
            with gr.Row():
                with gr.Column():
                    train_dataset = gr.Gallery(columns=4, interactive=True, label="Tus Imagenes")
                    model_name = gr.Textbox(label="Model Name",)
                    trigger_word = gr.Textbox(label="Trigger Word", 
                                              info="This will be a keyword to later instruct the model when to use these new capabilities we're going to teach it", 
                                              )
                    train_button = gr.Button("Start Training")
            with gr.Accordion("Advanced Settings", open=False):
                train_steps = gr.Slider(label="Training Steps", minimum=100, maximum=10000, step=100, value=1000)
                lora_rank = gr.Number(label='lora_rank', value=16)
                batch_size = gr.Number(label='batch_size', value=1)
                learning_rate = gr.Number(label='learning_rate', value=0.0004)
                training_status = gr.Textbox(label="Training Status")
                
            train_button.click(
                compress_and_train,
                inputs=[train_dataset, model_name, trigger_word, train_steps, lora_rank, batch_size, learning_rate],
                outputs=[training_status,train_credits_display]
            )
                
        main_demo.load(load_user_models, None, user_model_gallery)
        main_demo.load(load_greet_and_credits, None, [greetings, generation_credits_display, train_credits_display])