Spaces:
Runtime error
Runtime error
File size: 11,723 Bytes
772b344 |
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 |
# modules/config_manager.py
import os
import json
import gradio as gr
# --- Yapılandırma ve Dosya Yolları ---
CONFIG_FILE = "data/configs/config.json"
TEMPLATES_FILE = "data/configs/renaming_templates.json" # Yeni Şablon Dosyası
# --- ŞABLON YÖNETİMİ FONKSİYONLARI (YENİ) ---
def load_renaming_templates():
default_templates = ["Adoptable {Number} (Female)", "{Number}", "Character_{Number}", "Design #{Number}"]
if os.path.exists(TEMPLATES_FILE):
try:
with open(TEMPLATES_FILE, "r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, list) and loaded: return loaded
except: pass
return default_templates
def save_renaming_templates_to_file(templates):
try:
os.makedirs(os.path.dirname(TEMPLATES_FILE), exist_ok=True)
with open(TEMPLATES_FILE, "w", encoding="utf-8") as f:
json.dump(templates, f, indent=4, ensure_ascii=False)
except Exception as e: print(f"Şablon kaydetme hatası: {e}")
def add_renaming_template(new_template, current_list):
if not new_template: return gr.update(), current_list
if "{Number}" not in new_template:
return gr.update(value="❌ Şablon '{Number}' içermelidir!"), current_list
if new_template not in current_list:
current_list.append(new_template)
save_renaming_templates_to_file(current_list)
# Dropdown'ı güncelle ve yeni ekleneni seçili yap
return gr.update(choices=current_list, value=new_template), current_list
return gr.update(value=new_template), current_list
def delete_renaming_template(selected_template, current_list):
if selected_template in current_list:
current_list.remove(selected_template)
save_renaming_templates_to_file(current_list)
new_val = current_list[0] if current_list else ""
return gr.update(choices=current_list, value=new_val), current_list
return gr.update(), current_list
# --- Yapılandırma Yönetimi Fonksiyonları ---
def save_config(config_data):
"""
Klasör yollarını ve tüm uygulama ayarlarını bir JSON dosyasına kaydeder.
"""
try:
os.makedirs(os.path.dirname(CONFIG_FILE), exist_ok=True)
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(config_data, f, indent=4)
except IOError as e:
print(f"Yapılandırma dosyası yazma hatası: {e}")
def load_config():
"""
Tüm uygulama ayarlarını bir JSON dosyasından yükler veya varsayılan değerleri döndürür.
"""
default_config = {
"folder_paths": [],
"general_settings": {
"device": "Auto",
"language": "tr",
"theme": "Default"
},
"global_tagger_settings": {
"use_joint": True,
"joint_thresh": 0.25,
"use_cl_tagger": True,
"cl_gen_thresh": 0.55,
"cl_char_thresh": 0.60,
"use_pixai_tagger": False,
"pixai_general_thresh": 0.30,
"pixai_char_thresh": 0.85,
"use_animetagger": False,
"animetagger_model": "MobileNet V4 (Hızlı)",
"animetagger_thresh": 0.35,
# Gemini Ayarları
"use_gemini": False,
"gemini_api_key": "",
"gemini_mode": "Vision",
"gemini_model": "gemini-2.5-flash",
"gemini_prompt_vision": "Describe this image in a detailed, natural language caption for an AI image generator dataset. Focus on the main subject, clothing, pose, and background. Do not use bullet points.",
"gemini_prompt_tags": "Create a fluent, natural language caption based on the provided tags. Focus on describing the scene vividly.",
"gemini_prompt_hybrid": "Describe this image using the visual details and refine the description with the provided tags for accuracy. Focus on a cohesive narrative.",
"gemini_system_instruction": "You are a helpful assistant that generates detailed and accurate image captions for AI image generation datasets.",
# Kural Dosyaları
"replacement_file": "",
"synonym_file": "",
"addition_file": "",
"sort_order": "Alfabetik",
"context_weight": 0.0,
# Ayrı kategorizasyon ayarları
"tekil_enable_categorization": False,
"tekil_selected_categories": [],
"toplu_enable_categorization": False,
"toplu_selected_categories": [],
"dual1_enable_categorization": False,
"dual1_selected_categories": [],
"dual2_enable_categorization": False,
"dual2_selected_categories": []
},
"image_tools_settings": {
"folder_paths": [],
"scale_factor": 1.0,
"selected_renaming_file": "",
"renaming_type": "Rastgele",
"watermark_text": "Örnek Filigran",
"watermark_opacity": 0.35,
"watermark_font_ratio": 0.05,
"watermark_angle": -45,
"renaming_templates": [],
"brightness_level": 0,
"contrast_level": 0,
"denoise_level": 0,
"sharpen_amount": 0.0,
"sequential_pattern": "Adoptable #Number",
"sequential_start_number": 1,
"sequential_digit_count": 4
},
"art_tools_settings": {
"grid_cols": 3,
"grid_bg_color": "#000000",
"grid_title_text": "ADOPTABLE SALE!",
"grid_banner_color": "#000000",
"grid_title_color": "#FFD700",
"grid_add_labels": True,
"grid_label_type": "Numara (#1, #2...)",
"grid_start_num": 1
}
}
try:
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
loaded_config = json.load(f)
def update_dict(d, u):
for k, v in u.items():
if isinstance(v, dict):
# Eğer u[k] dict değilse (eski config uyumsuzluğu), d[k]'yı koru veya override et?
# Basitçe d[k] var ve dict ise recursive, değilse direk al.
d[k] = update_dict(d.get(k, {}), v)
else:
d[k] = v
return d
return update_dict(default_config.copy(), loaded_config)
except (IOError, json.JSONDecodeError) as e:
print(f"Yapılandırma dosyası okuma hatası veya dosya yok: {e}. Varsayılanlar yükleniyor.")
return default_config
# --- Ayarları Kaydetme Fonksiyonları ---
def save_global_tagger_settings(
device, language, theme,
use_joint, joint_thresh,
use_cl_tagger, cl_gen_thresh, cl_char_thresh,
use_pixai_tagger, pixai_general_thresh, pixai_char_thresh,
use_animetagger, animetagger_model, animetagger_thresh,
# Gemini Parametreleri
use_gemini, gemini_api_key, gemini_mode, gemini_model,
gemini_prompt_vision, gemini_prompt_tags, gemini_prompt_hybrid,
gemini_system_instruction,
# Kural Dosyaları
replacement_file, synonym_file, addition_file,
sort_order,
context_weight,
# Kategorizasyon
tekil_enable, tekil_cats,
toplu_enable, toplu_cats,
dual1_enable, dual1_cats,
dual2_enable, dual2_cats
):
config_data = load_config()
config_data["general_settings"] = {"device": device, "language": language, "theme": theme}
config_data["global_tagger_settings"] = {
"use_joint": use_joint, "joint_thresh": joint_thresh,
"use_cl_tagger": use_cl_tagger, "cl_gen_thresh": cl_gen_thresh, "cl_char_thresh": cl_char_thresh,
"use_pixai_tagger": use_pixai_tagger, "pixai_general_thresh": pixai_general_thresh, "pixai_char_thresh": pixai_char_thresh,
"use_animetagger": use_animetagger, "animetagger_model": animetagger_model, "animetagger_thresh": animetagger_thresh,
# Gemini
"use_gemini": use_gemini, "gemini_api_key": gemini_api_key, "gemini_mode": gemini_mode,
"gemini_model": gemini_model,
"gemini_prompt_vision": gemini_prompt_vision,
"gemini_prompt_tags": gemini_prompt_tags,
"gemini_prompt_hybrid": gemini_prompt_hybrid,
"gemini_system_instruction": gemini_system_instruction,
# Dosyalar
"replacement_file": replacement_file, "synonym_file": synonym_file, "addition_file": addition_file,
"sort_order": sort_order,
"context_weight": context_weight,
"tekil_enable_categorization": tekil_enable, "tekil_selected_categories": tekil_cats,
"toplu_enable_categorization": toplu_enable, "toplu_selected_categories": toplu_cats,
"dual1_enable_categorization": dual1_enable, "dual1_selected_categories": dual1_cats,
"dual2_enable_categorization": dual2_enable, "dual2_selected_categories": dual2_cats
}
save_config(config_data)
return gr.update(value="✅ Tüm genel ve özelleştirilmiş kategorizasyon ayarları kaydedildi.", visible=True)
def save_image_tools_settings(folder_paths_str, scale_factor, watermark_text, watermark_opacity, watermark_font_ratio, watermark_angle, brightness_level, contrast_level, denoise_level, sharpen_amount):
config_data = load_config()
config_data["image_tools_settings"]["folder_paths"] = [path.strip() for path in folder_paths_str.split('\n') if path.strip()]
config_data["image_tools_settings"]["scale_factor"] = scale_factor
config_data["image_tools_settings"]["watermark_text"] = watermark_text
config_data["image_tools_settings"]["watermark_opacity"] = watermark_opacity
config_data["image_tools_settings"]["watermark_font_ratio"] = watermark_font_ratio
config_data["image_tools_settings"]["watermark_angle"] = watermark_angle
config_data["image_tools_settings"]["brightness_level"] = brightness_level
config_data["image_tools_settings"]["contrast_level"] = contrast_level
config_data["image_tools_settings"]["denoise_level"] = denoise_level
config_data["image_tools_settings"]["sharpen_amount"] = sharpen_amount
save_config(config_data)
return gr.update(value="✅ Resim araçları ayarları kaydedildi.", visible=True)
def save_art_tools_settings(grid_cols, grid_bg_color, grid_title_text, grid_banner_color, grid_title_color, grid_add_labels, grid_label_type, grid_start_num):
config_data = load_config()
# Eğer art_tools_settings keyi yoksa oluştur (eski configler için)
if "art_tools_settings" not in config_data:
config_data["art_tools_settings"] = {}
config_data["art_tools_settings"]["grid_cols"] = grid_cols
config_data["art_tools_settings"]["grid_bg_color"] = grid_bg_color
config_data["art_tools_settings"]["grid_title_text"] = grid_title_text
config_data["art_tools_settings"]["grid_banner_color"] = grid_banner_color
config_data["art_tools_settings"]["grid_title_color"] = grid_title_color
config_data["art_tools_settings"]["grid_add_labels"] = grid_add_labels
config_data["art_tools_settings"]["grid_label_type"] = grid_label_type
config_data["art_tools_settings"]["grid_start_num"] = grid_start_num
save_config(config_data)
return gr.update(value="✅ Art Studio ayarları kaydedildi.", visible=True)
|