ciyidogan commited on
Commit
01ac332
·
verified ·
1 Parent(s): f7d2bfe

Update intent_utils.py

Browse files
Files changed (1) hide show
  1. intent_utils.py +63 -4
intent_utils.py CHANGED
@@ -5,7 +5,14 @@ import shutil
5
  import re
6
  import traceback
7
  from datasets import Dataset
8
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments, default_data_collator, AutoConfig
 
 
 
 
 
 
 
9
  from log import log
10
  from core import INTENT_MODELS
11
 
@@ -62,7 +69,7 @@ def background_training(project_name, intents, model_id, output_path, confidence
62
  model=model,
63
  args=TrainingArguments(output_path, per_device_train_batch_size=4, num_train_epochs=3, logging_steps=10, save_strategy="no", report_to=[]),
64
  train_dataset=tokenized,
65
- data_collator=default_data_collator
66
  )
67
  trainer.train()
68
 
@@ -97,10 +104,62 @@ def background_training(project_name, intents, model_id, output_path, confidence
97
  INTENT_MODELS[project_name] = {
98
  "model": model,
99
  "tokenizer": tokenizer,
100
- "label2id": label2id
101
  }
102
  log(f"✅ Intent eğitimi tamamlandı ve '{project_name}' modeli yüklendi.")
103
 
104
  except Exception as e:
105
  log(f"❌ Intent eğitimi hatası: {e}")
106
- traceback.print_exc()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import re
6
  import traceback
7
  from datasets import Dataset
8
+ from transformers import (
9
+ AutoTokenizer,
10
+ AutoModelForSequenceClassification,
11
+ Trainer,
12
+ TrainingArguments,
13
+ default_data_collator,
14
+ AutoConfig,
15
+ )
16
  from log import log
17
  from core import INTENT_MODELS
18
 
 
69
  model=model,
70
  args=TrainingArguments(output_path, per_device_train_batch_size=4, num_train_epochs=3, logging_steps=10, save_strategy="no", report_to=[]),
71
  train_dataset=tokenized,
72
+ data_collator=default_data_collator,
73
  )
74
  trainer.train()
75
 
 
104
  INTENT_MODELS[project_name] = {
105
  "model": model,
106
  "tokenizer": tokenizer,
107
+ "label2id": label2id,
108
  }
109
  log(f"✅ Intent eğitimi tamamlandı ve '{project_name}' modeli yüklendi.")
110
 
111
  except Exception as e:
112
  log(f"❌ Intent eğitimi hatası: {e}")
113
+ traceback.print_exc()
114
+
115
+ def extract_parameters(variables_list, user_input):
116
+ for pattern in variables_list:
117
+ regex = re.sub(r"(\w+):\{(.+?)\}", r"(?P<\1>.+?)", pattern)
118
+ match = re.match(regex, user_input)
119
+ if match:
120
+ return [{"key": k, "value": v} for k, v in match.groupdict().items()]
121
+ return []
122
+
123
+ def resolve_placeholders(text: str, session: dict, variables: dict) -> str:
124
+ def replacer(match):
125
+ full = match.group(1)
126
+ try:
127
+ if full.startswith("variables."):
128
+ key = full.split(".", 1)[1]
129
+ return str(variables.get(key, f"{{{full}}}"))
130
+ elif full.startswith("session."):
131
+ key = full.split(".", 1)[1]
132
+ return str(session.get("variables", {}).get(key, f"{{{full}}}"))
133
+ elif full.startswith("auth_tokens."):
134
+ parts = full.split(".")
135
+ if len(parts) == 3:
136
+ intent, token_type = parts[1], parts[2]
137
+ return str(session.get("auth_tokens", {}).get(intent, {}).get(token_type, f"{{{full}}}"))
138
+ else:
139
+ return f"{{{full}}}"
140
+ else:
141
+ return f"{{{full}}}"
142
+ except Exception:
143
+ return f"{{{full}}}"
144
+
145
+ return re.sub(r"\{([^{}]+)\}", replacer, text)
146
+
147
+ def validate_variable_formats(variables, variable_format_map, data_formats):
148
+ errors = {}
149
+ for var_name, format_name in variable_format_map.items():
150
+ value = variables.get(var_name)
151
+ if value is None:
152
+ continue
153
+
154
+ format_def = data_formats.get(format_name)
155
+ if not format_def:
156
+ continue
157
+
158
+ if "valid_options" in format_def:
159
+ if value not in format_def["valid_options"]:
160
+ errors[var_name] = format_def.get("error_message", f"{var_name} değeri geçersiz.")
161
+ elif "pattern" in format_def:
162
+ if not re.fullmatch(format_def["pattern"], value):
163
+ errors[var_name] = format_def.get("error_message", f"{var_name} formatı geçersiz.")
164
+
165
+ return len(errors) == 0, errors