Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,63 +43,104 @@ except Exception as e:
|
|
| 43 |
# --- 4. YARDIMCI FONKSİYONLAR (WIKIPEDIA & GPU) ---
|
| 44 |
|
| 45 |
def get_wiki_summary(term):
|
| 46 |
-
"""Wikipedia'dan terim hakkında özet bilgi çeker."""
|
| 47 |
try:
|
| 48 |
wikipedia.set_lang("tr")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
return None
|
| 56 |
|
| 57 |
@spaces.GPU
|
| 58 |
def refine_label_with_llm(entity_text, wiki_context, custom_label_definitions):
|
| 59 |
-
"""
|
|
|
|
|
|
|
| 60 |
if not IS_LLM_ENABLED or gen_pipe is None:
|
| 61 |
return "MISC", "LLM aktif değil."
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
if gen_pipe.device.type != "cuda":
|
| 65 |
gen_pipe.model.to("cuda")
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
prompt = f"""Sen uzman bir veri sınıflandırma sistemisin.
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
| 74 |
ETİKET TANIMLARI:
|
| 75 |
{label_definitions_str}
|
|
|
|
| 76 |
|
| 77 |
-
GÖREV:
|
| 78 |
VARLIK: {entity_text}
|
| 79 |
-
|
| 80 |
|
| 81 |
-
ÇIKTI FORMATI
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
messages = [{"role": "user", "content": prompt}]
|
| 87 |
|
| 88 |
try:
|
| 89 |
-
|
|
|
|
| 90 |
full_output = outputs[0]["generated_text"][-1]["content"].strip()
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
match_reasoning = re.search(r'AKIL YÜRÜTME:\s*(.*)', full_output, re.IGNORECASE | re.DOTALL)
|
| 97 |
-
cog_output = match_reasoning.group(1).strip().replace('\n', ' ') if match_reasoning else ""
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
# --- 5. ANA İŞLEM HATTI (PIPELINE) ---
|
| 104 |
|
| 105 |
def advanced_ner_pipeline(text, target_labels, progress=gr.Progress()):
|
|
|
|
| 43 |
# --- 4. YARDIMCI FONKSİYONLAR (WIKIPEDIA & GPU) ---
|
| 44 |
|
| 45 |
def get_wiki_summary(term):
|
|
|
|
| 46 |
try:
|
| 47 |
wikipedia.set_lang("tr")
|
| 48 |
+
# auto_suggest=True sayesinde 'Microsoft Windows' yazılsa bile doğru sayfayı bulur
|
| 49 |
+
page = wikipedia.page(term, auto_suggest=True, redirect=True)
|
| 50 |
+
return page.summary[:600] # İlk 600 karakter LLM için yeterli bağlam sağlar
|
| 51 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 52 |
+
# Eğer çok seçenek çıkarsa ilk seçeneği al
|
| 53 |
+
try:
|
| 54 |
+
return wikipedia.summary(e.options[0], sentences=2)
|
| 55 |
+
except: return None
|
| 56 |
+
except:
|
| 57 |
return None
|
| 58 |
|
| 59 |
@spaces.GPU
|
| 60 |
def refine_label_with_llm(entity_text, wiki_context, custom_label_definitions):
|
| 61 |
+
"""
|
| 62 |
+
Paylaştığınız çalışan örnek üzerinden güncellenmiş LLM fonksiyonu.
|
| 63 |
+
"""
|
| 64 |
if not IS_LLM_ENABLED or gen_pipe is None:
|
| 65 |
return "MISC", "LLM aktif değil."
|
| 66 |
|
| 67 |
+
# ZeroGPU üzerinde modeli GPU'ya taşıma kontrolü
|
| 68 |
+
if next(gen_pipe.model.parameters()).device.type != "cuda":
|
| 69 |
gen_pipe.model.to("cuda")
|
| 70 |
|
| 71 |
+
label_definitions_str = "\n".join(
|
| 72 |
+
[f"- {k}: {v}" for k, v in custom_label_definitions.items()]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Çalıştığı onaylanmış Few-Shot örnek yapısı
|
| 76 |
+
few_shot_examples = """
|
| 77 |
+
ÖRNEK 1 (NORP İÇİN):
|
| 78 |
+
VARLIK: Türk
|
| 79 |
+
BAĞLAM: Türkler, Türkiye Cumhuriyeti'nde yaşayan ve Türkçe konuşan büyük bir etnik gruptur. (NORP tanımı ile eşleşir.)
|
| 80 |
+
SONUÇ: NORP
|
| 81 |
+
|
| 82 |
+
ÖRNEK 2 (SOFTWARE İÇİN):
|
| 83 |
+
VARLIK: Photoshop
|
| 84 |
+
BAĞLAM: Adobe tarafından geliştirilen, fotoğraf ve resim düzenleme programı. (SOFTWARE tanımı ile eşleşir.)
|
| 85 |
+
SONUÇ: SOFTWARE
|
| 86 |
+
|
| 87 |
+
ÖRNEK 3 (PRODUCT İÇİN):
|
| 88 |
+
VARLIK: iPhone 15 Pro
|
| 89 |
+
BAĞLAM: Apple tarafından üretilen akıllı telefon modelidir. (PRODUCT tanımı ile eşleşir.)
|
| 90 |
+
SONUÇ: PRODUCT
|
| 91 |
+
"""
|
| 92 |
|
| 93 |
prompt = f"""Sen uzman bir veri sınıflandırma sistemisin.
|
| 94 |
+
Görevin, aşağıdaki BAĞLAM'ı okumak ve ETİKET TANIMLARI'ndan hangisinin varlığa en uygun olduğunu belirlemektir.
|
| 95 |
+
|
| 96 |
+
Aşağıdaki adımları izle:
|
| 97 |
+
1. BAĞLAM'ı analiz et.
|
| 98 |
+
2. ETİKET TANIMLARI ile karşılaştır.
|
| 99 |
+
3. BULDUĞUN EŞLEŞMEYİ VE SEÇİMİ TEK SATIRDA ÇIKTI FORMATINA UYGUN OLARAK VER.
|
| 100 |
+
|
| 101 |
+
{few_shot_examples}
|
| 102 |
|
| 103 |
+
---
|
| 104 |
ETİKET TANIMLARI:
|
| 105 |
{label_definitions_str}
|
| 106 |
+
---
|
| 107 |
|
| 108 |
+
GÖREV VARLIĞI:
|
| 109 |
VARLIK: {entity_text}
|
| 110 |
+
BAĞLAM (Wikipedia): {wiki_context}
|
| 111 |
|
| 112 |
+
ÇIKTI FORMATI:
|
| 113 |
+
Gerekçe: [Tanımlarla eşleşen anahtar kelime veya tanım]
|
| 114 |
+
Cevap: [SEÇİLEN ETİKET]
|
| 115 |
+
|
| 116 |
+
Gerekçe:"""
|
| 117 |
|
| 118 |
messages = [{"role": "user", "content": prompt}]
|
| 119 |
|
| 120 |
try:
|
| 121 |
+
# max_new_tokens değerini kısa tutarak modelin saçmalamasını önlüyoruz
|
| 122 |
+
outputs = gen_pipe(messages, max_new_tokens=60, do_sample=False, temperature=0.1)
|
| 123 |
full_output = outputs[0]["generated_text"][-1]["content"].strip()
|
| 124 |
|
| 125 |
+
# Paylaştığınız 'Cevap:' satırı bulma mantığı
|
| 126 |
+
final_label = "MISC"
|
| 127 |
+
reasoning = "Belirlenemedi"
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
lines = full_output.split('\n')
|
| 130 |
+
for line in lines:
|
| 131 |
+
if 'Cevap:' in line:
|
| 132 |
+
raw_label = line.split('Cevap:')[-1].strip().upper()
|
| 133 |
+
valid_labels = list(custom_label_definitions.keys())
|
| 134 |
+
if raw_label in valid_labels:
|
| 135 |
+
final_label = raw_label
|
| 136 |
+
if 'Gerekçe:' in line or not line.startswith('Cevap:'):
|
| 137 |
+
reasoning = line.replace('Gerekçe:', '').strip()
|
| 138 |
+
|
| 139 |
+
return final_label, reasoning
|
| 140 |
|
| 141 |
+
except Exception as e:
|
| 142 |
+
return "MISC", f"Hata: {str(e)}"
|
| 143 |
+
|
| 144 |
# --- 5. ANA İŞLEM HATTI (PIPELINE) ---
|
| 145 |
|
| 146 |
def advanced_ner_pipeline(text, target_labels, progress=gr.Progress()):
|