Spaces:
Sleeping
Sleeping
Auto commit at 09-2025-08 20:34:30
Browse files- lily_llm_api/app_v2.py +49 -11
- lily_llm_api/models/__init__.py +0 -6
- lily_llm_api/models/dialogpt_medium.py +0 -82
- lily_llm_api/models/kanana_1_5_2_1b_instruct.py +0 -93
- lily_llm_api/models/kanana_1_5_v_3b_instruct.py +6 -3
- lily_llm_api/models/kanana_1_5_v_3b_instruct_250809_0055.py +0 -256
- lily_llm_api/models/kanana_nano_2_1b_instruct.py +0 -95
- lily_llm_api/models/mistral_7b_instruct.py +0 -103
- lily_llm_api/models/polyglot_ko_1_3b.py +0 -102
- lily_llm_api/models/polyglot_ko_1_3b_chat.py +28 -20
- lily_llm_api/models/polyglot_ko_5_8b.py +0 -104
- lily_llm_api/models/polyglot_ko_5_8b_chat.py +26 -17
- lily_llm_core/config.py +2 -2
- test.py +69 -47
- test_hf_with_token.py +60 -0
- test_log.md +165 -0
lily_llm_api/app_v2.py
CHANGED
|
@@ -176,7 +176,7 @@ def select_model_interactive():
|
|
| 176 |
try:
|
| 177 |
# choice = input(f"\nπ μ¬μ©ν λͺ¨λΈ λ²νΈλ₯Ό μ ννμΈμ (1-{len(available_models)}): ")
|
| 178 |
# selected_model = available_models[int(choice) - 1]
|
| 179 |
-
selected_model = available_models[
|
| 180 |
print(f"\nβ
'{selected_model['name']}' λͺ¨λΈμ μ ννμ΅λλ€.")
|
| 181 |
return selected_model['model_id']
|
| 182 |
except (ValueError, IndexError):
|
|
@@ -292,26 +292,55 @@ def generate_sync(prompt: str, image_data_list: Optional[List[bytes]], max_lengt
|
|
| 292 |
for key in all_image_metas[0].keys():
|
| 293 |
combined_image_metas[key] = [meta[key] for meta in all_image_metas]
|
| 294 |
|
| 295 |
-
# --- 2. ν둬ννΈ κ΅¬μ±
|
| 296 |
image_tokens = "<image>" * len(all_pixel_values)
|
| 297 |
-
#
|
| 298 |
formatted_prompt = f"<|im_start|>user\n{image_tokens}{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 299 |
|
| 300 |
-
# --- 3. ν ν¬λμ΄μ§
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
# --- 4. λͺ¨λΈ μμ± (κ³΅ν΅ μ€ν) ---
|
| 308 |
gen_config = current_profile.get_generation_config()
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
# max_length λ± μ¬μ©μ μ§μ νλΌλ―Έν°κ° μμΌλ©΄ gen_configμ λ°μ
|
| 311 |
if max_length is not None: gen_config['max_new_tokens'] = max_length
|
| 312 |
if temperature is not None: gen_config['temperature'] = temperature
|
| 313 |
if top_p is not None: gen_config['top_p'] = top_p
|
| 314 |
if do_sample is not None: gen_config['do_sample'] = do_sample
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
with torch.no_grad():
|
| 317 |
if image_processed:
|
|
@@ -364,8 +393,17 @@ async def generate(prompt: str = Form(...),
|
|
| 364 |
image_data = await img.read()
|
| 365 |
image_data_list.append(image_data)
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
processing_time = time.time() - start_time
|
| 371 |
logger.info(f"β
μμ± μλ£ ({processing_time:.2f}μ΄), μ΄λ―Έμ§ μ²λ¦¬: {result['image_processed']}")
|
|
|
|
| 176 |
try:
|
| 177 |
# choice = input(f"\nπ μ¬μ©ν λͺ¨λΈ λ²νΈλ₯Ό μ ννμΈμ (1-{len(available_models)}): ")
|
| 178 |
# selected_model = available_models[int(choice) - 1]
|
| 179 |
+
selected_model = available_models[0]
|
| 180 |
print(f"\nβ
'{selected_model['name']}' λͺ¨λΈμ μ ννμ΅λλ€.")
|
| 181 |
return selected_model['model_id']
|
| 182 |
except (ValueError, IndexError):
|
|
|
|
| 292 |
for key in all_image_metas[0].keys():
|
| 293 |
combined_image_metas[key] = [meta[key] for meta in all_image_metas]
|
| 294 |
|
| 295 |
+
# --- 2. ν둬ννΈ κ΅¬μ± ---
|
| 296 |
image_tokens = "<image>" * len(all_pixel_values)
|
| 297 |
+
# Kanana κΈ°λ³Έ ν¬λ§·. ν
μ€νΈ-only λͺ¨λΈμ profile.format_promptλ‘ λ체λ¨
|
| 298 |
formatted_prompt = f"<|im_start|>user\n{image_tokens}{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 299 |
|
| 300 |
+
# --- 3. ν ν¬λμ΄μ§ ---
|
| 301 |
+
if hasattr(tokenizer, 'encode_prompt'):
|
| 302 |
+
inputs = tokenizer.encode_prompt(prompt=formatted_prompt, image_meta=combined_image_metas)
|
| 303 |
+
input_ids = inputs['input_ids']
|
| 304 |
+
attention_mask = inputs['attention_mask']
|
| 305 |
+
else:
|
| 306 |
+
# ν
μ€νΈ-only λͺ¨λΈμ κΆμ₯ ν둬ννΈ μ¬μ©
|
| 307 |
+
if not getattr(current_profile, 'multimodal', False) and hasattr(current_profile, 'format_prompt'):
|
| 308 |
+
formatted_prompt = current_profile.format_prompt(prompt)
|
| 309 |
+
inputs = tokenizer(
|
| 310 |
+
formatted_prompt,
|
| 311 |
+
return_tensors="pt",
|
| 312 |
+
padding=True,
|
| 313 |
+
truncation=True,
|
| 314 |
+
max_length=256,
|
| 315 |
+
)
|
| 316 |
+
if 'token_type_ids' in inputs:
|
| 317 |
+
del inputs['token_type_ids']
|
| 318 |
+
input_ids = inputs['input_ids']
|
| 319 |
+
attention_mask = inputs['attention_mask']
|
| 320 |
+
|
| 321 |
+
if input_ids.dim() == 1:
|
| 322 |
+
input_ids = input_ids.unsqueeze(0)
|
| 323 |
+
if attention_mask.dim() == 1:
|
| 324 |
+
attention_mask = attention_mask.unsqueeze(0)
|
| 325 |
+
input_ids = input_ids.to(model.device)
|
| 326 |
+
attention_mask = attention_mask.to(model.device)
|
| 327 |
|
| 328 |
# --- 4. λͺ¨λΈ μμ± (κ³΅ν΅ μ€ν) ---
|
| 329 |
gen_config = current_profile.get_generation_config()
|
| 330 |
+
# CPUμμ κ³Όλν max_new_tokensλ λκΈ° μκ°μ ν¬κ² λλ¦Ό β κΈ°λ³Έ μνμ 보μμ μΌλ‘ μ‘°μ
|
| 331 |
+
if gen_config.get('max_new_tokens', 256) > 128 and (not torch.cuda.is_available()):
|
| 332 |
+
gen_config['max_new_tokens'] = 128
|
| 333 |
|
| 334 |
# max_length λ± μ¬μ©μ μ§μ νλΌλ―Έν°κ° μμΌλ©΄ gen_configμ λ°μ
|
| 335 |
if max_length is not None: gen_config['max_new_tokens'] = max_length
|
| 336 |
if temperature is not None: gen_config['temperature'] = temperature
|
| 337 |
if top_p is not None: gen_config['top_p'] = top_p
|
| 338 |
if do_sample is not None: gen_config['do_sample'] = do_sample
|
| 339 |
+
# pad/eos 보μ (μΌλΆ ν ν¬λμ΄μ λ pad_token λ―Έμ μ)
|
| 340 |
+
if gen_config.get('pad_token_id') is None and hasattr(tokenizer, 'pad_token_id'):
|
| 341 |
+
gen_config['pad_token_id'] = tokenizer.pad_token_id or tokenizer.eos_token_id
|
| 342 |
+
if gen_config.get('eos_token_id') is None and hasattr(tokenizer, 'eos_token_id'):
|
| 343 |
+
gen_config['eos_token_id'] = tokenizer.eos_token_id
|
| 344 |
|
| 345 |
with torch.no_grad():
|
| 346 |
if image_processed:
|
|
|
|
| 393 |
image_data = await img.read()
|
| 394 |
image_data_list.append(image_data)
|
| 395 |
|
| 396 |
+
# λ¨μΌ μ€ν 보μ₯: generate_syncλ μ€μ§ ν λ²λ§ νΈμΆ
|
| 397 |
+
result = await loop.run_in_executor(
|
| 398 |
+
executor,
|
| 399 |
+
generate_sync,
|
| 400 |
+
prompt,
|
| 401 |
+
image_data_list,
|
| 402 |
+
max_length,
|
| 403 |
+
temperature,
|
| 404 |
+
top_p,
|
| 405 |
+
do_sample,
|
| 406 |
+
)
|
| 407 |
|
| 408 |
processing_time = time.time() - start_time
|
| 409 |
logger.info(f"β
μμ± μλ£ ({processing_time:.2f}μ΄), μ΄λ―Έμ§ μ²λ¦¬: {result['image_processed']}")
|
lily_llm_api/models/__init__.py
CHANGED
|
@@ -15,12 +15,6 @@ from .polyglot_ko_5_8b_chat import PolyglotKo58bChatProfile
|
|
| 15 |
|
| 16 |
# μ¬μ© κ°λ₯ν λͺ¨λΈ νλ‘νλ€
|
| 17 |
AVAILABLE_MODELS = {
|
| 18 |
-
# "polyglot-ko-1.3b": PolyglotKo13bProfile,
|
| 19 |
-
# "dialogpt-medium": DialoGPTMediumProfile,
|
| 20 |
-
# "kanana-1.5-2.1b-instruct": Kanana15V21bInstructProfile,
|
| 21 |
-
# "kanana-nano-2.1b-instruct": KananaNano21bInstructProfile,
|
| 22 |
-
# "mistral-7b-instruct": Mistral7bInstructProfile,
|
| 23 |
-
# "polyglot-ko-5.8b": PolyglotKo58bProfile,
|
| 24 |
"polyglot-ko-1.3b-chat": PolyglotKo13bChatProfile,
|
| 25 |
"kanana-1.5-v-3b-instruct": Kanana15V3bInstructProfile,
|
| 26 |
"polyglot-ko-5.8b-chat": PolyglotKo58bChatProfile,
|
|
|
|
| 15 |
|
| 16 |
# μ¬μ© κ°λ₯ν λͺ¨λΈ νλ‘νλ€
|
| 17 |
AVAILABLE_MODELS = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"polyglot-ko-1.3b-chat": PolyglotKo13bChatProfile,
|
| 19 |
"kanana-1.5-v-3b-instruct": Kanana15V3bInstructProfile,
|
| 20 |
"polyglot-ko-5.8b-chat": PolyglotKo58bChatProfile,
|
lily_llm_api/models/dialogpt_medium.py
DELETED
|
@@ -1,82 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
DialoGPT-medium λͺ¨λΈ νλ‘ν
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from typing import Dict, Any, Tuple
|
| 7 |
-
import torch
|
| 8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
-
import logging
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
class DialoGPTMediumProfile:
|
| 14 |
-
"""DialoGPT-medium λͺ¨λΈ νλ‘ν"""
|
| 15 |
-
|
| 16 |
-
def __init__(self):
|
| 17 |
-
self.model_name = "microsoft/DialoGPT-medium"
|
| 18 |
-
self.local_path = None # μ¨λΌμΈμμ λ‘λ
|
| 19 |
-
self.display_name = "DialoGPT-medium"
|
| 20 |
-
self.description = "μμ΄ λνν λͺ¨λΈ (774M)"
|
| 21 |
-
self.language = "en"
|
| 22 |
-
self.model_size = "774M"
|
| 23 |
-
|
| 24 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 25 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 26 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 27 |
-
|
| 28 |
-
try:
|
| 29 |
-
# μ¨λΌμΈμμ λͺ¨λΈ λ‘λ
|
| 30 |
-
tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 31 |
-
model = AutoModelForCausalLM.from_pretrained(self.model_name)
|
| 32 |
-
|
| 33 |
-
if tokenizer.pad_token is None:
|
| 34 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
-
|
| 36 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 37 |
-
return model, tokenizer
|
| 38 |
-
|
| 39 |
-
except Exception as e:
|
| 40 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 41 |
-
raise
|
| 42 |
-
|
| 43 |
-
def format_prompt(self, user_input: str) -> str:
|
| 44 |
-
"""ν둬ννΈ ν¬λ§·ν
"""
|
| 45 |
-
return f"User: {user_input}\nAssistant:"
|
| 46 |
-
|
| 47 |
-
def extract_response(self, full_text: str, formatted_prompt: str) -> str:
|
| 48 |
-
"""μλ΅ μΆμΆ"""
|
| 49 |
-
if "Assistant:" in full_text:
|
| 50 |
-
response = full_text.split("Assistant:")[-1].strip()
|
| 51 |
-
else:
|
| 52 |
-
if formatted_prompt in full_text:
|
| 53 |
-
response = full_text.replace(formatted_prompt, "").strip()
|
| 54 |
-
else:
|
| 55 |
-
response = full_text.strip()
|
| 56 |
-
|
| 57 |
-
return response
|
| 58 |
-
|
| 59 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 60 |
-
"""μμ± μ€μ """
|
| 61 |
-
return {
|
| 62 |
-
"max_new_tokens": 50,
|
| 63 |
-
"temperature": 0.9,
|
| 64 |
-
"do_sample": True,
|
| 65 |
-
"top_k": 50,
|
| 66 |
-
"top_p": 0.95,
|
| 67 |
-
"repetition_penalty": 1.1,
|
| 68 |
-
"no_repeat_ngram_size": 3,
|
| 69 |
-
"pad_token_id": None,
|
| 70 |
-
"eos_token_id": None
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 74 |
-
"""λͺ¨λΈ μ 보"""
|
| 75 |
-
return {
|
| 76 |
-
"model_name": self.model_name,
|
| 77 |
-
"display_name": self.display_name,
|
| 78 |
-
"description": self.description,
|
| 79 |
-
"language": self.language,
|
| 80 |
-
"model_size": self.model_size,
|
| 81 |
-
"local_path": self.local_path
|
| 82 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/kanana_1_5_2_1b_instruct.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Kanana 1.5 2.1B Instruct λͺ¨λΈ νλ‘ν
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 8 |
-
from typing import Dict, Any, Tuple
|
| 9 |
-
import logging
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
class Kanana15V21bInstructProfile:
|
| 14 |
-
"""Kanana 1.5 2.1B Instruct λͺ¨λΈ νλ‘ν"""
|
| 15 |
-
|
| 16 |
-
def __init__(self):
|
| 17 |
-
self.model_name = "kakaocorp/kanana-1.5-2.1b-instruct-2505"
|
| 18 |
-
self.local_path = "./lily_llm_core/models/kanana-1.5-2.1b-instruct"
|
| 19 |
-
self.display_name = "Kanana 1.5 2.1B Instruct"
|
| 20 |
-
self.description = "Kakaoμ Kanana 1.5 2.1B Instruct λͺ¨λΈ"
|
| 21 |
-
self.language = ["ko", "en"]
|
| 22 |
-
self.model_size = "2.1B"
|
| 23 |
-
|
| 24 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 25 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 26 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 27 |
-
|
| 28 |
-
try:
|
| 29 |
-
# ν ν¬λμ΄μ λ‘λ
|
| 30 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 31 |
-
self.local_path,
|
| 32 |
-
trust_remote_code=True,
|
| 33 |
-
local_files_only=True
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# λͺ¨λΈ λ‘λ
|
| 37 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
-
self.local_path,
|
| 39 |
-
torch_dtype=torch.float32,
|
| 40 |
-
device_map="cpu",
|
| 41 |
-
low_cpu_mem_usage=True,
|
| 42 |
-
local_files_only=True
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# ν ν¬λμ΄μ μ€μ
|
| 46 |
-
if tokenizer.pad_token is None:
|
| 47 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 48 |
-
if tokenizer.eos_token is None:
|
| 49 |
-
tokenizer.eos_token = "</s>"
|
| 50 |
-
|
| 51 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 52 |
-
return model, tokenizer
|
| 53 |
-
|
| 54 |
-
except Exception as e:
|
| 55 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 56 |
-
raise
|
| 57 |
-
|
| 58 |
-
def format_prompt(self, user_input: str) -> str:
|
| 59 |
-
"""ν둬ννΈ ν¬λ§·ν
"""
|
| 60 |
-
return f"<|im_start|>user\n{user_input}<|im_end|>\n<|im_start|>assistant\n"
|
| 61 |
-
|
| 62 |
-
def extract_response(self, full_text: str, formatted_prompt: str) -> str:
|
| 63 |
-
"""μλ΅ μΆμΆ"""
|
| 64 |
-
if "<|im_start|>assistant\n" in full_text:
|
| 65 |
-
response = full_text.split("<|im_start|>assistant\n")[-1]
|
| 66 |
-
if "<|im_end|>" in response:
|
| 67 |
-
response = response.split("<|im_end|>")[0]
|
| 68 |
-
return response.strip()
|
| 69 |
-
return full_text.strip()
|
| 70 |
-
|
| 71 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 72 |
-
"""μμ± μ€μ """
|
| 73 |
-
return {
|
| 74 |
-
"max_new_tokens": 512,
|
| 75 |
-
"temperature": 0.7,
|
| 76 |
-
"top_p": 0.9,
|
| 77 |
-
"do_sample": True,
|
| 78 |
-
"repetition_penalty": 1.1,
|
| 79 |
-
"no_repeat_ngram_size": 3,
|
| 80 |
-
"pad_token_id": None, # ν ν¬λμ΄μ μμ μ€μ λ¨
|
| 81 |
-
"eos_token_id": None, # ν ν¬λμ΄μ μμ μ€μ λ¨
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 85 |
-
"""λͺ¨λΈ μ 보"""
|
| 86 |
-
return {
|
| 87 |
-
"model_name": self.model_name,
|
| 88 |
-
"display_name": self.display_name,
|
| 89 |
-
"description": self.description,
|
| 90 |
-
"language": self.language,
|
| 91 |
-
"model_size": self.model_size,
|
| 92 |
-
"local_path": self.local_path
|
| 93 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/kanana_1_5_v_3b_instruct.py
CHANGED
|
@@ -156,12 +156,13 @@ class Kanana15V3bInstructProfile:
|
|
| 156 |
|
| 157 |
if use_local:
|
| 158 |
# λ‘컬 λͺ¨λΈ: 컀μ€ν
λͺ¨λΈλ§ ν΄λμ€ μ¬μ©
|
| 159 |
-
logger.info("π DEBUG: λ‘컬 λͺ¨λΈ λ‘λ μλ")
|
|
|
|
| 160 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 161 |
model_path,
|
| 162 |
token=HF_TOKEN,
|
| 163 |
trust_remote_code=True,
|
| 164 |
-
torch_dtype=
|
| 165 |
local_files_only=True,
|
| 166 |
# low_cpu_mem_usage=True,
|
| 167 |
).to(DEVICE)
|
|
@@ -176,10 +177,12 @@ class Kanana15V3bInstructProfile:
|
|
| 176 |
raise
|
| 177 |
|
| 178 |
logger.info("π DEBUG: KananaVForConditionalGeneration.from_pretrained νΈμΆ")
|
|
|
|
|
|
|
| 179 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 180 |
model_path,
|
| 181 |
token=HF_TOKEN,
|
| 182 |
-
torch_dtype=
|
| 183 |
trust_remote_code=True,
|
| 184 |
cache_dir="/app/cache/transformers",
|
| 185 |
# device_map="auto",
|
|
|
|
| 156 |
|
| 157 |
if use_local:
|
| 158 |
# λ‘컬 λͺ¨λΈ: 컀μ€ν
λͺ¨λΈλ§ ν΄λμ€ μ¬μ©
|
| 159 |
+
logger.info("π DEBUG: λ‘컬 λͺ¨λΈ λ‘λ μλ")
|
| 160 |
+
selected_dtype = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 161 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 162 |
model_path,
|
| 163 |
token=HF_TOKEN,
|
| 164 |
trust_remote_code=True,
|
| 165 |
+
torch_dtype=selected_dtype,
|
| 166 |
local_files_only=True,
|
| 167 |
# low_cpu_mem_usage=True,
|
| 168 |
).to(DEVICE)
|
|
|
|
| 177 |
raise
|
| 178 |
|
| 179 |
logger.info("π DEBUG: KananaVForConditionalGeneration.from_pretrained νΈμΆ")
|
| 180 |
+
# CPU νκ²½μμ float16/bfloat16λ³΄λ€ float32κ° λ μμ μ μΈ κ²½μ°κ° λ§μ
|
| 181 |
+
selected_dtype = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 182 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 183 |
model_path,
|
| 184 |
token=HF_TOKEN,
|
| 185 |
+
torch_dtype=selected_dtype,
|
| 186 |
trust_remote_code=True,
|
| 187 |
cache_dir="/app/cache/transformers",
|
| 188 |
# device_map="auto",
|
lily_llm_api/models/kanana_1_5_v_3b_instruct_250809_0055.py
DELETED
|
@@ -1,256 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Kanana-1.5-v-3b-instruct λͺ¨λΈ νλ‘ν (λ¨μ λ‘λ© μ΅μ’
λ³Έ)
|
| 4 |
-
"""
|
| 5 |
-
import sys
|
| 6 |
-
from typing import Dict, Any, Tuple
|
| 7 |
-
import torch
|
| 8 |
-
import logging
|
| 9 |
-
from transformers import AutoTokenizer
|
| 10 |
-
import os
|
| 11 |
-
from dotenv import load_dotenv
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
-
|
| 16 |
-
logger = logging.getLogger(__name__)
|
| 17 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
-
|
| 19 |
-
max_new_tokens = 64
|
| 20 |
-
|
| 21 |
-
class Kanana15V3bInstructProfile:
|
| 22 |
-
"""Kanana-1.5-v-3b-instruct λͺ¨λΈ νλ‘ν"""
|
| 23 |
-
|
| 24 |
-
def __init__(self):
|
| 25 |
-
# νκ²½ κ°μ§
|
| 26 |
-
self.is_local = self._detect_local_environment()
|
| 27 |
-
|
| 28 |
-
# λͺ¨λΈ κ²½λ‘ μ€μ
|
| 29 |
-
if self.is_local:
|
| 30 |
-
self.model_name = "gbrabbit/lily-math-model" # λ‘컬μμλ HF λͺ¨λΈλͺ
μ¬μ©
|
| 31 |
-
self.local_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 32 |
-
self.display_name = "Kanana-1.5-v-3b-instruct (λ‘컬)"
|
| 33 |
-
else:
|
| 34 |
-
self.model_name = "gbrabbit/lily-math-model" # Hugging Face Hub λͺ¨λΈ κ²½λ‘
|
| 35 |
-
self.local_path = None # μλ²μμλ λ‘컬 κ²½λ‘ μ¬μ© μν¨
|
| 36 |
-
self.display_name = "Kanana-1.5-v-3b-instruct (μλ²)"
|
| 37 |
-
|
| 38 |
-
self.description = "μΉ΄μΉ΄μ€ λ©ν°λͺ¨λ¬ λͺ¨λΈ (3.6B) - Math RAG νΉν"
|
| 39 |
-
self.language = "ko"
|
| 40 |
-
self.model_size = "3.6B"
|
| 41 |
-
self.multimodal = True
|
| 42 |
-
|
| 43 |
-
def _detect_local_environment(self) -> bool:
|
| 44 |
-
"""λ‘컬 νκ²½μΈμ§ κ°μ§"""
|
| 45 |
-
import os
|
| 46 |
-
|
| 47 |
-
# λ‘컬 νκ²½ κ°μ§ 쑰건λ€
|
| 48 |
-
local_indicators = [
|
| 49 |
-
os.path.exists('.env'),
|
| 50 |
-
os.path.exists('../.env'),
|
| 51 |
-
os.path.exists('../../.env'),
|
| 52 |
-
os.getenv('IS_LOCAL') == 'true',
|
| 53 |
-
os.getenv('ENVIRONMENT') == 'local',
|
| 54 |
-
os.getenv('DOCKER_ENV') == 'local',
|
| 55 |
-
# Windows κ²½λ‘ νμΈ
|
| 56 |
-
os.path.exists('C:/Project/lily_generate_project/lily_generate_package/.env'),
|
| 57 |
-
]
|
| 58 |
-
|
| 59 |
-
is_local = any(local_indicators)
|
| 60 |
-
logger.info(f"π νκ²½ κ°μ§: {'λ‘컬' if is_local else 'μλ²'}")
|
| 61 |
-
return is_local
|
| 62 |
-
|
| 63 |
-
def _load_environment_variables(self):
|
| 64 |
-
"""νκ²½λ³μλ₯Ό λ‘λν©λλ€."""
|
| 65 |
-
import os
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
if self.is_local:
|
| 69 |
-
# λ‘컬 νκ²½: .env νμΌ λ‘λ
|
| 70 |
-
from dotenv import load_dotenv
|
| 71 |
-
|
| 72 |
-
# μ¬λ¬ κ²½λ‘μμ .env νμΌ μ°ΎκΈ°
|
| 73 |
-
env_paths = [
|
| 74 |
-
'.env',
|
| 75 |
-
'../.env',
|
| 76 |
-
'../../.env',
|
| 77 |
-
'C:/Project/lily_generate_project/lily_generate_package/.env',
|
| 78 |
-
]
|
| 79 |
-
|
| 80 |
-
env_loaded = False
|
| 81 |
-
for env_path in env_paths:
|
| 82 |
-
if os.path.exists(env_path):
|
| 83 |
-
load_dotenv(env_path)
|
| 84 |
-
logger.info(f"β
νκ²½λ³μ λ‘λλ¨: {env_path}")
|
| 85 |
-
env_loaded = True
|
| 86 |
-
break
|
| 87 |
-
|
| 88 |
-
if not env_loaded:
|
| 89 |
-
logger.warning("β οΈ .env νμΌμ μ°Ύμ μ μμ΅λλ€")
|
| 90 |
-
else:
|
| 91 |
-
# μλ² νκ²½: μμ€ν
νκ²½λ³μ μ¬μ©
|
| 92 |
-
logger.info("π μλ² νκ²½λ³μ μ¬μ©")
|
| 93 |
-
|
| 94 |
-
except ImportError:
|
| 95 |
-
logger.warning("β οΈ python-dotenvκ° μ€μΉλμ§ μμ")
|
| 96 |
-
except Exception as e:
|
| 97 |
-
logger.error(f"β νκ²½λ³μ λ‘λ μ€ν¨: {e}")
|
| 98 |
-
|
| 99 |
-
def load_model(self) -> Tuple[Any, Any]:
|
| 100 |
-
"""νκ²½μ λ°λΌ λͺ¨λΈμ λ‘λν©λλ€."""
|
| 101 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 102 |
-
|
| 103 |
-
import os
|
| 104 |
-
from pathlib import Path
|
| 105 |
-
|
| 106 |
-
# νκ²½λ³μ λ‘λ©
|
| 107 |
-
self._load_environment_variables()
|
| 108 |
-
|
| 109 |
-
try:
|
| 110 |
-
# 1. λ‘컬 μΊμ κ²½λ‘κ° μλμ§ νμΈ
|
| 111 |
-
use_local = False
|
| 112 |
-
if self.local_path is not None:
|
| 113 |
-
local_model_path = Path(self.local_path)
|
| 114 |
-
use_local = local_model_path.exists() and any(local_model_path.iterdir())
|
| 115 |
-
|
| 116 |
-
if use_local:
|
| 117 |
-
logger.info(f"ποΈ λ‘컬 λͺ¨λΈ μ¬μ©: {self.local_path}")
|
| 118 |
-
model_path = self.local_path
|
| 119 |
-
local_files_only = True
|
| 120 |
-
|
| 121 |
-
# λ‘컬 λͺ¨λΈμ κ²½μ° sys.pathμ μΆκ°
|
| 122 |
-
if self.local_path not in sys.path:
|
| 123 |
-
sys.path.insert(0, self.local_path)
|
| 124 |
-
else:
|
| 125 |
-
logger.info(f"π Hugging Face Hubμμ λ€μ΄λ‘λ: {self.model_name}")
|
| 126 |
-
model_path = self.model_name
|
| 127 |
-
local_files_only = False
|
| 128 |
-
|
| 129 |
-
# νκ²½λ³ μΆκ° μ€μ
|
| 130 |
-
if self.is_local:
|
| 131 |
-
logger.info("π λ‘컬 νκ²½ μ€μ μ μ©")
|
| 132 |
-
# λ‘컬 νκ²½μμλ μΆκ° μ€μ μ΄ νμν μ μμ
|
| 133 |
-
else:
|
| 134 |
-
logger.info("βοΈ μλ² νκ²½ μ€μ μ μ©")
|
| 135 |
-
# μλ² νκ²½μμλ μΊμ λλ ν 리 λ± μ€μ
|
| 136 |
-
|
| 137 |
-
# 2. ν ν¬λμ΄μ λ‘λ
|
| 138 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 139 |
-
model_path,
|
| 140 |
-
token=HF_TOKEN,
|
| 141 |
-
trust_remote_code=True,
|
| 142 |
-
local_files_only=local_files_only,
|
| 143 |
-
cache_dir="/app/cache/transformers" if not use_local else None
|
| 144 |
-
)
|
| 145 |
-
logger.info(f"β
ν ν¬λμ΄μ λ‘λ μλ£ ({tokenizer.__class__.__name__})")
|
| 146 |
-
from modeling import KananaVForConditionalGeneration
|
| 147 |
-
# 3. λͺ¨λΈ λ‘λ
|
| 148 |
-
if use_local:
|
| 149 |
-
# λ‘컬 λͺ¨λΈ: 컀μ€ν
λͺ¨λΈλ§ ν΄λμ€ μ¬μ©
|
| 150 |
-
model = KananaVForConditionalGeneration.from_pretrained(
|
| 151 |
-
model_path,
|
| 152 |
-
token=HF_TOKEN,
|
| 153 |
-
trust_remote_code=True,
|
| 154 |
-
torch_dtype=torch.bfloat16,
|
| 155 |
-
local_files_only=True,
|
| 156 |
-
# low_cpu_mem_usage=True,
|
| 157 |
-
).to(DEVICE)
|
| 158 |
-
else:
|
| 159 |
-
model = KananaVForConditionalGeneration.from_pretrained(
|
| 160 |
-
model_path,
|
| 161 |
-
token=HF_TOKEN,
|
| 162 |
-
torch_dtype=torch.float16,
|
| 163 |
-
trust_remote_code=True,
|
| 164 |
-
cache_dir="/app/cache/transformers",
|
| 165 |
-
# device_map="auto",
|
| 166 |
-
# low_cpu_mem_usage=True,
|
| 167 |
-
).to(DEVICE)
|
| 168 |
-
|
| 169 |
-
logger.info(f"β
λͺ¨λΈ λ‘λ μλ£ ({model.__class__.__name__})")
|
| 170 |
-
return model, tokenizer
|
| 171 |
-
|
| 172 |
-
except Exception as e:
|
| 173 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}", exc_info=True)
|
| 174 |
-
if use_local and self.local_path in sys.path:
|
| 175 |
-
sys.path.remove(self.local_path)
|
| 176 |
-
raise
|
| 177 |
-
|
| 178 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 179 |
-
# λͺ¨λΈ νλΌλ―Έν° μ΅μ ν μ€μ , max_new_tokens : μμ±λλ ν
μ€νΈ κΈΈμ΄ μ΅λκ° (μ΄λ―Έμ§ μ€λͺ
μ μν΄ μ¦κ°)
|
| 180 |
-
return {"max_new_tokens": max_new_tokens, "temperature": 0.7, "do_sample": True, "top_k": 40, "top_p": 0.9, "repetition_penalty": 1.1}
|
| 181 |
-
|
| 182 |
-
def extract_response(self, full_text: str, formatted_prompt: str = None, **kwargs) -> str:
|
| 183 |
-
"""
|
| 184 |
-
λ€μν μλ΅ νμμ μ²λ¦¬ν μ μλ λ λλν μλ΅ μΆμΆ ν¨μ
|
| 185 |
-
"""
|
| 186 |
-
logger.info(f"--- μλ΅ μΆμΆ μμ ---")
|
| 187 |
-
logger.info(f"μ 체 μμ± ν
μ€νΈ (Raw): \n---\n{full_text}\n---")
|
| 188 |
-
|
| 189 |
-
# ν둬ννΈκ° μ 곡λ κ²½μ° μ΄λ₯Ό μ κ±°
|
| 190 |
-
if formatted_prompt and formatted_prompt in full_text:
|
| 191 |
-
response = full_text.replace(formatted_prompt, "").strip()
|
| 192 |
-
logger.info(f"β
μ±κ³΅: ν둬ννΈ μ κ±°λ‘ μλ΅ μΆμΆ")
|
| 193 |
-
logger.info(f"μΆμΆλ μλ΅: {response}")
|
| 194 |
-
if response: # λΉ λ¬Έμμ΄μ΄ μλ κ²½μ°μλ§ λ°ν
|
| 195 |
-
return response
|
| 196 |
-
|
| 197 |
-
# 1μμ: κ°μ₯ μ νν νΉμ νκ·Έλ‘ μΆμΆ μλ
|
| 198 |
-
# μ: <|start_header_id|>assistant<|end_header_id|>μλ
νμΈμ...
|
| 199 |
-
# λλ <|im_start|>assistantμλ
νμΈμ...
|
| 200 |
-
assistant_tags = [
|
| 201 |
-
"<|start_header_id|>assistant<|end_header_id|>",
|
| 202 |
-
"<|im_start|>assistant",
|
| 203 |
-
"assistant\n",
|
| 204 |
-
"assistant:"
|
| 205 |
-
]
|
| 206 |
-
for tag in assistant_tags:
|
| 207 |
-
if tag in full_text:
|
| 208 |
-
parts = full_text.split(tag)
|
| 209 |
-
if len(parts) > 1:
|
| 210 |
-
response = parts[-1].strip()
|
| 211 |
-
# μΆκ° μ 리: νΉμ ν ν° μ κ±°
|
| 212 |
-
response = response.replace("<|im_end|>", "").strip()
|
| 213 |
-
logger.info(f"β
μ±κ³΅: '{tag}' νκ·Έλ‘ μλ΅ μΆμΆ")
|
| 214 |
-
logger.info(f"μΆμΆλ μλ΅: {response}")
|
| 215 |
-
if response: # λΉ λ¬Έμμ΄μ΄ μλ κ²½μ°μλ§ λ°ν
|
| 216 |
-
return response
|
| 217 |
-
|
| 218 |
-
# 2μμ: κ°λ¨ν ν€μλλ‘ μΆμΆ μλ
|
| 219 |
-
# μ: ... user μλ
νμΈμ assistant μλ
νμΈμ ...
|
| 220 |
-
if "assistant" in full_text:
|
| 221 |
-
parts = full_text.split("assistant")
|
| 222 |
-
if len(parts) > 1:
|
| 223 |
-
response = parts[-1].strip()
|
| 224 |
-
response = response.replace("<|im_end|>", "").strip()
|
| 225 |
-
logger.info("β
μ±κ³΅: 'assistant' ν€μλλ‘ μλ΅ μΆμΆ")
|
| 226 |
-
logger.info(f"μΆμΆλ μλ΅: {response}")
|
| 227 |
-
if response: # λΉ λ¬Έμμ΄μ΄ μλ κ²½μ°μλ§ λ°ν
|
| 228 |
-
return response
|
| 229 |
-
|
| 230 |
-
# 3μμ: ν둬ννΈκ° μλ κ²½μ°, μ 체 ν
μ€νΈμμ λΆνμν λΆλΆ μ κ±°
|
| 231 |
-
clean_text = full_text.strip()
|
| 232 |
-
# μΌλ°μ μΈ ν둬ννΈ ν¨ν΄ μ κ±° μλ
|
| 233 |
-
patterns_to_remove = [
|
| 234 |
-
"<|im_start|>user\n",
|
| 235 |
-
"<|im_end|>",
|
| 236 |
-
"<image>",
|
| 237 |
-
"user\n",
|
| 238 |
-
"assistant\n"
|
| 239 |
-
]
|
| 240 |
-
|
| 241 |
-
for pattern in patterns_to_remove:
|
| 242 |
-
clean_text = clean_text.replace(pattern, "")
|
| 243 |
-
|
| 244 |
-
clean_text = clean_text.strip()
|
| 245 |
-
|
| 246 |
-
if clean_text and clean_text != full_text:
|
| 247 |
-
logger.info("β
μ±κ³΅: ν¨ν΄ μ κ±°λ‘ μλ΅ μ 리")
|
| 248 |
-
logger.info(f"μ 리λ μλ΅: {clean_text}")
|
| 249 |
-
return clean_text
|
| 250 |
-
|
| 251 |
-
logger.warning("β οΈ κ²½κ³ : μλ΅μμ assistant λΆλΆμ μ°Ύμ§ λͺ»νμ΅λλ€. μ 체 ν
μ€νΈλ₯Ό λ°νν©λλ€.")
|
| 252 |
-
logger.info(f"μ΅μ’
λ°ν ν
μ€νΈ: {full_text}")
|
| 253 |
-
return full_text
|
| 254 |
-
|
| 255 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 256 |
-
return {"model_name": self.model_name, "display_name": self.display_name, "description": self.description, "language": self.language, "model_size": self.model_size, "local_path": self.local_path, "multimodal": self.multimodal}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/kanana_nano_2_1b_instruct.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Kanana Nano 2.1B Instruct λͺ¨λΈ νλ‘ν
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 8 |
-
from typing import Dict, Any, Tuple
|
| 9 |
-
import logging
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
class KananaNano21bInstructProfile:
|
| 14 |
-
"""Kanana Nano 2.1B Instruct λͺ¨λΈ νλ‘ν"""
|
| 15 |
-
|
| 16 |
-
def __init__(self):
|
| 17 |
-
self.model_name = "kakaocorp/kanana-nano-2.1b-instruct"
|
| 18 |
-
self.local_path = "./lily_llm_core/models/kanana-nano-2.1b-instruct"
|
| 19 |
-
self.display_name = "Kanana Nano 2.1B Instruct"
|
| 20 |
-
self.description = "Kakaoμ Kanana Nano 2.1B Instruct λͺ¨λΈ (κ°μ₯ μμ λͺ¨λΈ)"
|
| 21 |
-
self.language = ["ko", "en"]
|
| 22 |
-
self.model_size = "2.1B"
|
| 23 |
-
|
| 24 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 25 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 26 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 27 |
-
|
| 28 |
-
try:
|
| 29 |
-
# ν ν¬λμ΄μ λ‘λ
|
| 30 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 31 |
-
self.local_path,
|
| 32 |
-
trust_remote_code=True,
|
| 33 |
-
local_files_only=True
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# λͺ¨λΈ λ‘λ
|
| 37 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
-
self.local_path,
|
| 39 |
-
torch_dtype=torch.float32,
|
| 40 |
-
device_map="cpu",
|
| 41 |
-
low_cpu_mem_usage=True,
|
| 42 |
-
local_files_only=True
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# ν ν¬λμ΄μ μ€μ
|
| 46 |
-
if tokenizer.pad_token is None:
|
| 47 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 48 |
-
if tokenizer.eos_token is None:
|
| 49 |
-
tokenizer.eos_token = "</s>"
|
| 50 |
-
|
| 51 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 52 |
-
return model, tokenizer
|
| 53 |
-
|
| 54 |
-
except Exception as e:
|
| 55 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 56 |
-
raise
|
| 57 |
-
|
| 58 |
-
def format_prompt(self, user_input: str) -> str:
|
| 59 |
-
"""ν둬ννΈ ν¬λ§·ν
"""
|
| 60 |
-
return f"<|im_start|>user\n{user_input}<|im_end|>\n<|im_start|>assistant\n"
|
| 61 |
-
|
| 62 |
-
def extract_response(self, full_text: str, formatted_prompt: str) -> str:
|
| 63 |
-
"""μλ΅ μΆμΆ"""
|
| 64 |
-
if "<|im_start|>assistant\n" in full_text:
|
| 65 |
-
response = full_text.split("<|im_start|>assistant\n")[-1]
|
| 66 |
-
if "<|im_end|>" in response:
|
| 67 |
-
response = response.split("<|im_end|>")[0]
|
| 68 |
-
return response.strip()
|
| 69 |
-
return full_text.strip()
|
| 70 |
-
|
| 71 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 72 |
-
"""μμ± μ€μ """
|
| 73 |
-
return {
|
| 74 |
-
"max_new_tokens": 128, # 512μμ 128λ‘ μ€μ
|
| 75 |
-
"temperature": 0.7,
|
| 76 |
-
"top_p": 0.9,
|
| 77 |
-
"do_sample": True,
|
| 78 |
-
"repetition_penalty": 1.1,
|
| 79 |
-
"no_repeat_ngram_size": 3,
|
| 80 |
-
"pad_token_id": None, # ν ν¬λμ΄μ μμ μ€μ λ¨
|
| 81 |
-
"eos_token_id": None, # ν ν¬λμ΄μ μμ μ€μ λ¨
|
| 82 |
-
"use_cache": True, # μΊμ μ¬μ©
|
| 83 |
-
"return_dict_in_generate": False, # λ©λͺ¨λ¦¬ μ μ½
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 87 |
-
"""λͺ¨λΈ μ 보"""
|
| 88 |
-
return {
|
| 89 |
-
"model_name": self.model_name,
|
| 90 |
-
"display_name": self.display_name,
|
| 91 |
-
"description": self.description,
|
| 92 |
-
"language": self.language,
|
| 93 |
-
"model_size": self.model_size,
|
| 94 |
-
"local_path": self.local_path
|
| 95 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/mistral_7b_instruct.py
DELETED
|
@@ -1,103 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Mistral-7B-Instruct-v0.2 λͺ¨λΈ νλ‘ν
|
| 4 |
-
mistralai/Mistral-7B-Instruct-v0.2 λͺ¨λΈμ©
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from typing import Dict, Any, Tuple
|
| 8 |
-
import torch
|
| 9 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
-
import logging
|
| 11 |
-
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
class Mistral7bInstructProfile:
|
| 15 |
-
"""Mistral-7B-Instruct-v0.2 λͺ¨λΈ νλ‘ν"""
|
| 16 |
-
|
| 17 |
-
def __init__(self):
|
| 18 |
-
self.model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 19 |
-
self.local_path = "./lily_llm_core/models/mistral-7B-Instruct-v0.2"
|
| 20 |
-
self.display_name = "Mistral-7B-Instruct-v0.2"
|
| 21 |
-
self.description = "Mistral AIμ 7B νλΌλ―Έν° μΈμ€νΈλνΈ λͺ¨λΈ"
|
| 22 |
-
self.language = "en"
|
| 23 |
-
self.model_size = "7B"
|
| 24 |
-
|
| 25 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 26 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 27 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
# λ‘컬 λͺ¨λΈ λ‘λ
|
| 31 |
-
tokenizer = AutoTokenizer.from_pretrained(self.local_path, use_fast=True)
|
| 32 |
-
|
| 33 |
-
if tokenizer.pad_token is None:
|
| 34 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
-
|
| 36 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
-
self.local_path,
|
| 38 |
-
trust_remote_code=True,
|
| 39 |
-
local_files_only=True,
|
| 40 |
-
torch_dtype=torch.bfloat16,
|
| 41 |
-
# device_map="cpu",
|
| 42 |
-
# low_cpu_mem_usage=True
|
| 43 |
-
# max_memory={"cpu": "8GB"}
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# λͺ¨λΈμ CPUλ‘ λͺ
μμ μ΄λ
|
| 47 |
-
model.to('cpu')
|
| 48 |
-
|
| 49 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 50 |
-
return model, tokenizer
|
| 51 |
-
|
| 52 |
-
except Exception as e:
|
| 53 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 54 |
-
raise
|
| 55 |
-
|
| 56 |
-
def format_prompt(self, user_input: str) -> str:
|
| 57 |
-
"""ν둬ννΈ ν¬λ§·ν
- Mistral μΈμ€νΈλνΈ νμ"""
|
| 58 |
-
# Mistral-7B-Instruct-v0.2 λͺ¨λΈμ κΆμ₯ ν둬ννΈ νμ
|
| 59 |
-
prompt = f"""<s>[INST] {user_input} [/INST]"""
|
| 60 |
-
return prompt
|
| 61 |
-
|
| 62 |
-
def extract_response(self, full_text: str, formatted_prompt: str) -> str:
|
| 63 |
-
"""μλ΅ μΆμΆ"""
|
| 64 |
-
# Mistral λͺ¨λΈμ μλ΅ μΆμΆ
|
| 65 |
-
if "[/INST]" in full_text:
|
| 66 |
-
response = full_text.split("[/INST]")[-1].strip()
|
| 67 |
-
else:
|
| 68 |
-
# ν둬ννΈ μ κ±°
|
| 69 |
-
if formatted_prompt in full_text:
|
| 70 |
-
response = full_text.replace(formatted_prompt, "").strip()
|
| 71 |
-
else:
|
| 72 |
-
response = full_text.strip()
|
| 73 |
-
|
| 74 |
-
# λΉ μλ΅μ΄λ μ΄μν λ¬Έμλ§ μλ κ²½μ° μ²λ¦¬
|
| 75 |
-
if not response or len(response.strip()) < 2:
|
| 76 |
-
return "Hello! How can I help you today?"
|
| 77 |
-
|
| 78 |
-
return response
|
| 79 |
-
|
| 80 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 81 |
-
"""μμ± μ€μ """
|
| 82 |
-
return {
|
| 83 |
-
"max_new_tokens": 128,
|
| 84 |
-
"temperature": 0.7,
|
| 85 |
-
"do_sample": True,
|
| 86 |
-
"top_k": 50,
|
| 87 |
-
"top_p": 0.9,
|
| 88 |
-
"repetition_penalty": 1.1,
|
| 89 |
-
"no_repeat_ngram_size": 3,
|
| 90 |
-
"pad_token_id": None, # λͺ¨λΈμμ μλ μ€μ
|
| 91 |
-
"eos_token_id": None # λͺ¨λΈμμ μλ μ€μ
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 95 |
-
"""λͺ¨λΈ μ 보"""
|
| 96 |
-
return {
|
| 97 |
-
"model_name": self.model_name,
|
| 98 |
-
"display_name": self.display_name,
|
| 99 |
-
"description": self.description,
|
| 100 |
-
"language": self.language,
|
| 101 |
-
"model_size": self.model_size,
|
| 102 |
-
"local_path": self.local_path
|
| 103 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/polyglot_ko_1_3b.py
DELETED
|
@@ -1,102 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Polyglot-ko-1.3b λͺ¨λΈ νλ‘ν
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from typing import Dict, Any, Tuple
|
| 7 |
-
import torch
|
| 8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
-
import logging
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
class PolyglotKo13bProfile:
|
| 14 |
-
"""Polyglot-ko-1.3b λͺ¨λΈ νλ‘ν"""
|
| 15 |
-
|
| 16 |
-
def __init__(self):
|
| 17 |
-
self.model_name = "EleutherAI/polyglot-ko-1.3b"
|
| 18 |
-
self.local_path = "./lily_llm_core/models/polyglot-ko-1.3b"
|
| 19 |
-
self.display_name = "Polyglot-ko-1.3b"
|
| 20 |
-
self.description = "νκ΅μ΄ μ μ© κ²½λ λͺ¨λΈ (1.3B)"
|
| 21 |
-
self.language = "ko"
|
| 22 |
-
self.model_size = "1.3B"
|
| 23 |
-
|
| 24 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 25 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 26 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 27 |
-
|
| 28 |
-
try:
|
| 29 |
-
# λ‘컬 λͺ¨λΈ λ‘λ
|
| 30 |
-
tokenizer = AutoTokenizer.from_pretrained(self.local_path, use_fast=True)
|
| 31 |
-
|
| 32 |
-
if tokenizer.pad_token is None:
|
| 33 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 34 |
-
|
| 35 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
-
self.local_path,
|
| 37 |
-
torch_dtype=torch.bfloat16,
|
| 38 |
-
device_map="cpu",
|
| 39 |
-
# low_cpu_mem_usage=True,
|
| 40 |
-
trust_remote_code=True,
|
| 41 |
-
local_files_only=True,
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 45 |
-
return model, tokenizer
|
| 46 |
-
|
| 47 |
-
except Exception as e:
|
| 48 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 49 |
-
raise
|
| 50 |
-
|
| 51 |
-
def format_prompt(self, user_input: str) -> str:
|
| 52 |
-
"""ν둬ννΈ ν¬λ§·ν
"""
|
| 53 |
-
# λ μμ°μ€λ¬μ΄ νκ΅μ΄ ν둬ννΈ νμ
|
| 54 |
-
prompt = f"""λ€μ μ§λ¬Έμ λν΄ μΉμ νκ³ μμΈν λ΅λ³ν΄μ£ΌμΈμ.
|
| 55 |
-
|
| 56 |
-
μ§λ¬Έ: {user_input}
|
| 57 |
-
|
| 58 |
-
λ΅λ³:"""
|
| 59 |
-
return prompt
|
| 60 |
-
|
| 61 |
-
def extract_response(self, full_text: str, formatted_prompt: str) -> str:
|
| 62 |
-
"""μλ΅ μΆμΆ"""
|
| 63 |
-
# "λ΅λ³:" μ΄νμ ν
μ€νΈλ₯Ό μΆμΆ
|
| 64 |
-
if "λ΅λ³:" in full_text:
|
| 65 |
-
response = full_text.split("λ΅λ³:")[-1].strip()
|
| 66 |
-
else:
|
| 67 |
-
# ν둬ννΈ μ κ±°
|
| 68 |
-
if formatted_prompt in full_text:
|
| 69 |
-
response = full_text.replace(formatted_prompt, "").strip()
|
| 70 |
-
else:
|
| 71 |
-
response = full_text.strip()
|
| 72 |
-
|
| 73 |
-
# λΉ μλ΅μ΄λ μ΄μν λ¬Έμλ§ μλ κ²½μ° μ²λ¦¬
|
| 74 |
-
if not response or len(response.strip()) < 2:
|
| 75 |
-
return "μλ
νμΈμ! 무μμ λμλ릴κΉμ?"
|
| 76 |
-
|
| 77 |
-
return response
|
| 78 |
-
|
| 79 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 80 |
-
"""μμ± μ€μ """
|
| 81 |
-
return {
|
| 82 |
-
"max_new_tokens": 128,
|
| 83 |
-
"temperature": 0.7,
|
| 84 |
-
"do_sample": True,
|
| 85 |
-
"top_k": 50,
|
| 86 |
-
"top_p": 0.9,
|
| 87 |
-
"repetition_penalty": 1.1,
|
| 88 |
-
"no_repeat_ngram_size": 3,
|
| 89 |
-
"pad_token_id": None, # λͺ¨λΈμμ μλ μ€μ
|
| 90 |
-
"eos_token_id": None # λͺ¨λΈμμ μλ μ€μ
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 94 |
-
"""λͺ¨λΈ μ 보"""
|
| 95 |
-
return {
|
| 96 |
-
"model_name": self.model_name,
|
| 97 |
-
"display_name": self.display_name,
|
| 98 |
-
"description": self.description,
|
| 99 |
-
"language": self.language,
|
| 100 |
-
"model_size": self.model_size,
|
| 101 |
-
"local_path": self.local_path
|
| 102 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/polyglot_ko_1_3b_chat.py
CHANGED
|
@@ -8,6 +8,8 @@ from typing import Dict, Any, Tuple
|
|
| 8 |
import torch
|
| 9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
import logging
|
|
|
|
|
|
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
@@ -16,38 +18,43 @@ class PolyglotKo13bChatProfile:
|
|
| 16 |
|
| 17 |
def __init__(self):
|
| 18 |
self.model_name = "heegyu/polyglot-ko-1.3b-chat"
|
| 19 |
-
self.local_path = "./lily_llm_core/models/
|
| 20 |
self.display_name = "Polyglot-ko-1.3b-chat"
|
| 21 |
self.description = "νκ΅μ΄ μ±ν
μ μ© κ²½λ λͺ¨λΈ (1.3B)"
|
| 22 |
self.language = "ko"
|
| 23 |
self.model_size = "1.3B"
|
| 24 |
|
| 25 |
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 26 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 27 |
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 28 |
-
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if tokenizer.pad_token is None:
|
| 34 |
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
-
|
| 38 |
-
# torch_dtype=torch.float32,
|
| 39 |
-
device_map="cpu",
|
| 40 |
-
# low_cpu_mem_usage=True
|
| 41 |
trust_remote_code=True,
|
| 42 |
-
torch_dtype=
|
| 43 |
-
local_files_only=
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 49 |
return model, tokenizer
|
| 50 |
-
|
| 51 |
except Exception as e:
|
| 52 |
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 53 |
raise
|
|
@@ -100,5 +107,6 @@ class PolyglotKo13bChatProfile:
|
|
| 100 |
"description": self.description,
|
| 101 |
"language": self.language,
|
| 102 |
"model_size": self.model_size,
|
| 103 |
-
"local_path": self.local_path
|
|
|
|
| 104 |
}
|
|
|
|
| 8 |
import torch
|
| 9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
import logging
|
| 11 |
+
import os
|
| 12 |
+
from pathlib import Path
|
| 13 |
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
|
|
|
| 18 |
|
| 19 |
def __init__(self):
|
| 20 |
self.model_name = "heegyu/polyglot-ko-1.3b-chat"
|
| 21 |
+
self.local_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 22 |
self.display_name = "Polyglot-ko-1.3b-chat"
|
| 23 |
self.description = "νκ΅μ΄ μ±ν
μ μ© κ²½λ λͺ¨λΈ (1.3B)"
|
| 24 |
self.language = "ko"
|
| 25 |
self.model_size = "1.3B"
|
| 26 |
|
| 27 |
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 28 |
+
"""λͺ¨λΈ λ‘λ (λ‘컬 μ°μ , μμΌλ©΄ Hub)"""
|
| 29 |
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
|
|
|
| 30 |
try:
|
| 31 |
+
use_local = Path(self.local_path).exists() and any(Path(self.local_path).iterdir())
|
| 32 |
+
model_path = self.local_path if use_local else self.model_name
|
| 33 |
+
|
| 34 |
+
logger.info(f"π λͺ¨λΈ κ²½λ‘: {model_path} (local={'yes' if use_local else 'no'})")
|
| 35 |
+
|
| 36 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 37 |
+
model_path,
|
| 38 |
+
use_fast=True,
|
| 39 |
+
trust_remote_code=True,
|
| 40 |
+
local_files_only=use_local,
|
| 41 |
+
)
|
| 42 |
if tokenizer.pad_token is None:
|
| 43 |
tokenizer.pad_token = tokenizer.eos_token
|
| 44 |
+
|
| 45 |
+
# CPUμμλ float32κ° λ μμ μ , CUDAμμλ float16 μ¬μ©
|
| 46 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 47 |
+
selected_dtype = torch.float16 if device == 'cuda' else torch.float32
|
| 48 |
+
|
| 49 |
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
+
model_path,
|
|
|
|
|
|
|
|
|
|
| 51 |
trust_remote_code=True,
|
| 52 |
+
torch_dtype=selected_dtype,
|
| 53 |
+
local_files_only=use_local,
|
| 54 |
+
).to(device)
|
| 55 |
+
|
| 56 |
+
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅! (device={device}, dtype={selected_dtype})")
|
|
|
|
|
|
|
| 57 |
return model, tokenizer
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 60 |
raise
|
|
|
|
| 107 |
"description": self.description,
|
| 108 |
"language": self.language,
|
| 109 |
"model_size": self.model_size,
|
| 110 |
+
"local_path": self.local_path,
|
| 111 |
+
"multimodal": False,
|
| 112 |
}
|
lily_llm_api/models/polyglot_ko_5_8b.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
|
| 3 |
-
"""
|
| 4 |
-
KoAlpaca-Polyglot-5.8B λͺ¨λΈ λ€μ΄λ‘λ
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from typing import Dict, Any, Tuple
|
| 8 |
-
import torch
|
| 9 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
-
import logging
|
| 11 |
-
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
class PolyglotKo58bProfile:
|
| 15 |
-
"""KoAlpaca-Polyglot-5.8B λͺ¨λΈ νλ‘ν"""
|
| 16 |
-
|
| 17 |
-
def __init__(self):
|
| 18 |
-
self.model_name = "beomi/KoAlpaca-Polyglot-5.8B"
|
| 19 |
-
self.local_path = "./lily_llm_core/models/koalpaca-polyglot-5.8b"
|
| 20 |
-
self.display_name = "KoAlpaca-Polyglot-5.8B"
|
| 21 |
-
self.description = "EleutherAI/polyglot-ko-5.8bμ λ―ΈμΈ μ‘°μ λ λ²μ "
|
| 22 |
-
self.language = "ko"
|
| 23 |
-
self.model_size = "5.8B"
|
| 24 |
-
|
| 25 |
-
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 26 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 27 |
-
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
# λ‘컬 λͺ¨λΈ λ‘λ
|
| 31 |
-
tokenizer = AutoTokenizer.from_pretrained(self.local_path, use_fast=True)
|
| 32 |
-
|
| 33 |
-
if tokenizer.pad_token is None:
|
| 34 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 35 |
-
|
| 36 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
-
self.local_path,
|
| 38 |
-
torch_dtype=torch.bfloat16,
|
| 39 |
-
# torch_dtype=torch.float32,
|
| 40 |
-
device_map="cpu",
|
| 41 |
-
# low_cpu_mem_usage=True,
|
| 42 |
-
trust_remote_code=True,
|
| 43 |
-
local_files_only=True,
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# model.to('cpu')
|
| 47 |
-
|
| 48 |
-
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅!")
|
| 49 |
-
return model, tokenizer
|
| 50 |
-
|
| 51 |
-
except Exception as e:
|
| 52 |
-
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 53 |
-
raise
|
| 54 |
-
|
| 55 |
-
def format_prompt(self, user_input: str) -> str:
|
| 56 |
-
"""ν둬ννΈ ν¬λ§·ν
- μ±ν
νμ"""
|
| 57 |
-
# heegyu/polyglot-ko-1.3b-chat λͺ¨λΈμ κΆμ₯ ν둬ννΈ νμ
|
| 58 |
-
prompt = f"""λΉμ μ AI μ±λ΄μ
λλ€. μ¬μ©μμκ² λμμ΄ λκ³ μ μ΅ν λ΄μ©μ μ 곡ν΄μΌν©λλ€. λ΅λ³μ κΈΈκ³ μμΈνλ©° μΉμ ν μ€λͺ
μ λ§λΆμ¬μ μμ±νμΈμ.
|
| 59 |
-
|
| 60 |
-
### μ¬μ©μ:
|
| 61 |
-
{user_input}
|
| 62 |
-
|
| 63 |
-
### μ±λ΄:
|
| 64 |
-
"""
|
| 65 |
-
return prompt
|
| 66 |
-
|
| 67 |
-
def extract_response(self, full_text: str, formatted_prompt: str) -> str:
|
| 68 |
-
"""μλ΅ μΆμΆ"""
|
| 69 |
-
# "### μ±λ΄:" μ΄νμ ν
μ€νΈλ₯Ό μΆμΆ
|
| 70 |
-
if "### μ±λ΄:" in full_text:
|
| 71 |
-
response = full_text.split("### μ±λ΄:")[-1].strip()
|
| 72 |
-
else:
|
| 73 |
-
# ν둬ννΈ μ κ±°
|
| 74 |
-
if formatted_prompt in full_text:
|
| 75 |
-
response = full_text.replace(formatted_prompt, "").strip()
|
| 76 |
-
else:
|
| 77 |
-
response = full_text.strip()
|
| 78 |
-
|
| 79 |
-
return response
|
| 80 |
-
|
| 81 |
-
def get_generation_config(self) -> Dict[str, Any]:
|
| 82 |
-
"""μμ± μ€μ """
|
| 83 |
-
return {
|
| 84 |
-
"max_new_tokens": 128,
|
| 85 |
-
"temperature": 0.7,
|
| 86 |
-
"do_sample": True,
|
| 87 |
-
"top_k": 50,
|
| 88 |
-
"top_p": 0.9,
|
| 89 |
-
"repetition_penalty": 1.1,
|
| 90 |
-
"no_repeat_ngram_size": 3,
|
| 91 |
-
"pad_token_id": None, # λͺ¨λΈμμ μλ μ€μ
|
| 92 |
-
"eos_token_id": None # λͺ¨λΈμμ μλ μ€μ
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
def get_model_info(self) -> Dict[str, Any]:
|
| 96 |
-
"""λͺ¨λΈ μ 보"""
|
| 97 |
-
return {
|
| 98 |
-
"model_name": self.model_name,
|
| 99 |
-
"display_name": self.display_name,
|
| 100 |
-
"description": self.description,
|
| 101 |
-
"language": self.language,
|
| 102 |
-
"model_size": self.model_size,
|
| 103 |
-
"local_path": self.local_path
|
| 104 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lily_llm_api/models/polyglot_ko_5_8b_chat.py
CHANGED
|
@@ -15,35 +15,43 @@ class PolyglotKo58bChatProfile:
|
|
| 15 |
|
| 16 |
def __init__(self):
|
| 17 |
self.model_name = "heegyu/polyglot-ko-5.8b-chat"
|
| 18 |
-
self.local_path = "./lily_llm_core/models/
|
| 19 |
self.display_name = "heegyu/polyglot-ko-5.8b-chat"
|
| 20 |
self.description = "EleutherAI/polyglot-ko-5.8bλ₯Ό μ¬λ¬ νκ΅μ΄ instruction λ°μ΄ν°μ
μΌλ‘ νμ΅ν λͺ¨λΈ"
|
| 21 |
self.language = "ko"
|
| 22 |
self.model_size = "5.8B"
|
| 23 |
|
| 24 |
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 25 |
-
"""λͺ¨λΈ λ‘λ"""
|
| 26 |
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
| 27 |
-
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if tokenizer.pad_token is None:
|
| 33 |
tokenizer.pad_token = tokenizer.eos_token
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
-
|
| 37 |
-
torch_dtype=torch.bfloat16,
|
| 38 |
-
device_map="cpu",
|
| 39 |
-
# low_cpu_mem_usage=True,
|
| 40 |
trust_remote_code=True,
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
return model, tokenizer
|
| 46 |
-
|
| 47 |
except Exception as e:
|
| 48 |
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 49 |
raise
|
|
@@ -84,5 +92,6 @@ class PolyglotKo58bChatProfile:
|
|
| 84 |
"description": self.description,
|
| 85 |
"language": self.language,
|
| 86 |
"model_size": self.model_size,
|
| 87 |
-
"local_path": self.local_path
|
|
|
|
| 88 |
}
|
|
|
|
| 15 |
|
| 16 |
def __init__(self):
|
| 17 |
self.model_name = "heegyu/polyglot-ko-5.8b-chat"
|
| 18 |
+
self.local_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 19 |
self.display_name = "heegyu/polyglot-ko-5.8b-chat"
|
| 20 |
self.description = "EleutherAI/polyglot-ko-5.8bλ₯Ό μ¬λ¬ νκ΅μ΄ instruction λ°μ΄ν°μ
μΌλ‘ νμ΅ν λͺ¨λΈ"
|
| 21 |
self.language = "ko"
|
| 22 |
self.model_size = "5.8B"
|
| 23 |
|
| 24 |
def load_model(self) -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
| 25 |
+
"""λͺ¨λΈ λ‘λ (λ‘컬 μ°μ , μμΌλ©΄ Hub)"""
|
| 26 |
logger.info(f"π₯ {self.display_name} λͺ¨λΈ λ‘λ μ€...")
|
|
|
|
| 27 |
try:
|
| 28 |
+
from pathlib import Path
|
| 29 |
+
use_local = Path(self.local_path).exists() and any(Path(self.local_path).iterdir())
|
| 30 |
+
model_path = self.local_path if use_local else self.model_name
|
| 31 |
+
|
| 32 |
+
logger.info(f"π λͺ¨λΈ κ²½λ‘: {model_path} (local={'yes' if use_local else 'no'})")
|
| 33 |
+
|
| 34 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 35 |
+
model_path,
|
| 36 |
+
use_fast=True,
|
| 37 |
+
trust_remote_code=True,
|
| 38 |
+
local_files_only=use_local,
|
| 39 |
+
)
|
| 40 |
if tokenizer.pad_token is None:
|
| 41 |
tokenizer.pad_token = tokenizer.eos_token
|
| 42 |
+
|
| 43 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 44 |
+
selected_dtype = torch.float16 if device == 'cuda' else torch.float32
|
| 45 |
+
|
| 46 |
model = AutoModelForCausalLM.from_pretrained(
|
| 47 |
+
model_path,
|
|
|
|
|
|
|
|
|
|
| 48 |
trust_remote_code=True,
|
| 49 |
+
torch_dtype=selected_dtype,
|
| 50 |
+
local_files_only=use_local,
|
| 51 |
+
).to(device)
|
| 52 |
+
|
| 53 |
+
logger.info(f"β
{self.display_name} λͺ¨λΈ λ‘λ μ±κ³΅! (device={device}, dtype={selected_dtype})")
|
| 54 |
return model, tokenizer
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
logger.error(f"β {self.display_name} λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 57 |
raise
|
|
|
|
| 92 |
"description": self.description,
|
| 93 |
"language": self.language,
|
| 94 |
"model_size": self.model_size,
|
| 95 |
+
"local_path": self.local_path,
|
| 96 |
+
"multimodal": False,
|
| 97 |
}
|
lily_llm_core/config.py
CHANGED
|
@@ -36,8 +36,8 @@ class ModelSettings(BaseSettings):
|
|
| 36 |
|
| 37 |
# λͺ¨λΈλ³ μ€μ
|
| 38 |
kanana_1_5_v_3b_instruct_model_path: str = Field(default="./models/kanana_1_5_v_3b_instruct", description="Kanana 1.5 v 3b λͺ¨λΈ κ²½λ‘")
|
| 39 |
-
polyglot_ko_1_3b_chat_model_path: str = Field(default="./models/
|
| 40 |
-
polyglot_ko_5_8b_chat_model_path: str = Field(default="./models/
|
| 41 |
|
| 42 |
class Config:
|
| 43 |
env_prefix = "MODEL_"
|
|
|
|
| 36 |
|
| 37 |
# λͺ¨λΈλ³ μ€μ
|
| 38 |
kanana_1_5_v_3b_instruct_model_path: str = Field(default="./models/kanana_1_5_v_3b_instruct", description="Kanana 1.5 v 3b λͺ¨λΈ κ²½λ‘")
|
| 39 |
+
polyglot_ko_1_3b_chat_model_path: str = Field(default="./models/polyglot_ko_1_3b_chat", description="Polyglot 1.3b λͺ¨λΈ κ²½λ‘")
|
| 40 |
+
polyglot_ko_5_8b_chat_model_path: str = Field(default="./models/polyglot_ko_5_8b_chat", description="Polyglot 5.8b λͺ¨λΈ κ²½λ‘")
|
| 41 |
|
| 42 |
class Config:
|
| 43 |
env_prefix = "MODEL_"
|
test.py
CHANGED
|
@@ -1,60 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
-
import os # os λͺ¨λ μΆκ°
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
load_dotenv()
|
| 6 |
-
|
| 7 |
-
# 1. νκ²½ λ³μμμ νκΉ
νμ΄μ€ ν ν°μ κ°μ Έμ΅λλ€.
|
| 8 |
-
# ν°λ―Έλμμ `set HUGGING_FACE_TOKEN=hf_...` (Windows) λλ
|
| 9 |
-
# `export HUGGING_FACE_TOKEN=hf_...` (Mac/Linux) λͺ
λ ΉμΌλ‘ 미리 μ€μ ν©λλ€.
|
| 10 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
print("
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
'prompt': 'μλ
νμΈμ! Private μ€νμ΄μ€μμ μ μ§λ΄μλμ?',
|
| 31 |
-
'max_length': 20
|
| 32 |
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if response.status_code == 200:
|
| 47 |
result = response.json()
|
| 48 |
-
print(f"
|
| 49 |
-
|
| 50 |
-
print(f"β μλ΅: {response.text}")
|
| 51 |
-
|
| 52 |
-
return response.status_code == 200
|
| 53 |
-
|
| 54 |
except Exception as e:
|
| 55 |
-
print(f"β
|
| 56 |
-
return False
|
| 57 |
|
| 58 |
-
# μ€ν¬λ¦½νΈ μ€ν
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
κ°λ¨ν API ν
μ€νΈ μ€ν¬λ¦½νΈ (μ΅μ’
μμ λ³Έ)
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
import requests
|
| 7 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def test_api():
|
| 10 |
+
"""API ν
μ€νΈ"""
|
| 11 |
+
url = "http://localhost:8001/generate"
|
| 12 |
+
|
| 13 |
+
test_prompts = [
|
| 14 |
+
"μλ
νμΈμ!",
|
| 15 |
+
# "μ€λ κΈ°λΆμ΄ μ΄λμ?",
|
| 16 |
+
# "κ°λ¨ν μκΈ°μκ°λ₯Ό ν΄μ£ΌμΈμ",
|
| 17 |
+
# "νλ‘κ·Έλλ°μ΄λ 무μμΈκ°μ?",
|
| 18 |
+
# "λ μ¨κ° μ’λ€μ"
|
| 19 |
+
]
|
| 20 |
|
| 21 |
+
for i, prompt in enumerate(test_prompts, 1):
|
| 22 |
+
print(f"\n{'='*50}")
|
| 23 |
+
print(f"ν
μ€νΈ {i}: {prompt}")
|
| 24 |
+
print(f"{'='*50}")
|
| 25 |
|
| 26 |
+
# API μμ² - Form λ°μ΄ν° νμμΌλ‘ μ μ‘
|
| 27 |
+
payload = {
|
| 28 |
+
"prompt": prompt,
|
| 29 |
+
"max_length": 20, # λ μ§§κ²
|
| 30 |
+
"temperature": 0.8, # λ λκ²
|
| 31 |
+
"top_p": 0.95, # λ λκ²
|
| 32 |
+
"do_sample": True
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
+
try:
|
| 36 |
+
# json= μΈμλ₯Ό data= λ‘ λ³κ²½νμ¬ Form λ°μ΄ν°λ‘ μ μ‘
|
| 37 |
+
response = requests.post(url, data=payload, timeout=600) # ν
μ€νΈ μμ± μκ°μ κ³ λ €ν΄ νμμμ μ¦κ°
|
| 38 |
+
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
result = response.json()
|
| 41 |
+
print(f"β
μ±κ³΅!")
|
| 42 |
+
print(f"π μμ±λ ν
μ€νΈ: '{result['generated_text']}'")
|
| 43 |
+
print(f"β±οΈ μ²λ¦¬ μκ°: {result['processing_time']:.2f}μ΄")
|
| 44 |
+
print(f"π€ λͺ¨λΈ: {result['model_name']}")
|
| 45 |
+
else:
|
| 46 |
+
print(f"β μ€λ₯: {response.status_code}")
|
| 47 |
+
print(f"π μλ΅: {response.text}")
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"β μμ² μ€ν¨: {e}")
|
| 51 |
+
|
| 52 |
+
def test_raw_response():
|
| 53 |
+
"""μμ μλ΅ νμΈ"""
|
| 54 |
+
url = "http://localhost:8001/generate"
|
| 55 |
+
|
| 56 |
+
payload = {
|
| 57 |
+
"prompt": "Hello",
|
| 58 |
+
"max_length": 20,
|
| 59 |
+
"temperature": 1.0,
|
| 60 |
+
"top_p": 1.0,
|
| 61 |
+
"do_sample": True
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
response = requests.post(url, json=payload, timeout=30)
|
| 66 |
+
print(f"\nπ μμ μλ΅ νμΈ:")
|
| 67 |
+
print(f"μν μ½λ: {response.status_code}")
|
| 68 |
+
print(f"μλ΅ ν€λ: {dict(response.headers)}")
|
| 69 |
+
print(f"μλ΅ λ΄μ©: {response.text}")
|
| 70 |
|
| 71 |
if response.status_code == 200:
|
| 72 |
result = response.json()
|
| 73 |
+
print(f"νμ±λ JSON: {json.dumps(result, indent=2, ensure_ascii=False)}")
|
| 74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
+
print(f"β μμ μλ΅ νμΈ μ€ν¨: {e}")
|
|
|
|
| 77 |
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
print("π§ͺ Lily LLM API ν
μ€νΈ μμ")
|
| 80 |
+
test_api()
|
| 81 |
+
# test_raw_response()
|
| 82 |
+
print("\nβ
ν
μ€νΈ μλ£!")
|
test_hf_with_token.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import os # os λͺ¨λ μΆκ°
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
# 1. νκ²½ λ³μμμ νκΉ
νμ΄μ€ ν ν°μ κ°μ Έμ΅λλ€.
|
| 8 |
+
# ν°λ―Έλμμ `set HUGGING_FACE_TOKEN=hf_...` (Windows) λλ
|
| 9 |
+
# `export HUGGING_FACE_TOKEN=hf_...` (Mac/Linux) λͺ
λ ΉμΌλ‘ 미리 μ€μ ν©λλ€.
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
# νκΉ
νμ΄μ€ FastAPI μλ² URL
|
| 13 |
+
HF_API_BASE = "https://gbrabbit-lily-fast-api.hf.space"
|
| 14 |
+
|
| 15 |
+
def test_generate_text():
|
| 16 |
+
"""ν
μ€νΈ μμ± ν
μ€νΈ (μΈμ¦ μΆκ°)"""
|
| 17 |
+
print("\nπ ν
μ€νΈ μμ± ν
μ€νΈ...")
|
| 18 |
+
|
| 19 |
+
if not HF_TOKEN:
|
| 20 |
+
print("β HUGGING_FACE_TOKEN νκ²½ λ³μκ° μ€μ λμ§ μμμ΅λλ€.")
|
| 21 |
+
return False
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# 2. μΈμ¦ ν ν°μ λ΄μ ν€λ(headers)λ₯Ό μμ±ν©λλ€.
|
| 25 |
+
headers = {
|
| 26 |
+
"Authorization": f"Bearer {HF_TOKEN}"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
data = {
|
| 30 |
+
'prompt': 'μλ
νμΈμ! Private μ€νμ΄μ€μμ μ μ§λ΄μλμ?',
|
| 31 |
+
'max_length': 20
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
print(f"π€ μμ² λ°μ΄ν° (Form): {json.dumps(data, ensure_ascii=False)}")
|
| 35 |
+
|
| 36 |
+
# 3. requests.post νΈμΆ μ headers νλΌλ―Έν°λ₯Ό μΆκ°ν©λλ€.
|
| 37 |
+
response = requests.post(
|
| 38 |
+
f"{HF_API_BASE}/generate",
|
| 39 |
+
headers=headers, # <<-- μΈμ¦ ν€λ μΆκ°!
|
| 40 |
+
data=data,
|
| 41 |
+
timeout=2000
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
print(f"β
μν μ½λ: {response.status_code}") # μ΄μ 200μ΄ νμλ κ²μ
λλ€.
|
| 45 |
+
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
result = response.json()
|
| 48 |
+
print(f"β
μλ΅: {json.dumps(result, indent=2, ensure_ascii=False)}")
|
| 49 |
+
else:
|
| 50 |
+
print(f"β μλ΅: {response.text}")
|
| 51 |
+
|
| 52 |
+
return response.status_code == 200
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"β ν
μ€νΈ μμ± ν
μ€νΈ μ€ν¨: {e}")
|
| 56 |
+
return False
|
| 57 |
+
|
| 58 |
+
# μ€ν¬λ¦½νΈ μ€ν
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
test_generate_text()
|
test_log.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
(lily_llm_env) C:\Project\lily_generate_project\lily_generate_package>python test.py
|
| 3 |
+
π§ͺ Lily LLM API ν
μ€νΈ μμ
|
| 4 |
+
|
| 5 |
+
==================================================
|
| 6 |
+
ν
μ€νΈ 1: μλ
νμΈμ!
|
| 7 |
+
==================================================
|
| 8 |
+
β
μ±κ³΅!
|
| 9 |
+
π μμ±λ ν
μ€νΈ: 'Hello! How can I assist you today?'
|
| 10 |
+
β±οΈ μ²λ¦¬ μκ°: 154.13μ΄
|
| 11 |
+
π€ λͺ¨λΈ: kanana-1.5-v-3b-instruct
|
| 12 |
+
|
| 13 |
+
β
ν
μ€νΈ μλ£!
|
| 14 |
+
|
| 15 |
+
(lily_llm_env) C:\Project\lily_generate_project\lily_generate_package>python test.py
|
| 16 |
+
π§ͺ Lily LLM API ν
μ€νΈ μμ
|
| 17 |
+
|
| 18 |
+
==================================================
|
| 19 |
+
ν
μ€νΈ 1: μλ
νμΈμ!
|
| 20 |
+
==================================================
|
| 21 |
+
β
μ±κ³΅!
|
| 22 |
+
π μμ±λ ν
μ€νΈ: 'Hello! How can I assist you today? We're here to help with any questions or tasks you'
|
| 23 |
+
β±οΈ μ²λ¦¬ μκ°: 217.69μ΄
|
| 24 |
+
π€ λͺ¨λΈ: kanana-1.5-v-3b-instruct
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
-----
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
INFO: 127.0.0.1:62794 - "POST /generate HTTP/1.1" 500 Internal Server Error
|
| 36 |
+
ERROR: Exception in ASGI application
|
| 37 |
+
Traceback (most recent call last):
|
| 38 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
|
| 39 |
+
result = await app( # type: ignore[func-returns-value]
|
| 40 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| 41 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
|
| 42 |
+
return await self.app(scope, receive, send)
|
| 43 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| 44 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
|
| 45 |
+
await super().__call__(scope, receive, send)
|
| 46 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\applications.py", line 113, in __call__
|
| 47 |
+
await self.middleware_stack(scope, receive, send)
|
| 48 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
|
| 49 |
+
raise exc
|
| 50 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
|
| 51 |
+
await self.app(scope, receive, _send)
|
| 52 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\middleware\cors.py", line 85, in __call__
|
| 53 |
+
await self.app(scope, receive, send)
|
| 54 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\middleware\exceptions.py", line 63, in __call__
|
| 55 |
+
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
|
| 56 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
|
| 57 |
+
raise exc
|
| 58 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
|
| 59 |
+
await app(scope, receive, sender)
|
| 60 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\routing.py", line 716, in __call__
|
| 61 |
+
await self.middleware_stack(scope, receive, send)
|
| 62 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\routing.py", line 736, in app
|
| 63 |
+
await route.handle(scope, receive, send)
|
| 64 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\routing.py", line 290, in handle
|
| 65 |
+
await self.app(scope, receive, send)
|
| 66 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\routing.py", line 78, in app
|
| 67 |
+
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
|
| 68 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
|
| 69 |
+
raise exc
|
| 70 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
|
| 71 |
+
await app(scope, receive, sender)
|
| 72 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\starlette\routing.py", line 75, in app
|
| 73 |
+
response = await f(request)
|
| 74 |
+
^^^^^^^^^^^^^^^^
|
| 75 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\fastapi\routing.py", line 302, in app
|
| 76 |
+
raw_response = await run_endpoint_function(
|
| 77 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| 78 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\fastapi\routing.py", line 213, in run_endpoint_function
|
| 79 |
+
return await dependant.call(**values)
|
| 80 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| 81 |
+
File "C:\Project\lily_generate_project\lily_generate_package\lily_llm_api\app_v2.py", line 372, in generate
|
| 82 |
+
result = await loop.run_in_executor(
|
| 83 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| 84 |
+
File "C:\Users\gigab\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
|
| 85 |
+
result = self.fn(*self.args, **self.kwargs)
|
| 86 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| 87 |
+
File "C:\Project\lily_generate_project\lily_generate_package\lily_llm_api\app_v2.py", line 303, in generate_sync
|
| 88 |
+
inputs = tokenizer.encode_prompt(prompt=formatted_prompt, image_meta=combined_image_metas)
|
| 89 |
+
^^^^^^^^^^^^^^^^^^^^^^^
|
| 90 |
+
File "c:\Project\lily_generate_project\lily_generate_package\lily_llm_env\Lib\site-packages\transformers\tokenization_utils_base.py", line 1099, in __getattr__
|
| 91 |
+
raise AttributeError(f"{self.__class__.__name__} has no attribute {key}")
|
| 92 |
+
AttributeError: PreTrainedTokenizerFast has no attribute encode_prompt
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
---
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
(lily_llm_env) C:\Project\lily_generate_project\lily_generate_package>python test.py
|
| 104 |
+
π§ͺ Lily LLM API ν
μ€νΈ μμ
|
| 105 |
+
|
| 106 |
+
==================================================
|
| 107 |
+
ν
μ€νΈ 1: μλ
νμΈμ!
|
| 108 |
+
==================================================
|
| 109 |
+
β
μ±κ³΅!
|
| 110 |
+
π μμ±λ ν
μ€νΈ: 'Hello! How can I assist you today? We're here to help with any questions or tasks you'
|
| 111 |
+
β±οΈ μ²λ¦¬ μκ°: 217.69μ΄
|
| 112 |
+
π€ λͺ¨λΈ: kanana-1.5-v-3b-instruct
|
| 113 |
+
|
| 114 |
+
β
ν
μ€νΈ μλ£!
|
| 115 |
+
|
| 116 |
+
(lily_llm_env) C:\Project\lily_generate_project\lily_generate_package>python test.py
|
| 117 |
+
π§ͺ Lily LLM API ν
μ€νΈ μμ
|
| 118 |
+
|
| 119 |
+
==================================================
|
| 120 |
+
ν
μ€νΈ 1: μλ
νμΈμ!
|
| 121 |
+
==================================================
|
| 122 |
+
β
μ±κ³΅!
|
| 123 |
+
π μμ±λ ν
μ€νΈ: '"μλ
νμΈμ!"
|
| 124 |
+
|
| 125 |
+
μΈμ¬: μλ
νμλκΉ?
|
| 126 |
+
|
| 127 |
+
μ§λ¬Έ: "μ '
|
| 128 |
+
β±οΈ μ²λ¦¬ μκ°: 20.50μ΄
|
| 129 |
+
π€ λͺ¨λΈ: Polyglot-ko-1.3b-chat
|
| 130 |
+
|
| 131 |
+
β
ν
μ€νΈ μλ£!
|
| 132 |
+
|
| 133 |
+
---
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
(lily_llm_env) C:\Project\lily_generate_project\lily_generate_package>python test.py
|
| 138 |
+
π§ͺ Lily LLM API ν
μ€νΈ μμ
|
| 139 |
+
|
| 140 |
+
==================================================
|
| 141 |
+
ν
μ€νΈ 1: μλ
νμΈμ!
|
| 142 |
+
==================================================
|
| 143 |
+
β
μ±κ³΅!
|
| 144 |
+
π μμ±λ ν
μ€νΈ: '&&...
|
| 145 |
+
|
| 146 |
+
μλ
νμΈμ, μ λ Alisterμ
λλ€'
|
| 147 |
+
β±οΈ μ²λ¦¬ μκ°: 17.73μ΄
|
| 148 |
+
π€ λͺ¨λΈ: Polyglot-ko-1.3b-chat
|
| 149 |
+
|
| 150 |
+
β
ν
μ€νΈ μλ£!
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
--
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
|