Spaces:
Sleeping
Sleeping
Update ner_logic.py
Browse files- ner_logic.py +16 -7
ner_logic.py
CHANGED
|
@@ -1,29 +1,39 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import spaces
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 6 |
from labels_config import CUSTOM_LABELS
|
| 7 |
-
from prompts import SYSTEM_COT_PROMPT
|
| 8 |
|
| 9 |
-
#
|
| 10 |
ner_pipe = pipeline("ner", model="xlm-roberta-large-finetuned-conll03-english", aggregation_strategy="simple", device=-1)
|
| 11 |
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
llm_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@spaces.GPU
|
| 16 |
def refine_labels_batch(misc_items, full_sentence):
|
| 17 |
if not misc_items: return []
|
| 18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
|
| 20 |
-
# Etiket kurallari metni oluşturma
|
| 21 |
label_rules = "".join([f"### {k}: {v['tanim']}\n" for k, v in CUSTOM_LABELS.items()])
|
| 22 |
-
|
| 23 |
-
# Varlik ve Wiki metni oluşturma
|
| 24 |
targets_text = "".join([f"- VARLIK: {item['word']} | WIKI: {item['wiki']}\n" for item in misc_items])
|
| 25 |
|
| 26 |
-
# Harici promptu doldurma
|
| 27 |
final_prompt = SYSTEM_COT_PROMPT.format(
|
| 28 |
label_rules=label_rules,
|
| 29 |
full_sentence=full_sentence,
|
|
@@ -41,7 +51,6 @@ def refine_labels_batch(misc_items, full_sentence):
|
|
| 41 |
json_match = re.search(r'\[\s*\{.*\}\s*\]', output, re.DOTALL)
|
| 42 |
if json_match:
|
| 43 |
results = json.loads(json_match.group(0))
|
| 44 |
-
# Karar alaninda True/False temizligi
|
| 45 |
for r in results:
|
| 46 |
if not isinstance(r.get("karar"), str) or r["karar"] in ["True", "False"]:
|
| 47 |
r["karar"] = "MISC"
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import wikipedia # Hata almamak için pip install wikipedia yapılmalı
|
| 3 |
import re
|
| 4 |
import json
|
| 5 |
import spaces
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 7 |
from labels_config import CUSTOM_LABELS
|
| 8 |
+
from prompts import SYSTEM_COT_PROMPT
|
| 9 |
|
| 10 |
+
# --- MODELLER ---
|
| 11 |
ner_pipe = pipeline("ner", model="xlm-roberta-large-finetuned-conll03-english", aggregation_strategy="simple", device=-1)
|
| 12 |
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 14 |
llm_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
|
| 15 |
|
| 16 |
+
def get_wiki_summary(term):
|
| 17 |
+
"""Wikipedia'dan bilgi çeken kritik fonksiyon."""
|
| 18 |
+
try:
|
| 19 |
+
wikipedia.set_lang("tr")
|
| 20 |
+
search_results = wikipedia.search(term)
|
| 21 |
+
if not search_results:
|
| 22 |
+
return "Wikipedia'da bu terim için sonuç bulunamadı."
|
| 23 |
+
# İlk sonucun özetini al
|
| 24 |
+
summary = wikipedia.summary(search_results[0], sentences=2, auto_suggest=False)
|
| 25 |
+
return summary
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Wikipedia sorgu hatası: {str(e)}"
|
| 28 |
+
|
| 29 |
@spaces.GPU
|
| 30 |
def refine_labels_batch(misc_items, full_sentence):
|
| 31 |
if not misc_items: return []
|
| 32 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
|
|
|
|
| 34 |
label_rules = "".join([f"### {k}: {v['tanim']}\n" for k, v in CUSTOM_LABELS.items()])
|
|
|
|
|
|
|
| 35 |
targets_text = "".join([f"- VARLIK: {item['word']} | WIKI: {item['wiki']}\n" for item in misc_items])
|
| 36 |
|
|
|
|
| 37 |
final_prompt = SYSTEM_COT_PROMPT.format(
|
| 38 |
label_rules=label_rules,
|
| 39 |
full_sentence=full_sentence,
|
|
|
|
| 51 |
json_match = re.search(r'\[\s*\{.*\}\s*\]', output, re.DOTALL)
|
| 52 |
if json_match:
|
| 53 |
results = json.loads(json_match.group(0))
|
|
|
|
| 54 |
for r in results:
|
| 55 |
if not isinstance(r.get("karar"), str) or r["karar"] in ["True", "False"]:
|
| 56 |
r["karar"] = "MISC"
|