File size: 8,223 Bytes
cc2f8db
 
a359aed
 
d066e07
cc2f8db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a359aed
cc2f8db
 
 
 
 
 
 
 
a359aed
cc2f8db
 
 
 
 
 
 
 
 
 
 
 
 
 
a359aed
cc2f8db
 
a359aed
cc2f8db
 
a359aed
cc2f8db
 
 
 
 
 
 
 
a359aed
cc2f8db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a359aed
d066e07
cc2f8db
 
 
 
 
d066e07
 
cc2f8db
 
 
 
 
 
a359aed
cc2f8db
 
 
 
a359aed
cc2f8db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a359aed
ba5408b
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
# app.py - HuggingFace Space Version
# UltraProTagger - Tam Uygulama

import os
import sys
import gradio as gr

# Modül importları
from modules.shared_state import set_web_image, check_and_reset_tab_switch, shared_state
from modules.config_manager import load_config
from modules.ui.common import CSS_STYLES
from modules.ui.tab_settings import create_settings_tab
from modules.ui.tab_single import create_single_tab
from modules.ui.tab_batch import create_batch_tab
from modules.ui.tab_dual import create_dual_tab
from modules.ui.tab_tools import create_tools_tab
from modules.managers.rule_manager import (
    read_replacement_file_content, 
    read_synonym_file_content, 
    read_addition_file_content
)
from modules.managers.localization_manager import get_str

# --- Global Kontroller ---
try:
    from modules.tagger import run_joint_classifier, cl_tagger_instance
except Exception as e:
    print(f"⚠️ Tagger yüklenirken hata: {e}")
    run_joint_classifier = None
    cl_tagger_instance = None

# Klasör Sabitleri
KATEGORILER_KLASORU = "data/rules/kategoriler"
ALT_KATEGORILER_KLASORU = "data/rules/alt_kategoriler"
REPLACEMENT_RULES_KLASORU = "data/rules/replacement_rules"
SYNONYM_RULES_KLASORU = "data/rules/synonym_rules"
ADDITION_RULES_KLASORU = "data/rules/addition_rules"
CONFIGLIST_KLASORU = "data/rules/configlist"

OS_ENV_HF_TOKEN = os.environ.get("HF_TOKEN")


def create_ui():
    # Ayarları yükle
    app_config = load_config()

    # Tema Ayarını Uygula
    current_theme_name = app_config.get("general_settings", {}).get("theme", "Default")
    theme_object = None
    
    if current_theme_name == "Soft":
        theme_object = gr.themes.Soft()
    elif current_theme_name == "Glass":
        theme_object = gr.themes.Glass()
    elif current_theme_name == "Monochrome":
        theme_object = gr.themes.Monochrome()

    with gr.Blocks(title=get_str("app_title"), css=CSS_STYLES, theme=theme_object) as demo:
        
        # Uyarı mesajları
        if OS_ENV_HF_TOKEN is None or run_joint_classifier is None:
            gr.Markdown("""<div style='background-color:#fff3cd; color:#856404; padding:10px; border-radius:8px; border:1px solid #ffeeba; margin-bottom: 20px;'>⚠️ **UYARI:** Joint Tagger modeli başlatılamadı veya 'HF_TOKEN' ortam değişkeni ayarlanmamış.</div>""")
        if cl_tagger_instance is None or (hasattr(cl_tagger_instance, 'session') and cl_tagger_instance.session is None):
            gr.Markdown("""<div style='background-color:#f8d7da; color:#721c24; padding:10px; border-radius:8px; border:1px solid #f5c6cb; margin-bottom: 20px;'>❌ **HATA:** CL Tagger modeli başlatılamadı.</div>""")

        # ANA TABS
        with gr.Tabs() as tabs_ana_panel:
            
            # 1. AYARLAR
            settings_components = create_settings_tab(app_config)
            
            # 2. TEKİL ETİKETLEME
            single_components = create_single_tab(settings_components)

            # 3. DUAL ETİKETLEME
            dual_components = create_dual_tab(settings_components)

            # 4. TOPLU ETİKETLEME
            batch_components = create_batch_tab(settings_components)
            
            # 5. ARAÇLAR
            tools_components = create_tools_tab(app_config)

        # --- ANA SEKME KONTROL DÖNGÜSÜ ---
        def control_tab_switch():
            if check_and_reset_tab_switch():
                return gr.Tabs(selected="tab_single")
            return gr.update()
            
        main_loop_btn = gr.Button("Main Loop", visible=False, elem_id="main_loop_btn")
        main_loop_btn.click(
            fn=control_tab_switch,
            inputs=[],
            outputs=[tabs_ana_panel],
            show_progress="hidden"
        )

        # --- BUTON BAĞLANTILARI ---
        def transfer_ve_gecis(text):
            return text, gr.Tabs(selected="tab_kategori_araclari")

        if "btn_send_tekil" in single_components and "toplu_filtered_output" in single_components:
            single_components["btn_send_tekil"].click(
                fn=transfer_ve_gecis,
                inputs=[single_components["toplu_filtered_output"]],
                outputs=[tools_components["etiket_input_common"], tabs_ana_panel]
            )

        if "btn_send_toplu" in batch_components and "toplu_batch_combined_original_tags" in batch_components:
            batch_components["btn_send_toplu"].click(
                fn=transfer_ve_gecis,
                inputs=[batch_components["toplu_batch_combined_original_tags"]],
                outputs=[tools_components["etiket_input_common"], tabs_ana_panel]
            )

        if "btn_send_dual" in dual_components and "dual_combined_all_tags" in dual_components:
            dual_components["btn_send_dual"].click(
                fn=transfer_ve_gecis,
                inputs=[dual_components["dual_combined_all_tags"]],
                outputs=[tools_components["etiket_input_common"], tabs_ana_panel]
            )

        gr.Markdown(
            f"<h1 style='text-align: center; color: #4a00e0; margin-bottom: 5px; font-size: 1.5em; font-weight: 700; text-shadow: 1px 1px 2px rgba(0,0,0,0.1);'>🚀 {get_str('app_title')}</h1>"
            f"<div style='text-align: center; color: #555; font-size: 0.9em;'>{get_str('app_subtitle')}</div>"
        )

        # Default dosya yüklemeleri
        default_replacement_path = os.path.join(REPLACEMENT_RULES_KLASORU, "varsayilan_degisiklikler.txt")
        default_synonym_path = os.path.join(SYNONYM_RULES_KLASORU, "varsayilan_birlesimler.txt")
        default_addition_path = os.path.join(ADDITION_RULES_KLASORU, "varsayilan_eklemeler.txt")

        demo.load(fn=lambda: read_replacement_file_content(default_replacement_path) if os.path.exists(default_replacement_path) else "", outputs=tools_components["replacement_file_content"])
        demo.load(fn=lambda: read_synonym_file_content(default_synonym_path) if os.path.exists(default_synonym_path) else "", outputs=tools_components["synonym_file_content"])
        demo.load(fn=lambda: read_addition_file_content(default_addition_path) if os.path.exists(default_addition_path) else "", outputs=tools_components["addition_file_content"])

    demo.queue()
    return demo


# ==================== BAŞLANGIÇ ====================

# Klasörleri oluştur
folders_to_create = [
    KATEGORILER_KLASORU, ALT_KATEGORILER_KLASORU, 
    REPLACEMENT_RULES_KLASORU, SYNONYM_RULES_KLASORU, ADDITION_RULES_KLASORU,
    CONFIGLIST_KLASORU, "data/configs"
]
for folder in folders_to_create:
    os.makedirs(folder, exist_ok=True)

# Varsayılan dosyaları oluştur
default_replacement_file_path = os.path.join(REPLACEMENT_RULES_KLASORU, "varsayilan_degisiklikler.txt")
if not os.path.exists(default_replacement_file_path):
    with open(default_replacement_file_path, "w", encoding="utf-8") as f:
        f.write("# Her satıra bir kural: eski_etiket_regex -> yeni_etiket\n")
        f.write("# Örnek: blue_hair -> cyan hair\n")

default_synonym_file_path = os.path.join(SYNONYM_RULES_KLASORU, "varsayilan_birlesimler.txt")
if not os.path.exists(default_synonym_file_path):
    with open(default_synonym_file_path, "w", encoding="utf-8") as f:
        f.write("# Her satıra bir kural: ana_etiket: silinecek_es_anlamli_etiket1, silinecek_es_anlamli_etiket2\n")
        f.write("# Örnek: panties: underwear\n")

default_addition_file_path = os.path.join(ADDITION_RULES_KLASORU, "varsayilan_eklemeler.txt")
if not os.path.exists(default_addition_file_path):
    with open(default_addition_file_path, "w", encoding="utf-8") as f:
        f.write("best quality, masterpiece\n")

default_configlist_file_path = os.path.join(CONFIGLIST_KLASORU, "varsayilan_isimler.txt")
if not os.path.exists(default_configlist_file_path):
    with open(default_configlist_file_path, "w", encoding="utf-8") as f:
        f.write("Güzel_Çiçek\nHarika_Manzara\nGizemli_Sanat\n")

# Demo oluştur
demo = create_ui()

# Başlat
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)