ciyidogan commited on
Commit
96453da
·
verified ·
1 Parent(s): e1fecdb

Update controllers/admin_controller.py

Browse files
Files changed (1) hide show
  1. controllers/admin_controller.py +103 -70
controllers/admin_controller.py CHANGED
@@ -3,78 +3,111 @@ from core import service_config, llm_models
3
  from llm_model import LLMModel
4
  from intent_utils import background_training
5
  from log import log
6
- import json, os, shutil
7
 
8
  router = APIRouter()
9
 
10
  @router.post("/reload_config")
11
  async def reload_config(request: Request):
12
- try:
13
- body = await request.json()
14
- project_name = body.get("project_name")
15
- new_config_data = body.get("service_config")
16
-
17
- if not project_name or not new_config_data:
18
- return {"error": "project_name ve service_config gereklidir."}
19
-
20
- current_project = service_config.projects.get(project_name)
21
- incoming_project = new_config_data.get("projects", {}).get(project_name)
22
-
23
- if not incoming_project:
24
- return {"error": f"'{project_name}' proje tanımı yeni config içinde bulunamadı."}
25
-
26
- # Eğer farklılık yoksa erken çık
27
- if current_project == incoming_project:
28
- return {"status": "ok", "message": "Hiçbir değişiklik bulunamadı."}
29
-
30
- log(f"🔄 '{project_name}' güncellemesi tespit edildi, güncelleme başlatılıyor...")
31
-
32
- project_path = f"/data/projects/{project_name}"
33
- temp_path = f"/data/projects/{project_name}_temp"
34
-
35
- if os.path.exists(temp_path):
36
- shutil.rmtree(temp_path)
37
- os.makedirs(temp_path, exist_ok=True)
38
-
39
- llm_config = incoming_project["llm"]
40
- intents = incoming_project["intents"]
41
-
42
- # Geçici model yüklemesi
43
- temp_instance = LLMModel()
44
-
45
- if current_project["llm"]["model_base"] != llm_config["model_base"]:
46
- temp_instance.setup(service_config, llm_config, temp_path)
47
- else:
48
- temp_instance.model = llm_models[project_name].model
49
- temp_instance.tokenizer = llm_models[project_name].tokenizer
50
-
51
- if current_project["intents"] != intents:
52
- intent_model_path = os.path.join(temp_path, "intent", "trained_model")
53
- background_training(
54
- project_name,
55
- intents,
56
- llm_config["intent_model_id"],
57
- intent_model_path,
58
- llm_config["train_confidence_treshold"]
59
- )
60
- temp_instance.load_intent_model(intent_model_path)
61
- else:
62
- temp_instance.intent_model = llm_models[project_name].intent_model
63
- temp_instance.intent_tokenizer = llm_models[project_name].intent_tokenizer
64
- temp_instance.intent_label2id = llm_models[project_name].intent_label2id
65
-
66
- # Ana klasörü sıfırla ve temp içeriğini taşı
67
- if os.path.exists(project_path):
68
- shutil.rmtree(project_path)
69
- shutil.copytree(temp_path, project_path)
70
-
71
- llm_models[project_name] = temp_instance
72
- service_config.projects[project_name] = incoming_project
73
-
74
- log(f"✅ '{project_name}' güncellemesi tamamlandı ve belleğe alındı.")
75
- return {"status": "ok", "message": f"'{project_name}' projesi güncellendi ve yeniden yüklendi."}
76
-
77
- except Exception as e:
78
- log(f"❌ reload_config hatası: {e}")
79
- return {"error": str(e)}
80
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from llm_model import LLMModel
4
  from intent_utils import background_training
5
  from log import log
6
+ import json, os, shutil, threading
7
 
8
  router = APIRouter()
9
 
10
  @router.post("/reload_config")
11
  async def reload_config(request: Request):
12
+ body = await request.json()
13
+ project_name = body.get("project_name")
14
+ new_config_data = body.get("service_config")
15
+
16
+ if not project_name or not new_config_data:
17
+ return {"error": "project_name ve service_config gereklidir."}
18
+
19
+ def background_reload():
20
+ try:
21
+ current_project = service_config.projects.get(project_name)
22
+ incoming_project = new_config_data.get("projects", {}).get(project_name)
23
+
24
+ if not incoming_project:
25
+ log(f"❌ '{project_name}' yeni config içinde bulunamadı, işlem durduruldu.")
26
+ return
27
+
28
+ project_path = f"/data/projects/{project_name}"
29
+ temp_path = f"/data/projects/{project_name}_temp"
30
+
31
+ if os.path.exists(temp_path):
32
+ shutil.rmtree(temp_path)
33
+ os.makedirs(temp_path, exist_ok=True)
34
+
35
+ llm_config = incoming_project["llm"]
36
+ intents = incoming_project["intents"]
37
+
38
+ temp_instance = LLMModel()
39
+
40
+ # 🆕 Yeni proje ekleniyor
41
+ if current_project is None:
42
+ log(f"🆕 Yeni proje '{project_name}' tespit edildi, yükleme başlatılıyor...")
43
+
44
+ temp_instance.setup(service_config, llm_config, temp_path)
45
+ intent_model_path = os.path.join(temp_path, "intent", "trained_model")
46
+ background_training(
47
+ project_name,
48
+ intents,
49
+ llm_config["intent_model_id"],
50
+ intent_model_path,
51
+ llm_config["train_confidence_treshold"]
52
+ )
53
+ temp_instance.load_intent_model(intent_model_path)
54
+
55
+ if os.path.exists(project_path):
56
+ shutil.rmtree(project_path)
57
+ shutil.copytree(temp_path, project_path)
58
+
59
+ llm_models[project_name] = temp_instance
60
+ service_config.projects[project_name] = incoming_project
61
+
62
+ log(f"✅ Yeni proje '{project_name}' başarıyla yüklendi ve belleğe alındı.")
63
+ return
64
+
65
+ # 🔄 Var olan projede değişiklik varsa güncelle
66
+ if current_project == incoming_project:
67
+ log(f"ℹ️ '{project_name}' için değişiklik bulunamadı, işlem atlandı.")
68
+ return
69
+
70
+ log(f"🔄 '{project_name}' güncellemesi tespit edildi, güncelleme başlatılıyor...")
71
+
72
+ # Ana model değiştiyse yükle
73
+ if current_project["llm"]["model_base"] != llm_config["model_base"]:
74
+ temp_instance.setup(service_config, llm_config, temp_path)
75
+ else:
76
+ temp_instance.model = llm_models[project_name].model
77
+ temp_instance.tokenizer = llm_models[project_name].tokenizer
78
+
79
+ # Intent değiştiyse yeniden eğit
80
+ if current_project["intents"] != intents:
81
+ intent_model_path = os.path.join(temp_path, "intent", "trained_model")
82
+ background_training(
83
+ project_name,
84
+ intents,
85
+ llm_config["intent_model_id"],
86
+ intent_model_path,
87
+ llm_config["train_confidence_treshold"]
88
+ )
89
+ temp_instance.load_intent_model(intent_model_path)
90
+ else:
91
+ temp_instance.intent_model = llm_models[project_name].intent_model
92
+ temp_instance.intent_tokenizer = llm_models[project_name].intent_tokenizer
93
+ temp_instance.intent_label2id = llm_models[project_name].intent_label2id
94
+
95
+ if os.path.exists(project_path):
96
+ shutil.rmtree(project_path)
97
+ shutil.copytree(temp_path, project_path)
98
+
99
+ llm_models[project_name] = temp_instance
100
+ service_config.projects[project_name] = incoming_project
101
+
102
+ log(f"✅ '{project_name}' güncellemesi tamamlandı ve belleğe alındı.")
103
+
104
+ except Exception as e:
105
+ log(f"❌ reload_config background hatası: {e}")
106
+
107
+ # Arka planda başlat
108
+ threading.Thread(target=background_reload, daemon=True).start()
109
+
110
+ return {
111
+ "status": "accepted",
112
+ "message": f"'{project_name}' için güncelleme arka planda başlatıldı. İşlem loglardan takip edilebilir."
113
+ }