Spaces:
Runtime error
Runtime error
| import os | |
| import re | |
| import random | |
| import gradio as gr | |
| REFINELIST_PATH = "data/text_lists/RafineListesi.txt" | |
| REFINELIST_FILENAME = "RafineListesi.txt" | |
| def clean_and_uniquify_text_simple(text): | |
| """Metni temizler, alt çizgileri boşluğa çevirir ve etiketleri tekilleştirir.""" | |
| if not text: return "" | |
| cleaned = re.sub(r'[\s_]+', ' ', text).strip() | |
| tags = {t.strip() for t in cleaned.split(',') if t.strip()} | |
| return ", ".join(sorted(tags)) | |
| def convert_lines_to_comma(text): | |
| """Satırları virgülle ayrılmış metne çevirir.""" | |
| lines = [line.strip() for line in text.split('\n') if line.strip()] | |
| if not lines: return "", gr.update(value="⚠️ Girişte geçerli metin bulunamadı.", visible=True) | |
| return ", ".join(lines), gr.update(value="✅ Satırlar virgüllü metne çevrildi.", visible=True) | |
| def convert_comma_to_lines(text): | |
| """Virgülle ayrılmış metni satırlara çevirir.""" | |
| lines = [line.strip() for line in text.split(',') if line.strip()] | |
| if not lines: return "", gr.update(value="⚠️ Girişte geçerli metin bulunamadı.", visible=True) | |
| return "\n".join(lines), gr.update(value="✅ Virgüllü metin satırlara çevrildi.", visible=True) | |
| def select_and_copy_random_lines(text_input_raw, num_lines_to_copy_slider): | |
| """Metinden rastgele satırları seçer.""" | |
| lines = [line.strip() for line in text_input_raw.split('\n') if line.strip()] | |
| num_lines = int(num_lines_to_copy_slider) | |
| if not lines: return "", gr.update(value="⚠️ Girişte geçerli metin bulunamadı.", visible=True) | |
| if num_lines > len(lines): | |
| return "", gr.update(value=f"⚠️ {len(lines)} satır var, ancak {num_lines} satır istendi. İşlem iptal edildi.", visible=True) | |
| selected = random.sample(lines, num_lines) | |
| return "\n".join(selected), gr.update(value=f"✅ {num_lines} adet rastgele satır seçildi.", visible=True) | |
| def refine_text_by_file(input_text: str): | |
| """Kullanıcı metnini RafineListesi.txt dosyasındaki anahtar kelimelere göre filtreler.""" | |
| if not input_text.strip(): | |
| return "", gr.update(value="⚠️ Girişte geçerli metin bulunamadı.", visible=True) | |
| target_path = REFINELIST_PATH | |
| try: | |
| if not os.path.exists(target_path): | |
| os.makedirs(os.path.dirname(target_path), exist_ok=True) | |
| with open(target_path, "w", encoding="utf-8") as f: f.write("") | |
| return input_text, gr.update(value=f"⚠️ Uyarı: '{target_path}' dosyası bulunamadı, boş bir dosya oluşturuldu.", visible=True) | |
| with open(target_path, 'r', encoding='utf-8') as f: | |
| kaldirilacak_kelimeler = [satir.strip().lower() for satir in f if satir.strip()] | |
| if not kaldirilacak_kelimeler: | |
| return input_text, gr.update(value="⚠️ Kaldırılacak kelime listesi boş.", visible=True) | |
| except Exception as e: | |
| return input_text, gr.update(value=f"❌ Dosya okuma hatası: {e}", visible=True) | |
| bolumler = [b.strip() for b in re.split(r'[, \n]+', input_text) if b.strip()] | |
| temiz_bolumler = [] | |
| kaldirilan_sayisi = 0 | |
| for bolum in bolumler: | |
| bolum_kucuk_harf = bolum.lower() | |
| gecerli_kalacak_mi = True | |
| for kelime in kaldirilacak_kelimeler: | |
| if kelime in bolum_kucuk_harf: | |
| gecerli_kalacak_mi = False; break | |
| if gecerli_kalacak_mi: temiz_bolumler.append(bolum) | |
| else: kaldirilan_sayisi += 1 | |
| temiz_bolumler_set = sorted(list(set(temiz_bolumler))) | |
| return ', '.join(temiz_bolumler_set), gr.update(value=f"✅ {len(temiz_bolumler_set)} etiket kaldı, {kaldirilan_sayisi} etiket kaldırıldı.", visible=True) | |
| def append_text_to_wildcard_file(text, filename): | |
| if not text.strip(): return gr.update(value="⚠️ Metin boş.") | |
| folder = "data/wildcards" | |
| os.makedirs(folder, exist_ok=True) | |
| filepath = os.path.join(folder, f"{filename}.txt") | |
| try: | |
| # Mevcut içeriği oku ve duplicate kontrolü yap | |
| existing_lines = set() | |
| if os.path.exists(filepath): | |
| with open(filepath, "r", encoding="utf-8") as f: | |
| existing_lines = {line.strip() for line in f if line.strip()} | |
| new_text = text.strip() | |
| # Eğer bu etiket listesi zaten eklenmişse uyarı ver | |
| if new_text in existing_lines: | |
| return gr.update(value=f"⚠️ Bu etiket listesi '{filename}' paketine zaten eklenmiş!", visible=True) | |
| # Eklenmemişse dosyaya ekle | |
| with open(filepath, "a", encoding="utf-8") as f: | |
| f.write(new_text + "\n") | |
| return gr.update(value=f"✅ '{filename}' paketine eklendi.", visible=True) | |
| except Exception as e: return gr.update(value=f"❌ Hata: {e}", visible=True) | |