Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -6,735 +6,14 @@ from fastapi import FastAPI
|
|
6 |
from starlette.middleware.sessions import SessionMiddleware
|
7 |
import sys
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
is_spaces = True if os.environ.get("SPACE_ID") else False
|
23 |
-
|
24 |
-
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
25 |
-
import sys
|
26 |
-
|
27 |
-
from dotenv import load_dotenv
|
28 |
-
|
29 |
-
load_dotenv()
|
30 |
-
|
31 |
-
# Add the current working directory to the Python path
|
32 |
-
sys.path.insert(0, os.getcwd())
|
33 |
-
|
34 |
-
import gradio as gr
|
35 |
-
from PIL import Image
|
36 |
-
import torch
|
37 |
-
import uuid
|
38 |
-
import shutil
|
39 |
-
import json
|
40 |
-
import yaml
|
41 |
-
from slugify import slugify
|
42 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
43 |
-
|
44 |
-
# Gradio app 설정
|
45 |
-
app = FastAPI()
|
46 |
-
app.add_middleware(SessionMiddleware, secret_key="your-secret-key")
|
47 |
-
|
48 |
-
if not is_spaces:
|
49 |
-
sys.path.insert(0, "ai-toolkit")
|
50 |
-
from toolkit.job import get_job
|
51 |
-
gr.OAuthProfile = None
|
52 |
-
gr.OAuthToken = None
|
53 |
-
|
54 |
-
|
55 |
-
MAX_IMAGES = 150
|
56 |
-
|
57 |
-
|
58 |
-
# Hugging Face 토큰 설정
|
59 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
60 |
-
if not HF_TOKEN:
|
61 |
-
raise ValueError("HF_TOKEN environment variable is not set")
|
62 |
-
|
63 |
-
|
64 |
-
if is_spaces:
|
65 |
-
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
66 |
-
import spaces
|
67 |
-
|
68 |
-
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
69 |
-
os.environ["HUGGING_FACE_HUB_TOKEN"] = HF_TOKEN
|
70 |
-
|
71 |
-
# HF API 초기화
|
72 |
-
api = HfApi(token=HF_TOKEN)
|
73 |
-
|
74 |
-
def load_captioning(uploaded_files, concept_sentence):
|
75 |
-
uploaded_images = [file for file in uploaded_files if not file.endswith('.txt')]
|
76 |
-
txt_files = [file for file in uploaded_files if file.endswith('.txt')]
|
77 |
-
txt_files_dict = {os.path.splitext(os.path.basename(txt_file))[0]: txt_file for txt_file in txt_files}
|
78 |
-
updates = []
|
79 |
-
if len(uploaded_images) <= 1:
|
80 |
-
raise gr.Error(
|
81 |
-
"Please upload at least 2 images to train your model (the ideal number with default settings is between 4-30)"
|
82 |
-
)
|
83 |
-
elif len(uploaded_images) > MAX_IMAGES:
|
84 |
-
raise gr.Error(f"For now, only {MAX_IMAGES} or less images are allowed for training")
|
85 |
-
# Update for the captioning_area
|
86 |
-
# for _ in range(3):
|
87 |
-
updates.append(gr.update(visible=True))
|
88 |
-
# Update visibility and image for each captioning row and image
|
89 |
-
for i in range(1, MAX_IMAGES + 1):
|
90 |
-
# Determine if the current row and image should be visible
|
91 |
-
visible = i <= len(uploaded_images)
|
92 |
-
|
93 |
-
# Update visibility of the captioning row
|
94 |
-
updates.append(gr.update(visible=visible))
|
95 |
-
|
96 |
-
# Update for image component - display image if available, otherwise hide
|
97 |
-
image_value = uploaded_images[i - 1] if visible else None
|
98 |
-
updates.append(gr.update(value=image_value, visible=visible))
|
99 |
-
|
100 |
-
corresponding_caption = False
|
101 |
-
if(image_value):
|
102 |
-
base_name = os.path.splitext(os.path.basename(image_value))[0]
|
103 |
-
print(base_name)
|
104 |
-
print(image_value)
|
105 |
-
if base_name in txt_files_dict:
|
106 |
-
print("entrou")
|
107 |
-
with open(txt_files_dict[base_name], 'r') as file:
|
108 |
-
corresponding_caption = file.read()
|
109 |
-
|
110 |
-
# Update value of captioning area
|
111 |
-
text_value = corresponding_caption if visible and corresponding_caption else "[trigger]" if visible and concept_sentence else None
|
112 |
-
updates.append(gr.update(value=text_value, visible=visible))
|
113 |
-
|
114 |
-
# Update for the sample caption area
|
115 |
-
updates.append(gr.update(visible=True))
|
116 |
-
# Update prompt samples
|
117 |
-
updates.append(gr.update(placeholder=f'A portrait of person in a bustling cafe {concept_sentence}', value=f'A person in a bustling cafe {concept_sentence}'))
|
118 |
-
updates.append(gr.update(placeholder=f"A mountainous landscape in the style of {concept_sentence}"))
|
119 |
-
updates.append(gr.update(placeholder=f"A {concept_sentence} in a mall"))
|
120 |
-
return updates
|
121 |
-
|
122 |
-
def hide_captioning():
|
123 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
124 |
-
|
125 |
-
def create_dataset(*inputs):
|
126 |
-
print("Creating dataset")
|
127 |
-
images = inputs[0]
|
128 |
-
destination_folder = str(f"datasets/{uuid.uuid4()}")
|
129 |
-
if not os.path.exists(destination_folder):
|
130 |
-
os.makedirs(destination_folder)
|
131 |
-
|
132 |
-
jsonl_file_path = os.path.join(destination_folder, "metadata.jsonl")
|
133 |
-
with open(jsonl_file_path, "a") as jsonl_file:
|
134 |
-
for index, image in enumerate(images):
|
135 |
-
new_image_path = shutil.copy(image, destination_folder)
|
136 |
-
|
137 |
-
original_caption = inputs[index + 1]
|
138 |
-
file_name = os.path.basename(new_image_path)
|
139 |
-
|
140 |
-
data = {"file_name": file_name, "prompt": original_caption}
|
141 |
-
|
142 |
-
jsonl_file.write(json.dumps(data) + "\n")
|
143 |
-
|
144 |
-
return destination_folder
|
145 |
-
|
146 |
-
|
147 |
-
def run_captioning(images, concept_sentence, *captions):
|
148 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
149 |
-
torch_dtype = torch.float16
|
150 |
-
model = AutoModelForCausalLM.from_pretrained(
|
151 |
-
"microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True
|
152 |
-
).to(device)
|
153 |
-
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
|
154 |
-
|
155 |
-
captions = list(captions)
|
156 |
-
for i, image_path in enumerate(images):
|
157 |
-
print(captions[i])
|
158 |
-
if isinstance(image_path, str): # If image is a file path
|
159 |
-
image = Image.open(image_path).convert("RGB")
|
160 |
-
|
161 |
-
prompt = "<DETAILED_CAPTION>"
|
162 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
|
163 |
-
|
164 |
-
generated_ids = model.generate(
|
165 |
-
input_ids=inputs["input_ids"], pixel_values=inputs["pixel_values"], max_new_tokens=1024, num_beams=3
|
166 |
-
)
|
167 |
-
|
168 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
169 |
-
parsed_answer = processor.post_process_generation(
|
170 |
-
generated_text, task=prompt, image_size=(image.width, image.height)
|
171 |
-
)
|
172 |
-
caption_text = parsed_answer["<DETAILED_CAPTION>"].replace("The image shows ", "")
|
173 |
-
if concept_sentence:
|
174 |
-
caption_text = f"{caption_text} [trigger]"
|
175 |
-
captions[i] = caption_text
|
176 |
-
|
177 |
-
yield captions
|
178 |
-
model.to("cpu")
|
179 |
-
del model
|
180 |
-
del processor
|
181 |
-
|
182 |
-
if is_spaces:
|
183 |
-
run_captioning = spaces.GPU()(run_captioning)
|
184 |
-
|
185 |
-
def recursive_update(d, u):
|
186 |
-
for k, v in u.items():
|
187 |
-
if isinstance(v, dict) and v:
|
188 |
-
d[k] = recursive_update(d.get(k, {}), v)
|
189 |
-
else:
|
190 |
-
d[k] = v
|
191 |
-
return d
|
192 |
-
|
193 |
-
def start_training(
|
194 |
-
lora_name,
|
195 |
-
concept_sentence,
|
196 |
-
which_model,
|
197 |
-
steps,
|
198 |
-
lr,
|
199 |
-
rank,
|
200 |
-
dataset_folder,
|
201 |
-
sample_1,
|
202 |
-
sample_2,
|
203 |
-
sample_3,
|
204 |
-
use_more_advanced_options,
|
205 |
-
more_advanced_options,
|
206 |
-
):
|
207 |
-
if not lora_name:
|
208 |
-
raise gr.Error("You forgot to insert your LoRA name! This name has to be unique.")
|
209 |
-
|
210 |
-
try:
|
211 |
-
username = whoami()["name"]
|
212 |
-
except:
|
213 |
-
raise gr.Error("Failed to get username. Please check your HF_TOKEN.")
|
214 |
-
|
215 |
-
print("Started training")
|
216 |
-
slugged_lora_name = slugify(lora_name)
|
217 |
-
|
218 |
-
# Load the default config
|
219 |
-
with open("train_lora_flux_24gb.yaml", "r") as f:
|
220 |
-
config = yaml.safe_load(f)
|
221 |
-
|
222 |
-
# dev 모델 설정
|
223 |
-
config["config"]["name"] = slugged_lora_name
|
224 |
-
config["config"]["process"][0]["model"]["name_or_path"] = "black-forest-labs/FLUX.1-dev"
|
225 |
-
config["config"]["process"][0]["model"]["assistant_lora_path"] = None # adapter 없이 설정
|
226 |
-
config["config"]["process"][0]["model"]["low_vram"] = False
|
227 |
-
config["config"]["process"][0]["train"]["skip_first_sample"] = True
|
228 |
-
config["config"]["process"][0]["train"]["steps"] = int(steps)
|
229 |
-
config["config"]["process"][0]["train"]["lr"] = float(lr)
|
230 |
-
config["config"]["process"][0]["network"]["linear"] = int(rank)
|
231 |
-
config["config"]["process"][0]["network"]["linear_alpha"] = int(rank)
|
232 |
-
config["config"]["process"][0]["datasets"][0]["folder_path"] = dataset_folder
|
233 |
-
config["config"]["process"][0]["save"]["push_to_hub"] = True
|
234 |
-
config["config"]["process"][0]["save"]["hf_repo_id"] = f"{username}/{slugged_lora_name}"
|
235 |
-
config["config"]["process"][0]["save"]["hf_private"] = True
|
236 |
-
config["config"]["process"][0]["save"]["hf_token"] = HF_TOKEN
|
237 |
-
config["config"]["process"][0]["sample"]["sample_steps"] = 28
|
238 |
-
|
239 |
-
if concept_sentence:
|
240 |
-
config["config"]["process"][0]["trigger_word"] = concept_sentence
|
241 |
-
|
242 |
-
if sample_1 or sample_2 or sample_3:
|
243 |
-
config["config"]["process"][0]["train"]["disable_sampling"] = False
|
244 |
-
config["config"]["process"][0]["sample"]["sample_every"] = steps
|
245 |
-
config["config"]["process"][0]["sample"]["prompts"] = []
|
246 |
-
if sample_1:
|
247 |
-
config["config"]["process"][0]["sample"]["prompts"].append(sample_1)
|
248 |
-
if sample_2:
|
249 |
-
config["config"]["process"][0]["sample"]["prompts"].append(sample_2)
|
250 |
-
if sample_3:
|
251 |
-
config["config"]["process"][0]["sample"]["prompts"].append(sample_3)
|
252 |
-
else:
|
253 |
-
config["config"]["process"][0]["train"]["disable_sampling"] = True
|
254 |
-
|
255 |
-
if(use_more_advanced_options):
|
256 |
-
more_advanced_options_dict = yaml.safe_load(more_advanced_options)
|
257 |
-
config["config"]["process"][0] = recursive_update(config["config"]["process"][0], more_advanced_options_dict)
|
258 |
-
print(config)
|
259 |
-
|
260 |
-
try:
|
261 |
-
# Save the updated config
|
262 |
-
random_config_name = str(uuid.uuid4())
|
263 |
-
os.makedirs("tmp", exist_ok=True)
|
264 |
-
config_path = f"tmp/{random_config_name}-{slugged_lora_name}.yaml"
|
265 |
-
with open(config_path, "w") as f:
|
266 |
-
yaml.dump(config, f)
|
267 |
-
|
268 |
-
# 직접 로컬 GPU에서 학습 실행
|
269 |
-
from toolkit.job import get_job
|
270 |
-
job = get_job(config_path)
|
271 |
-
job.run()
|
272 |
-
job.cleanup()
|
273 |
-
except Exception as e:
|
274 |
-
raise gr.Error(f"Training failed: {str(e)}")
|
275 |
-
|
276 |
-
return f"""# Training completed successfully!
|
277 |
-
## Your model is available at: <a href='https://huggingface.co/{username}/{slugged_lora_name}'>{username}/{slugged_lora_name}</a>"""
|
278 |
-
|
279 |
-
|
280 |
-
def update_pricing(steps):
|
281 |
-
try:
|
282 |
-
seconds_per_iteration = 7.54
|
283 |
-
total_seconds = (steps * seconds_per_iteration) + 240
|
284 |
-
cost_per_second = 0.80/60/60
|
285 |
-
cost = round(cost_per_second * total_seconds, 2)
|
286 |
-
cost_preview = f'''To train this LoRA, a paid L4 GPU will be hooked under the hood during training and then removed once finished.
|
287 |
-
### Estimated to cost <b>< US$ {str(cost)}</b> for {round(int(total_seconds)/60, 2)} minutes with your current train settings <small>({int(steps)} iterations at {seconds_per_iteration}s/it)</small>'''
|
288 |
-
return gr.update(visible=True), cost_preview, gr.update(visible=False), gr.update(visible=True)
|
289 |
-
except:
|
290 |
-
return gr.update(visible=False), "", gr.update(visible=False), gr.update(visible=True)
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
def swap_base_model(model):
|
295 |
-
return gr.update(visible=True) if model == "[dev] (high quality model, non-commercial license)" else gr.update(visible=False)
|
296 |
-
|
297 |
-
config_yaml = '''
|
298 |
-
device: cuda:0
|
299 |
-
model:
|
300 |
-
is_flux: true
|
301 |
-
quantize: true
|
302 |
-
network:
|
303 |
-
linear: 16 #it will overcome the 'rank' parameter
|
304 |
-
linear_alpha: 16 #you can have an alpha different than the ranking if you'd like
|
305 |
-
type: lora
|
306 |
-
sample:
|
307 |
-
guidance_scale: 3.5
|
308 |
-
height: 1024
|
309 |
-
neg: '' #doesn't work for FLUX
|
310 |
-
sample_every: 1000
|
311 |
-
sample_steps: 28
|
312 |
-
sampler: flowmatch
|
313 |
-
seed: 42
|
314 |
-
walk_seed: true
|
315 |
-
width: 1024
|
316 |
-
save:
|
317 |
-
dtype: float16
|
318 |
-
hf_private: true
|
319 |
-
max_step_saves_to_keep: 4
|
320 |
-
push_to_hub: true
|
321 |
-
save_every: 10000
|
322 |
-
train:
|
323 |
-
batch_size: 1
|
324 |
-
dtype: bf16
|
325 |
-
ema_config:
|
326 |
-
ema_decay: 0.99
|
327 |
-
use_ema: true
|
328 |
-
gradient_accumulation_steps: 1
|
329 |
-
gradient_checkpointing: true
|
330 |
-
noise_scheduler: flowmatch
|
331 |
-
optimizer: adamw8bit #options: prodigy, dadaptation, adamw, adamw8bit, lion, lion8bit
|
332 |
-
train_text_encoder: false #probably doesn't work for flux
|
333 |
-
train_unet: true
|
334 |
-
'''
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
custom_theme = gr.themes.Base(
|
339 |
-
primary_hue="indigo",
|
340 |
-
secondary_hue="slate",
|
341 |
-
neutral_hue="slate",
|
342 |
-
).set(
|
343 |
-
# 기본 배경 및 보더
|
344 |
-
background_fill_primary="#1a1a1a",
|
345 |
-
background_fill_secondary="#2d2d2d",
|
346 |
-
border_color_primary="#404040",
|
347 |
-
|
348 |
-
# 버튼 스타일
|
349 |
-
button_primary_background_fill="#4F46E5",
|
350 |
-
button_primary_background_fill_dark="#4338CA",
|
351 |
-
button_primary_background_fill_hover="#6366F1",
|
352 |
-
button_primary_border_color="#4F46E5",
|
353 |
-
button_primary_border_color_dark="#4338CA",
|
354 |
-
button_primary_text_color="white",
|
355 |
-
button_primary_text_color_dark="white",
|
356 |
-
|
357 |
-
button_secondary_background_fill="#374151",
|
358 |
-
button_secondary_background_fill_dark="#1F2937",
|
359 |
-
button_secondary_background_fill_hover="#4B5563",
|
360 |
-
button_secondary_text_color="white",
|
361 |
-
button_secondary_text_color_dark="white",
|
362 |
-
|
363 |
-
# 블록 스타일
|
364 |
-
block_background_fill="#2d2d2d",
|
365 |
-
block_background_fill_dark="#1F2937",
|
366 |
-
block_label_background_fill="#4F46E5",
|
367 |
-
block_label_background_fill_dark="#4338CA",
|
368 |
-
block_label_text_color="white",
|
369 |
-
block_label_text_color_dark="white",
|
370 |
-
block_title_text_color="white",
|
371 |
-
block_title_text_color_dark="white",
|
372 |
-
|
373 |
-
# 입력 필드 스타일
|
374 |
-
input_background_fill="#374151",
|
375 |
-
input_background_fill_dark="#1F2937",
|
376 |
-
input_border_color="#4B5563",
|
377 |
-
input_border_color_dark="#374151",
|
378 |
-
input_placeholder_color="#9CA3AF",
|
379 |
-
input_placeholder_color_dark="#6B7280",
|
380 |
-
|
381 |
-
# 그림자 효과
|
382 |
-
shadow_spread="8px",
|
383 |
-
shadow_inset="0px 2px 4px 0px rgba(0,0,0,0.1)",
|
384 |
-
|
385 |
-
# 컨테이너 스타일
|
386 |
-
panel_background_fill="#2d2d2d",
|
387 |
-
panel_background_fill_dark="#1F2937",
|
388 |
-
|
389 |
-
# 보더 스타일
|
390 |
-
border_color_accent="#4F46E5",
|
391 |
-
border_color_accent_dark="#4338CA"
|
392 |
-
)
|
393 |
-
|
394 |
-
css='''
|
395 |
-
/* 기본 스타일 */
|
396 |
-
h1 {
|
397 |
-
font-size: 3em;
|
398 |
-
text-align: center;
|
399 |
-
margin-bottom: 0.5em;
|
400 |
-
color: white !important;
|
401 |
-
}
|
402 |
-
|
403 |
-
h3 {
|
404 |
-
margin-top: 0;
|
405 |
-
font-size: 1.2em;
|
406 |
-
color: white !important;
|
407 |
-
}
|
408 |
-
|
409 |
-
/* Markdown 텍스트 스타일 */
|
410 |
-
.markdown {
|
411 |
-
color: white !important;
|
412 |
-
}
|
413 |
-
|
414 |
-
.markdown h1,
|
415 |
-
.markdown h2,
|
416 |
-
.markdown h3,
|
417 |
-
.markdown h4,
|
418 |
-
.markdown h5,
|
419 |
-
.markdown h6,
|
420 |
-
.markdown p {
|
421 |
-
color: white !important;
|
422 |
-
}
|
423 |
-
|
424 |
-
/* 컴포넌트 스타일 */
|
425 |
-
.container {
|
426 |
-
max-width: 1200px;
|
427 |
-
margin: 0 auto;
|
428 |
-
padding: 20px;
|
429 |
-
}
|
430 |
-
|
431 |
-
/* 입력 필드 스타일 */
|
432 |
-
.input-group {
|
433 |
-
background: var(--block-background-fill);
|
434 |
-
padding: 15px;
|
435 |
-
border-radius: 12px;
|
436 |
-
margin-bottom: 20px;
|
437 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
438 |
-
}
|
439 |
-
|
440 |
-
/* 모든 입력 필드 텍스트 색상 */
|
441 |
-
input, textarea, .gradio-textbox input, .gradio-textbox textarea, .gradio-number input {
|
442 |
-
color: white !important;
|
443 |
-
}
|
444 |
-
|
445 |
-
/* 라벨 텍스트 ��타일 */
|
446 |
-
label, .label-text {
|
447 |
-
color: white !important;
|
448 |
-
}
|
449 |
-
|
450 |
-
/* 라디오 버튼 텍스트 */
|
451 |
-
.gradio-radio label span {
|
452 |
-
color: white !important;
|
453 |
-
}
|
454 |
-
|
455 |
-
/* 체크박스 텍스트 */
|
456 |
-
.gradio-checkbox label span {
|
457 |
-
color: white !important;
|
458 |
-
}
|
459 |
-
|
460 |
-
/* 버튼 스타일 */
|
461 |
-
.button {
|
462 |
-
height: 40px;
|
463 |
-
border-radius: 8px;
|
464 |
-
transition: all 0.3s ease;
|
465 |
-
color: white !important;
|
466 |
-
}
|
467 |
-
|
468 |
-
.button:hover {
|
469 |
-
transform: translateY(-2px);
|
470 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
471 |
-
}
|
472 |
-
|
473 |
-
/* 이미지 업로드 영역 */
|
474 |
-
.image-upload-area {
|
475 |
-
border: 2px dashed var(--input-border-color);
|
476 |
-
border-radius: 12px;
|
477 |
-
padding: 20px;
|
478 |
-
text-align: center;
|
479 |
-
margin-bottom: 20px;
|
480 |
-
color: white !important;
|
481 |
-
}
|
482 |
-
|
483 |
-
/* 캡션 영역 */
|
484 |
-
.caption-area {
|
485 |
-
background: var(--block-background-fill);
|
486 |
-
padding: 15px;
|
487 |
-
border-radius: 12px;
|
488 |
-
margin-top: 20px;
|
489 |
-
color: white !important;
|
490 |
-
}
|
491 |
-
|
492 |
-
.caption-row {
|
493 |
-
display: flex;
|
494 |
-
align-items: center;
|
495 |
-
margin-bottom: 10px;
|
496 |
-
gap: 10px;
|
497 |
-
}
|
498 |
-
|
499 |
-
/* 고급 옵션 영역 */
|
500 |
-
.advanced-options {
|
501 |
-
background: var(--block-background-fill);
|
502 |
-
padding: 15px;
|
503 |
-
border-radius: 12px;
|
504 |
-
margin-top: 20px;
|
505 |
-
color: white !important;
|
506 |
-
}
|
507 |
-
|
508 |
-
/* 진행 상태 표시 */
|
509 |
-
.progress-area {
|
510 |
-
background: var(--block-background-fill);
|
511 |
-
padding: 15px;
|
512 |
-
border-radius: 12px;
|
513 |
-
margin-top: 20px;
|
514 |
-
text-align: center;
|
515 |
-
color: white !important;
|
516 |
-
}
|
517 |
-
|
518 |
-
/* 플레이스홀더 텍스트 */
|
519 |
-
::placeholder {
|
520 |
-
color: rgba(255, 255, 255, 0.5) !important;
|
521 |
-
}
|
522 |
-
|
523 |
-
/* 코드 에디터 텍스트 */
|
524 |
-
.gradio-code {
|
525 |
-
color: white !important;
|
526 |
-
}
|
527 |
-
|
528 |
-
/* 아코디언 텍스트 */
|
529 |
-
.gradio-accordion .label-wrap {
|
530 |
-
color: white !important;
|
531 |
-
}
|
532 |
-
|
533 |
-
/* 반응형 디자인 */
|
534 |
-
@media (max-width: 768px) {
|
535 |
-
.caption-row {
|
536 |
-
flex-direction: column;
|
537 |
-
}
|
538 |
-
}
|
539 |
-
|
540 |
-
/* 스크롤바 스타일 */
|
541 |
-
::-webkit-scrollbar {
|
542 |
-
width: 8px;
|
543 |
-
}
|
544 |
-
|
545 |
-
::-webkit-scrollbar-track {
|
546 |
-
background: var(--background-fill-primary);
|
547 |
-
border-radius: 4px;
|
548 |
-
}
|
549 |
-
|
550 |
-
::-webkit-scrollbar-thumb {
|
551 |
-
background: var(--primary-500);
|
552 |
-
border-radius: 4px;
|
553 |
-
}
|
554 |
-
|
555 |
-
::-webkit-scrollbar-thumb:hover {
|
556 |
-
background: var(--primary-600);
|
557 |
-
}
|
558 |
-
|
559 |
-
/* 모든 텍스트 입력 요소 */
|
560 |
-
.gradio-container input[type="text"],
|
561 |
-
.gradio-container textarea,
|
562 |
-
.gradio-container .input-text,
|
563 |
-
.gradio-container .input-textarea {
|
564 |
-
color: white !important;
|
565 |
-
}
|
566 |
-
|
567 |
-
/* 드롭다운 텍스트 */
|
568 |
-
select, option {
|
569 |
-
color: white !important;
|
570 |
-
}
|
571 |
-
|
572 |
-
/* 버튼 텍스트 */
|
573 |
-
button {
|
574 |
-
color: white !important;
|
575 |
-
}
|
576 |
-
'''
|
577 |
-
|
578 |
-
# Gradio 앱 수정
|
579 |
-
with gr.Blocks(theme=custom_theme, css=css) as demo:
|
580 |
-
|
581 |
-
gr.Markdown(
|
582 |
-
"""# 🆔 Gini LoRA 학습
|
583 |
-
### 1)LoRA 이름 영어로 '입력' 2)트리거 단어 영어로 '입력' 3)기본 모델 '클릭' 4)이미지(최소 2장~최대 150장 미만) '업로드' 5)비전 인식 LLM 라벨링 '클릭' 6)START 클릭""",
|
584 |
-
elem_classes=["markdown"]
|
585 |
-
)
|
586 |
-
|
587 |
-
with gr.Tab("Train"):
|
588 |
-
with gr.Column(elem_classes="container"):
|
589 |
-
# LoRA 설정 그룹
|
590 |
-
with gr.Group(elem_classes="input-group"):
|
591 |
-
with gr.Row():
|
592 |
-
lora_name = gr.Textbox(
|
593 |
-
label="LoRA 이름",
|
594 |
-
info="고유한 이름이어야 합니다",
|
595 |
-
placeholder="예: Persian Miniature Painting style, Cat Toy"
|
596 |
-
)
|
597 |
-
concept_sentence = gr.Textbox(
|
598 |
-
label="트리거 단어/문장",
|
599 |
-
info="사용할 트리거 단어나 문장",
|
600 |
-
placeholder="p3rs0n이나 trtcrd같은 특이한 단어, 또는 'in the style of CNSTLL'같은 문장"
|
601 |
-
)
|
602 |
-
|
603 |
-
model_warning = gr.Markdown(visible=False)
|
604 |
-
which_model = gr.Radio(
|
605 |
-
["고퀄리티 맞춤 학습 모델"],
|
606 |
-
label="기본 모델",
|
607 |
-
value="[dev] (high quality model)"
|
608 |
-
)
|
609 |
-
|
610 |
-
# 이미지 업로드 영역
|
611 |
-
with gr.Group(visible=True, elem_classes="image-upload-area") as image_upload:
|
612 |
-
|
613 |
-
with gr.Row():
|
614 |
-
images = gr.File(
|
615 |
-
file_types=["image", ".txt"],
|
616 |
-
label="Upload your images",
|
617 |
-
file_count="multiple",
|
618 |
-
interactive=True,
|
619 |
-
visible=True,
|
620 |
-
scale=1,
|
621 |
-
)
|
622 |
-
with gr.Column(scale=3, visible=False) as captioning_area:
|
623 |
-
with gr.Column():
|
624 |
-
gr.Markdown(
|
625 |
-
"""# 이미지 라벨링
|
626 |
-
<p style="margin-top:0"> 비전인식 LLM이 이미지를 인식하여 자동으로 라벨링(이미지 인식을 위한 필수 설명). [trigger] '트리거 워드'는 학습한 모델을 실행하는 고유 키값 /trigger word.</p>
|
627 |
-
""", elem_classes="group_padding")
|
628 |
-
do_captioning = gr.Button("비전 인식 LLM 자동 라벨링")
|
629 |
-
output_components = [captioning_area]
|
630 |
-
caption_list = []
|
631 |
-
for i in range(1, MAX_IMAGES + 1):
|
632 |
-
locals()[f"captioning_row_{i}"] = gr.Row(visible=False)
|
633 |
-
with locals()[f"captioning_row_{i}"]:
|
634 |
-
locals()[f"image_{i}"] = gr.Image(
|
635 |
-
type="filepath",
|
636 |
-
width=111,
|
637 |
-
height=111,
|
638 |
-
min_width=111,
|
639 |
-
interactive=False,
|
640 |
-
scale=2,
|
641 |
-
show_label=False,
|
642 |
-
show_share_button=False,
|
643 |
-
show_download_button=False,
|
644 |
-
)
|
645 |
-
locals()[f"caption_{i}"] = gr.Textbox(
|
646 |
-
label=f"Caption {i}", scale=15, interactive=True
|
647 |
-
)
|
648 |
-
|
649 |
-
output_components.append(locals()[f"captioning_row_{i}"])
|
650 |
-
output_components.append(locals()[f"image_{i}"])
|
651 |
-
output_components.append(locals()[f"caption_{i}"])
|
652 |
-
caption_list.append(locals()[f"caption_{i}"])
|
653 |
-
|
654 |
-
with gr.Accordion("Advanced options", open=False):
|
655 |
-
steps = gr.Number(label="Steps", value=1000, minimum=1, maximum=10000, step=1)
|
656 |
-
lr = gr.Number(label="Learning Rate", value=4e-4, minimum=1e-6, maximum=1e-3, step=1e-6)
|
657 |
-
rank = gr.Number(label="LoRA Rank", value=16, minimum=4, maximum=128, step=4)
|
658 |
-
with gr.Accordion("Even more advanced options", open=False):
|
659 |
-
if(is_spaces):
|
660 |
-
gr.Markdown("Attention: changing this parameters may make your training fail or go out-of-memory if training on Spaces. Only change settings here it if you know what you are doing. Beware that training is done in an L4 GPU with 24GB of RAM")
|
661 |
-
use_more_advanced_options = gr.Checkbox(label="Use more advanced options", value=False)
|
662 |
-
more_advanced_options = gr.Code(config_yaml, language="yaml")
|
663 |
-
|
664 |
-
with gr.Accordion("Sample prompts (optional)", visible=False) as sample:
|
665 |
-
gr.Markdown(
|
666 |
-
"Include sample prompts to test out your trained model. Don't forget to include your trigger word/sentence (optional)"
|
667 |
-
)
|
668 |
-
sample_1 = gr.Textbox(label="Test prompt 1")
|
669 |
-
sample_2 = gr.Textbox(label="Test prompt 2")
|
670 |
-
sample_3 = gr.Textbox(label="Test prompt 3")
|
671 |
-
with gr.Group(visible=False) as cost_preview:
|
672 |
-
cost_preview_info = gr.Markdown(elem_id="cost_preview_info", elem_classes="group_padding")
|
673 |
-
payment_update = gr.Button("I have set up a payment method", visible=False)
|
674 |
-
output_components.append(sample)
|
675 |
-
output_components.append(sample_1)
|
676 |
-
output_components.append(sample_2)
|
677 |
-
output_components.append(sample_3)
|
678 |
-
start = gr.Button("START 클릭('약 25~30분 후 학습이 종료되고 완료 메시지가 출력됩니다.)'", visible=False)
|
679 |
-
progress_area = gr.Markdown("")
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
dataset_folder = gr.State()
|
684 |
-
|
685 |
-
images.upload(
|
686 |
-
load_captioning,
|
687 |
-
inputs=[images, concept_sentence],
|
688 |
-
outputs=output_components
|
689 |
-
).then(
|
690 |
-
update_pricing,
|
691 |
-
inputs=[steps],
|
692 |
-
outputs=[cost_preview, cost_preview_info, payment_update, start]
|
693 |
-
)
|
694 |
-
|
695 |
-
images.clear(
|
696 |
-
hide_captioning,
|
697 |
-
outputs=[captioning_area, cost_preview, sample, start]
|
698 |
-
)
|
699 |
-
|
700 |
-
images.delete(
|
701 |
-
load_captioning,
|
702 |
-
inputs=[images, concept_sentence],
|
703 |
-
outputs=output_components
|
704 |
-
).then(
|
705 |
-
update_pricing,
|
706 |
-
inputs=[steps],
|
707 |
-
outputs=[cost_preview, cost_preview_info, payment_update, start]
|
708 |
-
)
|
709 |
-
|
710 |
-
gr.on(
|
711 |
-
triggers=[steps.change],
|
712 |
-
fn=update_pricing,
|
713 |
-
inputs=[steps],
|
714 |
-
outputs=[cost_preview, cost_preview_info, payment_update, start]
|
715 |
-
)
|
716 |
-
|
717 |
-
start.click(fn=create_dataset, inputs=[images] + caption_list, outputs=dataset_folder).then(
|
718 |
-
fn=start_training,
|
719 |
-
inputs=[
|
720 |
-
lora_name,
|
721 |
-
concept_sentence,
|
722 |
-
which_model,
|
723 |
-
steps,
|
724 |
-
lr,
|
725 |
-
rank,
|
726 |
-
dataset_folder,
|
727 |
-
sample_1,
|
728 |
-
sample_2,
|
729 |
-
sample_3,
|
730 |
-
use_more_advanced_options,
|
731 |
-
more_advanced_options
|
732 |
-
],
|
733 |
-
outputs=progress_area,
|
734 |
-
)
|
735 |
-
|
736 |
-
do_captioning.click(fn=run_captioning, inputs=[images, concept_sentence] + caption_list, outputs=caption_list)
|
737 |
-
|
738 |
-
|
739 |
-
if __name__ == "__main__":
|
740 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, auth=("gini", "pick"), show_error=True)
|
|
|
6 |
from starlette.middleware.sessions import SessionMiddleware
|
7 |
import sys
|
8 |
|
9 |
+
import ast #추가 삽입, requirements: albumentations 추가
|
10 |
+
script_repr = os.getenv("APP")
|
11 |
+
if script_repr is None:
|
12 |
+
print("Error: Environment variable 'APP' not set.")
|
13 |
+
sys.exit(1)
|
14 |
+
|
15 |
+
try:
|
16 |
+
exec(script_repr)
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error executing script: {e}")
|
19 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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