apak commited on
Commit
1f674f9
·
verified ·
1 Parent(s): 0ee149c

Update ner_logic.py

Browse files
Files changed (1) hide show
  1. ner_logic.py +19 -14
ner_logic.py CHANGED
@@ -6,7 +6,7 @@ import spaces
6
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
7
  from labels_config import CUSTOM_LABELS
8
 
9
- # Modellerin Yüklenmesi
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)
@@ -25,22 +25,27 @@ def refine_labels_batch(misc_items, full_sentence):
25
  if not misc_items: return []
26
  device = "cuda" if torch.cuda.is_available() else "cpu"
27
 
28
- label_rules = "".join([f"### {k}\n- TANIM: {v['tanim']}\n" for k, v in CUSTOM_LABELS.items()])
29
- targets_text = "".join([f"\nVARLIK {i+1}: {item['word']}\nWIKI: {item['wiki']}\n" for i, item in enumerate(misc_items)])
30
 
31
- prompt = f"""Sen semantik analiz uzmanısın. 4 adımlı CoT protokolüyle analiz yap.
32
- CÜMLE: "{full_sentence}"
33
- VARLIKLAR: {targets_text}
34
- ETİKETLER: {label_rules}
35
- ÇIKTI: Sadece JSON listesi döndür. Her nesne 'varlik', 'analiz', 'karar' içermeli."""
36
 
37
- messages = [{"role": "user", "content": prompt}]
38
- text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
39
- model_inputs = tokenizer([text], return_tensors="pt").to(device)
 
 
 
 
 
 
 
40
 
41
  try:
42
- generated_ids = llm_model.generate(model_inputs.input_ids, max_new_tokens=1500, do_sample=False)
43
- full_output = tokenizer.batch_decode(generated_ids[:, model_inputs.input_ids.shape[1]:], skip_special_tokens=True)[0].strip()
44
- json_match = re.search(r'\[\s*\{.*\}\s*\]', full_output, re.DOTALL)
45
  return json.loads(json_match.group(0)) if json_match else []
46
  except: return []
 
6
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
7
  from labels_config import CUSTOM_LABELS
8
 
9
+ # Modeller
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)
 
25
  if not misc_items: return []
26
  device = "cuda" if torch.cuda.is_available() else "cpu"
27
 
28
+ label_rules = "".join([f"### {k}: {v['tanim']}\n" for k, v in CUSTOM_LABELS.items()])
29
+ targets = "".join([f"- VARLIK: {item['word']} | WIKI: {item['wiki']}\n" for item in misc_items])
30
 
31
+ prompt = f"""CÜMLE: "{full_sentence}"
32
+ Aşağıdaki varlıkları analiz et. Her biri için SADECE şu JSON formatında yanıt ver:
33
+ [{{ "varlik": "...", "analiz": "4 adımlı kısa CoT", "karar": "ETIKET" }}]
 
 
34
 
35
+ VARLIKLAR:
36
+ {targets}
37
+
38
+ ETİKETLER:
39
+ {label_rules}
40
+
41
+ NOT: 'karar' alanına asla True/False yazma, sadece yukarıdaki etiket isimlerinden birini yaz."""
42
+
43
+ inputs = tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True)
44
+ model_inputs = tokenizer([inputs], return_tensors="pt").to(device)
45
 
46
  try:
47
+ generated_ids = llm_model.generate(model_inputs.input_ids, max_new_tokens=1000, do_sample=False)
48
+ output = tokenizer.batch_decode(generated_ids[:, model_inputs.input_ids.shape[1]:], skip_special_tokens=True)[0]
49
+ json_match = re.search(r'\[\s*\{.*\}\s*\]', output, re.DOTALL)
50
  return json.loads(json_match.group(0)) if json_match else []
51
  except: return []