Spaces:
Sleeping
Sleeping
Auto commit at 08-2025-08 3:40:22
Browse files- app.py +60 -112
- test_input.py +0 -100
- test_text.py +0 -100
- test_tokenizer.py +0 -159
app.py
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import traceback
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoImageProcessor
|
| 5 |
import torch
|
| 6 |
-
import fitz
|
| 7 |
from PIL import Image
|
| 8 |
from typing import Optional, List
|
| 9 |
|
| 10 |
-
# --- 1. ์ ์ญ
|
|
|
|
|
|
|
|
|
|
| 11 |
tokenizer = None
|
| 12 |
model = None
|
| 13 |
-
image_processor = None
|
| 14 |
MODEL_LOADED = False
|
| 15 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
|
| 17 |
IS_LOCAL = os.path.exists('.env') or os.path.exists('../.env') or os.getenv('IS_LOCAL') == 'true'
|
| 18 |
-
|
| 19 |
try:
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
if IS_LOCAL:
|
|
@@ -23,58 +26,43 @@ try:
|
|
| 23 |
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 24 |
except ImportError:
|
| 25 |
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์")
|
| 26 |
-
|
| 27 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 28 |
MODEL_NAME_SERVER = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 29 |
MODEL_PATH_LOCAL = "../lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 30 |
MODEL_PATH = MODEL_PATH_LOCAL if IS_LOCAL else MODEL_NAME_SERVER
|
| 31 |
-
|
| 32 |
print(f"============== ์์คํ
ํ๊ฒฝ ์ ๋ณด ==============")
|
| 33 |
print(f"๐ ์คํ ํ๊ฒฝ: {'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}")
|
| 34 |
print(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก: {MODEL_PATH}")
|
| 35 |
print(f"๐ ์ฌ์ฉ ๋๋ฐ์ด์ค: {DEVICE.upper()}")
|
| 36 |
print("==========================================")
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# --- 2. ํต์ฌ ๋ก์ง: ๋ชจ๋ธ ๋ฐ ํ๋ก์ธ์ ๋ก๋ฉ ---
|
| 40 |
try:
|
| 41 |
print("๐ง ๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
| 42 |
from modeling import KananaVForConditionalGeneration
|
| 43 |
-
|
| 44 |
if IS_LOCAL:
|
| 45 |
if not os.path.exists(MODEL_PATH):
|
| 46 |
raise FileNotFoundError(f"๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: {MODEL_PATH}")
|
| 47 |
-
|
| 48 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True, local_files_only=True)
|
| 49 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 50 |
-
MODEL_PATH, torch_dtype=torch.
|
| 51 |
).to(DEVICE)
|
| 52 |
-
# ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋ (๋ก์ปฌ)
|
| 53 |
image_processor = AutoImageProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True, local_files_only=True)
|
| 54 |
print("โ
๋ก์ปฌ ๋ชจ๋ธ ๋ฐ ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋ฉ ์๋ฃ!")
|
| 55 |
-
|
| 56 |
-
else: # ์๋ฒ ํ๊ฒฝ
|
| 57 |
if not HF_TOKEN:
|
| 58 |
raise ValueError("์๋ฒ ํ๊ฒฝ์์๋ Hugging Face ํ ํฐ(HF_TOKEN)์ด ๋ฐ๋์ ํ์ํฉ๋๋ค.")
|
| 59 |
-
|
| 60 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, token=HF_TOKEN, trust_remote_code=True)
|
| 61 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 62 |
-
MODEL_PATH, token=HF_TOKEN, torch_dtype=torch.float16, trust_remote_code=True,
|
| 63 |
-
)
|
| 64 |
-
# ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋ (์๋ฒ)
|
| 65 |
image_processor = AutoImageProcessor.from_pretrained(MODEL_PATH, token=HF_TOKEN, trust_remote_code=True)
|
| 66 |
print("โ
์๋ฒ ๋ชจ๋ธ ๋ฐ ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋ฉ ์๋ฃ!")
|
| 67 |
-
|
| 68 |
MODEL_LOADED = True
|
| 69 |
-
|
| 70 |
except Exception as e:
|
| 71 |
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
| 72 |
traceback.print_exc()
|
| 73 |
MODEL_LOADED = False
|
| 74 |
|
| 75 |
-
|
| 76 |
-
# --- 3. ํ์ผ ์ฒ๋ฆฌ ๋ฐ ์๋ต ์์ฑ ๋ก์ง ---
|
| 77 |
-
|
| 78 |
def extract_text_from_pdf(pdf_file_path):
|
| 79 |
try:
|
| 80 |
doc = fitz.open(pdf_file_path)
|
|
@@ -86,140 +74,100 @@ def extract_text_from_pdf(pdf_file_path):
|
|
| 86 |
return f"PDF ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 87 |
|
| 88 |
def generate_response(prompt_template: str, message: str, files: Optional[List] = None):
|
| 89 |
-
if not MODEL_LOADED:
|
| 90 |
-
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 91 |
-
|
| 92 |
try:
|
| 93 |
-
all_pixel_values = []
|
| 94 |
-
all_image_metas = []
|
| 95 |
-
file_texts = []
|
| 96 |
-
|
| 97 |
-
# 1. ์
๋ก๋๋ ํ์ผ๋ค ์ฒ๋ฆฌ (์ด๋ฏธ์ง/PDF ๋ถ๋ฆฌ)
|
| 98 |
if files:
|
| 99 |
for file in files:
|
| 100 |
-
file_path = file.name
|
| 101 |
-
file_extension
|
| 102 |
-
|
| 103 |
-
if file_extension == '.pdf':
|
| 104 |
-
file_texts.append(extract_text_from_pdf(file_path))
|
| 105 |
elif file_extension in ['.png', '.jpg', '.jpeg']:
|
| 106 |
pil_image = Image.open(file_path).convert('RGB')
|
| 107 |
processed_data = image_processor(pil_image)
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
image_metas = processed_data["image_meta"]
|
| 111 |
-
|
| 112 |
-
all_pixel_values.append(pixel_values)
|
| 113 |
-
all_image_metas.append(image_metas)
|
| 114 |
-
|
| 115 |
-
# 2. ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 116 |
image_tokens = "<image>" * len(all_pixel_values)
|
| 117 |
pdf_content = "\n\n".join(file_texts)
|
| 118 |
full_message = message + (f"\n{image_tokens}" if image_tokens else "") + (f"\n\n[์ฒจ๋ถ๋ PDF ๋ด์ฉ]:\n{pdf_content}" if pdf_content else "")
|
| 119 |
full_prompt = prompt_template.format(message=full_message)
|
| 120 |
-
|
| 121 |
-
# 3. ํ ํฌ๋์ด์ง ๋ฐ `image_metas` ๊ฒฐํฉ
|
| 122 |
if all_image_metas:
|
| 123 |
-
|
| 124 |
-
combined_metas = {}
|
| 125 |
-
for key in all_image_metas[0].keys():
|
| 126 |
-
combined_metas[key] = [meta[key] for meta in all_image_metas]
|
| 127 |
-
|
| 128 |
-
# `encode_prompt`๋ Kanana ๋ชจ๋ธ์ ํ ํฌ๋์ด์ ์ ๋ด์ฅ๋ ์ปค์คํ
ํจ์๋ก ๊ฐ์
|
| 129 |
inputs = tokenizer.encode_prompt(prompt=full_prompt, image_meta=combined_metas)
|
| 130 |
-
|
| 131 |
-
# ๊ฐ์ด ํ
์์ธ ๊ฒฝ์ฐ์๋ง ๋ฐฐ์น ์ฐจ์์ ์ถ๊ฐํ๊ณ ๋๋ฐ์ด์ค๋ก ๋ณด๋
๋๋ค.
|
| 132 |
-
inputs = {
|
| 133 |
-
k: (v.unsqueeze(0).to(model.device) if torch.is_tensor(v) else v)
|
| 134 |
-
for k, v in inputs.items()
|
| 135 |
-
}
|
| 136 |
else:
|
| 137 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 138 |
-
|
| 139 |
-
# 4. ์์ฑ ํ๋ผ๋ฏธํฐ ์ค๋น
|
| 140 |
generation_args = {
|
| 141 |
-
"max_new_tokens":
|
| 142 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
-
|
| 145 |
-
# 5. ๋ชจ๋ธ ์ถ๋ก (๋ฉํฐ๋ชจ๋ฌ / ํ
์คํธ ์ ์ฉ ๋ถ๊ธฐ)
|
| 146 |
with torch.no_grad():
|
| 147 |
if all_pixel_values:
|
| 148 |
-
|
| 149 |
-
# pixel_values์ image_metas๋ฅผ `generate` ํจ์์ ์ง์ ์ ๋ฌ
|
| 150 |
-
outputs = model.generate(
|
| 151 |
-
**inputs,
|
| 152 |
-
pixel_values=all_pixel_values,
|
| 153 |
-
image_metas=combined_metas,
|
| 154 |
-
**generation_args
|
| 155 |
-
)
|
| 156 |
else:
|
| 157 |
-
print("๐ ํ
์คํธ๋ง์ผ๋ก ์์ฑ")
|
| 158 |
outputs = model.generate(**inputs, **generation_args)
|
| 159 |
-
|
| 160 |
-
# 6. ๊ฒฐ๊ณผ ๋์ฝ๋ฉ
|
| 161 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 162 |
-
|
| 163 |
-
assistant_response = response.split("<|im_start|>assistant\n")[-1].strip()
|
| 164 |
-
|
| 165 |
-
return assistant_response
|
| 166 |
-
|
| 167 |
except Exception as e:
|
| 168 |
-
print(f"โ ์๋ต ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 169 |
-
traceback.print_exc()
|
| 170 |
-
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 171 |
|
| 172 |
-
|
|
|
|
| 173 |
with gr.Blocks(title="Lily LLM System", theme=gr.themes.Soft()) as demo:
|
| 174 |
gr.Markdown("# ๐งฎ Lily LLM System")
|
| 175 |
gr.Markdown("์ด๋ฏธ์ง, PDF, ํ
์คํธ๋ฅผ ์ดํดํ๊ณ ๋ต๋ณํ๋ ๋ฉํฐ๋ชจ๋ฌ AI ์์คํ
์
๋๋ค.")
|
| 176 |
|
| 177 |
-
with gr.Tabs():
|
| 178 |
with gr.Tab("๐ฌ ์ฑํ
"):
|
| 179 |
chat_prompt = "<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 180 |
-
chatbot = gr.Chatbot(height=
|
| 181 |
-
|
| 182 |
-
with gr.Row():
|
| 183 |
-
file_input = gr.File(
|
| 184 |
-
label="ํ์ผ ์
๋ก๋ (๋ค์ค ์ ํ ๊ฐ๋ฅ)",
|
| 185 |
-
file_count="multiple", # ๋ค์ค ํ์ผ ์
๋ก๋ ํ์ฑํ
|
| 186 |
-
file_types=[".pdf", ".png", ".jpg", ".jpeg"]
|
| 187 |
-
)
|
| 188 |
with gr.Row():
|
| 189 |
-
msg = gr.Textbox(
|
| 190 |
-
|
| 191 |
-
placeholder="ํ์ผ์ ์
๋ก๋ํ๊ณ ์ง๋ฌธํ๊ฑฐ๋, ํ
์คํธ๋ก๋ง ๋ํํ ์ ์์ต๋๋ค.",
|
| 192 |
-
lines=3,
|
| 193 |
-
show_label=False,
|
| 194 |
-
scale=7
|
| 195 |
-
)
|
| 196 |
send_btn = gr.Button("์ ์ก", variant="primary", scale=1)
|
| 197 |
|
|
|
|
| 198 |
def respond(message, chat_history, files):
|
| 199 |
if not message.strip() and not files:
|
| 200 |
-
|
| 201 |
-
return "", chat_history
|
| 202 |
|
| 203 |
bot_message = generate_response(chat_prompt, message, files)
|
| 204 |
|
| 205 |
-
# 'messages' ํ์
์ ๋ง๋ ๋์
๋๋ฆฌ ํํ๋ก ๋ํ ๊ธฐ๋ก ์ถ๊ฐ
|
| 206 |
chat_history.append({"role": "user", "content": message})
|
| 207 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
with gr.Tab("โ๏ธ ์์คํ
์ ๋ณด"):
|
| 215 |
gr.Markdown(f"**์คํ ํ๊ฒฝ**: `{'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}`")
|
| 216 |
gr.Markdown(f"**๋ชจ๋ธ ๊ฒฝ๋ก**: `{MODEL_PATH}`")
|
| 217 |
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: `{'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}`")
|
| 218 |
|
| 219 |
-
if __name__ == "__main__":
|
| 220 |
if IS_LOCAL:
|
| 221 |
-
print("\n๐ ๋ก์ปฌ ์๋ฒ๋ฅผ ์์ํฉ๋๋ค. http://
|
| 222 |
-
demo.launch(server_name="
|
| 223 |
else:
|
| 224 |
print("\n๐ ์๋ฒ๋ฅผ ์์ํฉ๋๋ค...")
|
| 225 |
demo.launch()
|
|
|
|
| 1 |
+
# ํ์ผ: app.py (์ต์ข
์์ ๋ณธ)
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
import traceback
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoImageProcessor
|
| 7 |
import torch
|
| 8 |
+
import fitz
|
| 9 |
from PIL import Image
|
| 10 |
from typing import Optional, List
|
| 11 |
|
| 12 |
+
# --- 1 & 2. ์ ์ญ ๋ณ์, ํ๊ฒฝ ์ค์ , ๋ชจ๋ธ ๋ก๋ฉ (๊ธฐ์กด ์ฝ๋์ ๋์ผ) ---
|
| 13 |
+
# (์ด ๋ถ๋ถ์ ์์ ํ ํ์ ์์ด ๊ทธ๋๋ก ๋์๋ฉด ๋ฉ๋๋ค)
|
| 14 |
+
# ... (์๋ต) ...
|
| 15 |
+
# --- 1 & 2. ์ ์ญ ๋ณ์, ํ๊ฒฝ ์ค์ , ๋ชจ๋ธ ๋ก๋ฉ (๊ธฐ์กด ์ฝ๋์ ๋์ผ) ---
|
| 16 |
tokenizer = None
|
| 17 |
model = None
|
| 18 |
+
image_processor = None
|
| 19 |
MODEL_LOADED = False
|
| 20 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 21 |
IS_LOCAL = os.path.exists('.env') or os.path.exists('../.env') or os.getenv('IS_LOCAL') == 'true'
|
|
|
|
| 22 |
try:
|
| 23 |
from dotenv import load_dotenv
|
| 24 |
if IS_LOCAL:
|
|
|
|
| 26 |
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 27 |
except ImportError:
|
| 28 |
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์")
|
|
|
|
| 29 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 30 |
MODEL_NAME_SERVER = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 31 |
MODEL_PATH_LOCAL = "../lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 32 |
MODEL_PATH = MODEL_PATH_LOCAL if IS_LOCAL else MODEL_NAME_SERVER
|
|
|
|
| 33 |
print(f"============== ์์คํ
ํ๊ฒฝ ์ ๋ณด ==============")
|
| 34 |
print(f"๐ ์คํ ํ๊ฒฝ: {'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}")
|
| 35 |
print(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก: {MODEL_PATH}")
|
| 36 |
print(f"๐ ์ฌ์ฉ ๋๋ฐ์ด์ค: {DEVICE.upper()}")
|
| 37 |
print("==========================================")
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
print("๐ง ๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
| 40 |
from modeling import KananaVForConditionalGeneration
|
|
|
|
| 41 |
if IS_LOCAL:
|
| 42 |
if not os.path.exists(MODEL_PATH):
|
| 43 |
raise FileNotFoundError(f"๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: {MODEL_PATH}")
|
|
|
|
| 44 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True, local_files_only=True)
|
| 45 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 46 |
+
MODEL_PATH, torch_dtype=torch.bfloat16, trust_remote_code=True, local_files_only=True,
|
| 47 |
).to(DEVICE)
|
|
|
|
| 48 |
image_processor = AutoImageProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True, local_files_only=True)
|
| 49 |
print("โ
๋ก์ปฌ ๋ชจ๋ธ ๋ฐ ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋ฉ ์๋ฃ!")
|
| 50 |
+
else:
|
|
|
|
| 51 |
if not HF_TOKEN:
|
| 52 |
raise ValueError("์๋ฒ ํ๊ฒฝ์์๋ Hugging Face ํ ํฐ(HF_TOKEN)์ด ๋ฐ๋์ ํ์ํฉ๋๋ค.")
|
|
|
|
| 53 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, token=HF_TOKEN, trust_remote_code=True)
|
| 54 |
model = KananaVForConditionalGeneration.from_pretrained(
|
| 55 |
+
MODEL_PATH, token=HF_TOKEN, torch_dtype=torch.float16, trust_remote_code=True, device_map="auto"
|
| 56 |
+
)
|
|
|
|
| 57 |
image_processor = AutoImageProcessor.from_pretrained(MODEL_PATH, token=HF_TOKEN, trust_remote_code=True)
|
| 58 |
print("โ
์๋ฒ ๋ชจ๋ธ ๋ฐ ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋ฉ ์๋ฃ!")
|
|
|
|
| 59 |
MODEL_LOADED = True
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
| 62 |
traceback.print_exc()
|
| 63 |
MODEL_LOADED = False
|
| 64 |
|
| 65 |
+
# --- 3. ์๋ต ์์ฑ ๋ก์ง (๊ธฐ์กด ์ฝ๋์ ๋์ผ) ---
|
|
|
|
|
|
|
| 66 |
def extract_text_from_pdf(pdf_file_path):
|
| 67 |
try:
|
| 68 |
doc = fitz.open(pdf_file_path)
|
|
|
|
| 74 |
return f"PDF ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 75 |
|
| 76 |
def generate_response(prompt_template: str, message: str, files: Optional[List] = None):
|
| 77 |
+
if not MODEL_LOADED: return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
+
all_pixel_values, all_image_metas, file_texts = [], [], []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if files:
|
| 81 |
for file in files:
|
| 82 |
+
file_path, file_extension = file.name, os.path.splitext(file.name)[1].lower()
|
| 83 |
+
if file_extension == '.pdf': file_texts.append(extract_text_from_pdf(file_path))
|
|
|
|
|
|
|
|
|
|
| 84 |
elif file_extension in ['.png', '.jpg', '.jpeg']:
|
| 85 |
pil_image = Image.open(file_path).convert('RGB')
|
| 86 |
processed_data = image_processor(pil_image)
|
| 87 |
+
all_pixel_values.append(processed_data["pixel_values"])
|
| 88 |
+
all_image_metas.append(processed_data["image_meta"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
image_tokens = "<image>" * len(all_pixel_values)
|
| 90 |
pdf_content = "\n\n".join(file_texts)
|
| 91 |
full_message = message + (f"\n{image_tokens}" if image_tokens else "") + (f"\n\n[์ฒจ๋ถ๋ PDF ๋ด์ฉ]:\n{pdf_content}" if pdf_content else "")
|
| 92 |
full_prompt = prompt_template.format(message=full_message)
|
|
|
|
|
|
|
| 93 |
if all_image_metas:
|
| 94 |
+
combined_metas = {key: [meta[key] for meta in all_image_metas] for key in all_image_metas[0]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
inputs = tokenizer.encode_prompt(prompt=full_prompt, image_meta=combined_metas)
|
| 96 |
+
inputs = {k: (v.unsqueeze(0).to(model.device) if torch.is_tensor(v) else v) for k, v in inputs.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
else:
|
| 98 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
| 99 |
generation_args = {
|
| 100 |
+
"max_new_tokens": 32,
|
| 101 |
+
"temperature": 0.8,
|
| 102 |
+
"do_sample": True,
|
| 103 |
+
"pad_token_id": tokenizer.eos_token_id,
|
| 104 |
+
"eos_token_id": tokenizer.eos_token_id,
|
| 105 |
+
"top_p": 0.95,
|
| 106 |
}
|
|
|
|
|
|
|
| 107 |
with torch.no_grad():
|
| 108 |
if all_pixel_values:
|
| 109 |
+
outputs = model.generate(**inputs, pixel_values=all_pixel_values, image_metas=combined_metas, **generation_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
else:
|
|
|
|
| 111 |
outputs = model.generate(**inputs, **generation_args)
|
|
|
|
|
|
|
| 112 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 113 |
+
return response.split("<|im_start|>assistant\n")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
+
print(f"โ ์๋ต ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}"); traceback.print_exc(); return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
|
| 118 |
+
# --- 4. Gradio UI ๋ฐ ์คํ (์ต์ข
์์ ) ---
|
| 119 |
with gr.Blocks(title="Lily LLM System", theme=gr.themes.Soft()) as demo:
|
| 120 |
gr.Markdown("# ๐งฎ Lily LLM System")
|
| 121 |
gr.Markdown("์ด๋ฏธ์ง, PDF, ํ
์คํธ๋ฅผ ์ดํดํ๊ณ ๋ต๋ณํ๋ ๋ฉํฐ๋ชจ๋ฌ AI ์์คํ
์
๋๋ค.")
|
| 122 |
|
| 123 |
+
with gr.Tabs():
|
| 124 |
with gr.Tab("๐ฌ ์ฑํ
"):
|
| 125 |
chat_prompt = "<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 126 |
+
chatbot = gr.Chatbot(height=320, label="๋ํ์ฐฝ", elem_id="chatbot", type="messages")
|
| 127 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
with gr.Row():
|
| 129 |
+
msg = gr.Textbox(label="๋ฉ์์ง ์
๋ ฅ", placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์", lines=3, show_label=False, scale=4)
|
| 130 |
+
file_input = gr.File(label="ํ์ผ ์
๋ก๋", file_count="multiple", file_types=[".pdf", ".png", ".jpg", ".jpeg"], scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
send_btn = gr.Button("์ ์ก", variant="primary", scale=1)
|
| 132 |
|
| 133 |
+
# โ
1. respond ํจ์๊ฐ 'files'๋ฅผ ์ธ ๋ฒ์งธ ์ธ์๋ก ๋ฐ๋๋ก ์์
|
| 134 |
def respond(message, chat_history, files):
|
| 135 |
if not message.strip() and not files:
|
| 136 |
+
return "", chat_history, None # files ์ถ๋ ฅ๋ ๋น์์ค
|
|
|
|
| 137 |
|
| 138 |
bot_message = generate_response(chat_prompt, message, files)
|
| 139 |
|
|
|
|
| 140 |
chat_history.append({"role": "user", "content": message})
|
| 141 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 142 |
|
| 143 |
+
# โ
2. ์ถ๋ ฅ์ ๊ฐ์๋ฅผ inputs์ ๋ง์ถ๊ธฐ ์ํด file_input๋ ๋ฐํ๊ฐ์ ์ถ๊ฐ
|
| 144 |
+
return "", chat_history, None
|
| 145 |
+
|
| 146 |
+
# โ
3. click๊ณผ submit์ inputs ๋ฆฌ์คํธ์ 'file_input' ์ถ๊ฐ
|
| 147 |
+
send_btn.click(
|
| 148 |
+
respond,
|
| 149 |
+
inputs=[msg, chatbot, file_input],
|
| 150 |
+
outputs=[msg, chatbot, file_input], # ์ถ๋ ฅ์๋ file_input ์ถ๊ฐ
|
| 151 |
+
api_name="chat", # api_name์ ์ฌ๋์ ์์ด ์ฌ์ฉ
|
| 152 |
+
# queue=False
|
| 153 |
+
)
|
| 154 |
+
msg.submit(
|
| 155 |
+
respond,
|
| 156 |
+
inputs=[msg, chatbot, file_input],
|
| 157 |
+
outputs=[msg, chatbot, file_input], # ์ถ๋ ฅ์๋ file_input ์ถ๊ฐ
|
| 158 |
+
api_name="chat",
|
| 159 |
+
# queue=False
|
| 160 |
+
)
|
| 161 |
|
| 162 |
with gr.Tab("โ๏ธ ์์คํ
์ ๋ณด"):
|
| 163 |
gr.Markdown(f"**์คํ ํ๊ฒฝ**: `{'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}`")
|
| 164 |
gr.Markdown(f"**๋ชจ๋ธ ๊ฒฝ๋ก**: `{MODEL_PATH}`")
|
| 165 |
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: `{'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}`")
|
| 166 |
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
if IS_LOCAL:
|
| 169 |
+
print("\n๐ ๋ก์ปฌ ์๋ฒ๋ฅผ ์์ํฉ๋๋ค. http://127.0.0.1:8006")
|
| 170 |
+
demo.launch(server_name="127.0.0.1", server_port=8006, share=False)
|
| 171 |
else:
|
| 172 |
print("\n๐ ์๋ฒ๋ฅผ ์์ํฉ๋๋ค...")
|
| 173 |
demo.launch()
|
test_input.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from gradio_client import Client, file
|
| 3 |
-
|
| 4 |
-
# --- ์ค์ ---
|
| 5 |
-
# ๋ก์ปฌ Gradio ์๋ฒ ์ฃผ์ (app.py ์คํ ์ ํฐ๋ฏธ๋์ ํ์๋๋ ์ฃผ์)
|
| 6 |
-
SERVER_URL = "http://localhost:8006/"
|
| 7 |
-
|
| 8 |
-
def run_chat_test(client):
|
| 9 |
-
"""์ผ๋ฐ ์ฑํ
ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 10 |
-
print("\n--- ๐ฌ ์ผ๋ฐ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 11 |
-
|
| 12 |
-
test_message = "์๋
ํ์ธ์! ์ค๋ ๋ ์จ๋ ์ด๋ค๊ฐ์?"
|
| 13 |
-
chat_history = [] # ์ด๊ธฐ ๋ํ ๋ด์ญ์ ๋น์ด์์
|
| 14 |
-
|
| 15 |
-
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 16 |
-
|
| 17 |
-
# `respond` ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 0)
|
| 18 |
-
# ์
๋ ฅ: (๋ฉ์์ง, ์ฑํ
๋ด์ญ, ํ์ผ)
|
| 19 |
-
# ์ถ๋ ฅ: (๋น์์ง ํ
์คํธ ๋ฐ์ค, ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ)
|
| 20 |
-
result = client.predict(
|
| 21 |
-
test_message,
|
| 22 |
-
chat_history,
|
| 23 |
-
None, # ํ์ผ ์์
|
| 24 |
-
fn_index=0
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ์์ ๋ง์ง๋ง ์๋ต(๋ด ๋ฉ์์ง)์ ์ถ์ถ
|
| 28 |
-
updated_history = result[1]
|
| 29 |
-
bot_response = updated_history[-1]['content']
|
| 30 |
-
|
| 31 |
-
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 32 |
-
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 33 |
-
|
| 34 |
-
def run_math_test(client):
|
| 35 |
-
"""์ํ ๋ฌธ์ ํด๊ฒฐ ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 36 |
-
print("\n--- ๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ ํ
์คํธ ์์ ---")
|
| 37 |
-
|
| 38 |
-
test_problem = "๋ ๊ฐ์ ์ฐ์๋ ์ง์์ ํฉ์ด 34์ผ ๋, ๋ ์ง์๋ ๋ฌด์์ธ๊ฐ์?"
|
| 39 |
-
|
| 40 |
-
print(f"๋ณด๋ด๋ ๋ฌธ์ : '{test_problem}'")
|
| 41 |
-
|
| 42 |
-
# ์ํ ๋ฌธ์ ํด๊ฒฐ ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 1)
|
| 43 |
-
# ์
๋ ฅ: (์ํ ๋ฌธ์ , ํ์ผ)
|
| 44 |
-
# ์ถ๋ ฅ: (๊ฒฐ๊ณผ ํ
์คํธ)
|
| 45 |
-
result = client.predict(
|
| 46 |
-
test_problem,
|
| 47 |
-
None, # ํ์ผ ์์
|
| 48 |
-
fn_index=1
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 52 |
-
print(f"๐ค ๋ฐ์ ์๋ต (์ผ๋ถ): '{result[:200]}...'")
|
| 53 |
-
|
| 54 |
-
def run_file_test(client):
|
| 55 |
-
"""ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 56 |
-
print("\n--- ๐ ํ์ผ ์
๋ก๋ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 57 |
-
|
| 58 |
-
# ํ
์คํธ์ฉ ์์ ํ
์คํธ ํ์ผ ์์ฑ
|
| 59 |
-
temp_file_path = "test_document.txt"
|
| 60 |
-
with open(temp_file_path, "w", encoding="utf-8") as f:
|
| 61 |
-
f.write("์ด ํ์ผ์ ํ
์คํธ๋ฅผ ์ํด ์์ฑ๋์์ต๋๋ค.\n")
|
| 62 |
-
f.write("ํ์ผ์ ํต์ฌ ๋ด์ฉ์ '๋ํ๋ฏผ๊ตญ์ ์๋๋ ์์ธ์ด๋ค' ์
๋๋ค.")
|
| 63 |
-
|
| 64 |
-
print(f"์
๋ก๋ํ ํ์ผ: '{temp_file_path}'")
|
| 65 |
-
test_message = "์
๋ก๋ํ ํ์ผ์ ํต์ฌ ๋ด์ฉ์ด ๋ญ์ผ?"
|
| 66 |
-
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 67 |
-
|
| 68 |
-
# `file()` ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์๋ฒ์ ์
๋ก๋ ๊ฐ๋ฅํ ํํ๋ก ๋ณํ
|
| 69 |
-
result = client.predict(
|
| 70 |
-
test_message,
|
| 71 |
-
[], # ์ฑํ
๋ด์ญ ์์
|
| 72 |
-
file(temp_file_path),
|
| 73 |
-
fn_index=0
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
# ์์ ํ์ผ ์ญ์
|
| 77 |
-
os.remove(temp_file_path)
|
| 78 |
-
|
| 79 |
-
bot_response = result[1][-1]['content']
|
| 80 |
-
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 81 |
-
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
if __name__ == "__main__":
|
| 85 |
-
print(f"Gradio ์๋ฒ({SERVER_URL})์ ์ฐ๊ฒฐ์ ์๋ํฉ๋๋ค...")
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
# ์๋ฒ์ ํด๋ผ์ด์ธํธ๋ก ์ฐ๊ฒฐ
|
| 89 |
-
client = Client(SERVER_URL, verbose=False)
|
| 90 |
-
print("โ
์๋ฒ ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
| 91 |
-
|
| 92 |
-
# ํ
์คํธ ์คํ
|
| 93 |
-
run_chat_test(client)
|
| 94 |
-
run_math_test(client)
|
| 95 |
-
# run_file_test(client) # ํ์ผ ํ
์คํธ๋ ํ์์ ์ฃผ์ ํด์ ํ์ฌ ์ฌ์ฉ
|
| 96 |
-
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"\nโ ํ
์คํธ ์คํจ: ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์๊ฑฐ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.")
|
| 99 |
-
print("๋จผ์ ๋ค๋ฅธ ํฐ๋ฏธ๋์์ 'python app.py'๋ฅผ ์คํํ๋์ง ํ์ธํด์ฃผ์ธ์.")
|
| 100 |
-
print(f"์ค๋ฅ ์์ธ ์ ๋ณด: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_text.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from gradio_client import Client, file
|
| 3 |
-
|
| 4 |
-
# --- ์ค์ ---
|
| 5 |
-
# ๋ก์ปฌ Gradio ์๋ฒ ์ฃผ์ (app.py ์คํ ์ ํฐ๋ฏธ๋์ ํ์๋๋ ์ฃผ์)
|
| 6 |
-
SERVER_URL = "http://localhost:8006/"
|
| 7 |
-
|
| 8 |
-
def run_chat_test(client):
|
| 9 |
-
"""์ผ๋ฐ ์ฑํ
ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 10 |
-
print("\n--- ๐ฌ ์ผ๋ฐ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 11 |
-
|
| 12 |
-
test_message = "์๋
ํ์ธ์! ์ค๋ ๋ ์จ๋ ์ด๋ค๊ฐ์?"
|
| 13 |
-
chat_history = [] # ์ด๊ธฐ ๋ํ ๋ด์ญ์ ๋น์ด์์
|
| 14 |
-
|
| 15 |
-
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 16 |
-
|
| 17 |
-
# `respond` ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 0)
|
| 18 |
-
# ์
๋ ฅ: (๋ฉ์์ง, ์ฑํ
๋ด์ญ, ํ์ผ)
|
| 19 |
-
# ์ถ๋ ฅ: (๋น์์ง ํ
์คํธ ๋ฐ์ค, ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ)
|
| 20 |
-
result = client.predict(
|
| 21 |
-
test_message,
|
| 22 |
-
chat_history,
|
| 23 |
-
None, # ํ์ผ ์์
|
| 24 |
-
fn_index=0
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ์์ ๋ง์ง๋ง ์๋ต(๋ด ๋ฉ์์ง)์ ์ถ์ถ
|
| 28 |
-
updated_history = result[1]
|
| 29 |
-
bot_response = updated_history[-1]['content']
|
| 30 |
-
|
| 31 |
-
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 32 |
-
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 33 |
-
|
| 34 |
-
def run_math_test(client):
|
| 35 |
-
"""์ํ ๋ฌธ์ ํด๊ฒฐ ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 36 |
-
print("\n--- ๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ ํ
์คํธ ์์ ---")
|
| 37 |
-
|
| 38 |
-
test_problem = "๋ ๊ฐ์ ์ฐ์๋ ์ง์์ ํฉ์ด 34์ผ ๋, ๋ ์ง์๋ ๋ฌด์์ธ๊ฐ์?"
|
| 39 |
-
|
| 40 |
-
print(f"๋ณด๋ด๋ ๋ฌธ์ : '{test_problem}'")
|
| 41 |
-
|
| 42 |
-
# ์ํ ๋ฌธ์ ํด๊ฒฐ ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 1)
|
| 43 |
-
# ์
๋ ฅ: (์ํ ๋ฌธ์ , ํ์ผ)
|
| 44 |
-
# ์ถ๋ ฅ: (๊ฒฐ๊ณผ ํ
์คํธ)
|
| 45 |
-
result = client.predict(
|
| 46 |
-
test_problem,
|
| 47 |
-
None, # ํ์ผ ์์
|
| 48 |
-
fn_index=1
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 52 |
-
print(f"๐ค ๋ฐ์ ์๋ต (์ผ๋ถ): '{result[:200]}...'")
|
| 53 |
-
|
| 54 |
-
def run_file_test(client):
|
| 55 |
-
"""ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 56 |
-
print("\n--- ๐ ํ์ผ ์
๋ก๋ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 57 |
-
|
| 58 |
-
# ํ
์คํธ์ฉ ์์ ํ
์คํธ ํ์ผ ์์ฑ
|
| 59 |
-
temp_file_path = "test_document.txt"
|
| 60 |
-
with open(temp_file_path, "w", encoding="utf-8") as f:
|
| 61 |
-
f.write("์ด ํ์ผ์ ํ
์คํธ๋ฅผ ์ํด ์์ฑ๋์์ต๋๋ค.\n")
|
| 62 |
-
f.write("ํ์ผ์ ํต์ฌ ๋ด์ฉ์ '๋ํ๋ฏผ๊ตญ์ ์๋๋ ์์ธ์ด๋ค' ์
๋๋ค.")
|
| 63 |
-
|
| 64 |
-
print(f"์
๋ก๋ํ ํ์ผ: '{temp_file_path}'")
|
| 65 |
-
test_message = "์
๋ก๋ํ ํ์ผ์ ํต์ฌ ๋ด์ฉ์ด ๋ญ์ผ?"
|
| 66 |
-
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 67 |
-
|
| 68 |
-
# `file()` ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์๋ฒ์ ์
๋ก๋ ๊ฐ๋ฅํ ํํ๋ก ๋ณํ
|
| 69 |
-
result = client.predict(
|
| 70 |
-
test_message,
|
| 71 |
-
[], # ์ฑํ
๋ด์ญ ์์
|
| 72 |
-
file(temp_file_path),
|
| 73 |
-
fn_index=0
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
# ์์ ํ์ผ ์ญ์
|
| 77 |
-
os.remove(temp_file_path)
|
| 78 |
-
|
| 79 |
-
bot_response = result[1][-1]['content']
|
| 80 |
-
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 81 |
-
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
if __name__ == "__main__":
|
| 85 |
-
print(f"Gradio ์๋ฒ({SERVER_URL})์ ์ฐ๊ฒฐ์ ์๋ํฉ๋๋ค...")
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
# ์๋ฒ์ ํด๋ผ์ด์ธํธ๋ก ์ฐ๊ฒฐ
|
| 89 |
-
client = Client(SERVER_URL, verbose=False)
|
| 90 |
-
print("โ
์๋ฒ ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
| 91 |
-
|
| 92 |
-
# ํ
์คํธ ์คํ
|
| 93 |
-
run_chat_test(client)
|
| 94 |
-
run_math_test(client)
|
| 95 |
-
# run_file_test(client) # ํ์ผ ํ
์คํธ๋ ํ์์ ์ฃผ์ ํด์ ํ์ฌ ์ฌ์ฉ
|
| 96 |
-
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"\nโ ํ
์คํธ ์คํจ: ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์๊ฑฐ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.")
|
| 99 |
-
print("๋จผ์ ๋ค๋ฅธ ํฐ๋ฏธ๋์์ 'python app.py'๋ฅผ ์คํํ๋์ง ํ์ธํด์ฃผ์ธ์.")
|
| 100 |
-
print(f"์ค๋ฅ ์์ธ ์ ๋ณด: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_tokenizer.py
DELETED
|
@@ -1,159 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import traceback
|
| 3 |
-
from typing import Optional
|
| 4 |
-
from transformers import AutoTokenizer
|
| 5 |
-
import torch
|
| 6 |
-
|
| 7 |
-
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
| 8 |
-
try:
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
load_dotenv()
|
| 11 |
-
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 12 |
-
except ImportError:
|
| 13 |
-
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์")
|
| 14 |
-
|
| 15 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
-
|
| 17 |
-
# ํ๊ฒฝ ๊ฐ์ง
|
| 18 |
-
IS_LOCAL = os.path.exists('../.env') or 'LOCAL_TEST' in os.environ
|
| 19 |
-
print(f"๐ ํ๊ฒฝ: {'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}")
|
| 20 |
-
|
| 21 |
-
# ํ๊ฒฝ์ ๋ฐ๋ฅธ ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
| 22 |
-
if IS_LOCAL:
|
| 23 |
-
# ๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก (hearth_llm_model ํด๋ ์ฌ์ฉ)
|
| 24 |
-
MODEL_PATH = "../lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 25 |
-
print(f"๐ ๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก: {MODEL_PATH}")
|
| 26 |
-
print(f"๐ ๊ฒฝ๋ก ์กด์ฌ: {os.path.exists(MODEL_PATH)}")
|
| 27 |
-
else:
|
| 28 |
-
# ์๋ฒ์์๋ Hugging Face ๋ชจ๋ธ ์ฌ์ฉ
|
| 29 |
-
MODEL_PATH = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 30 |
-
print(f"๐ ์๋ฒ ๋ชจ๋ธ: {MODEL_PATH}")
|
| 31 |
-
|
| 32 |
-
print(f"๐ ํ ํฐ: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 33 |
-
|
| 34 |
-
# ํ ํฌ๋์ด์ ํ
์คํธ
|
| 35 |
-
print("\n๐ง ํ ํฌ๋์ด์ ํ
์คํธ ์์...")
|
| 36 |
-
|
| 37 |
-
try:
|
| 38 |
-
print("๐ค ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 39 |
-
print(f" MODEL_PATH: {MODEL_PATH}")
|
| 40 |
-
print(f" IS_LOCAL: {IS_LOCAL}")
|
| 41 |
-
print(f" trust_remote_code: True")
|
| 42 |
-
print(f" use_fast: False")
|
| 43 |
-
|
| 44 |
-
if IS_LOCAL:
|
| 45 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 46 |
-
MODEL_PATH,
|
| 47 |
-
trust_remote_code=True,
|
| 48 |
-
)
|
| 49 |
-
else:
|
| 50 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 51 |
-
MODEL_PATH,
|
| 52 |
-
token=HF_TOKEN,
|
| 53 |
-
trust_remote_code=True,
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
print(f"โ
ํ ํฌ๋์ด์ ๋ก๋ฉ ์๋ฃ")
|
| 57 |
-
print(f" ํ์
: {type(tokenizer)}")
|
| 58 |
-
print(f" ๊ฐ: {tokenizer}")
|
| 59 |
-
print(f" hasattr('encode'): {hasattr(tokenizer, 'encode')}")
|
| 60 |
-
print(f" hasattr('__call__'): {hasattr(tokenizer, '__call__')}")
|
| 61 |
-
|
| 62 |
-
# ํ ํฌ๋์ด์ ํ
์คํธ
|
| 63 |
-
test_input = "์๋
ํ์ธ์"
|
| 64 |
-
print(f"\n๐ค ํ ํฌ๋์ด์ ํ
์คํธ: '{test_input}'")
|
| 65 |
-
|
| 66 |
-
test_tokens = tokenizer(test_input, return_tensors="pt")
|
| 67 |
-
print(f" โ
ํ ํฌ๋์ด์ ํธ์ถ ์ฑ๊ณต")
|
| 68 |
-
print(f" input_ids shape: {test_tokens['input_ids'].shape}")
|
| 69 |
-
print(f" attention_mask shape: {test_tokens['attention_mask'].shape}")
|
| 70 |
-
|
| 71 |
-
# ๋์ฝ๋ฉ ํ
์คํธ
|
| 72 |
-
decoded = tokenizer.decode(test_tokens['input_ids'][0], skip_special_tokens=True)
|
| 73 |
-
print(f" ๋์ฝ๋ฉ ๊ฒฐ๊ณผ: '{decoded}'")
|
| 74 |
-
|
| 75 |
-
except Exception as e:
|
| 76 |
-
print(f"โ ํ ํฌ๋์ด์ ํ
์คํธ ์คํจ: {e}")
|
| 77 |
-
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 78 |
-
traceback.print_exc()
|
| 79 |
-
|
| 80 |
-
# ๋ชจ๋ธ ํ
์คํธ
|
| 81 |
-
print("\n๐ง ๋ชจ๋ธ ํ
์คํธ ์์...")
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
print("๐ค ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 85 |
-
from modeling import KananaVForConditionalGeneration
|
| 86 |
-
|
| 87 |
-
if IS_LOCAL:
|
| 88 |
-
model = KananaVForConditionalGeneration.from_pretrained(
|
| 89 |
-
MODEL_PATH,
|
| 90 |
-
torch_dtype=torch.float16,
|
| 91 |
-
trust_remote_code=True,
|
| 92 |
-
device_map=None,
|
| 93 |
-
low_cpu_mem_usage=True
|
| 94 |
-
)
|
| 95 |
-
else:
|
| 96 |
-
model = KananaVForConditionalGeneration.from_pretrained(
|
| 97 |
-
MODEL_PATH,
|
| 98 |
-
token=HF_TOKEN,
|
| 99 |
-
torch_dtype=torch.float16,
|
| 100 |
-
trust_remote_code=True,
|
| 101 |
-
device_map=None,
|
| 102 |
-
low_cpu_mem_usage=True
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
print(f"โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 106 |
-
# print(f" ํ์
: {type(model)}")
|
| 107 |
-
# print(f" ๋๋ฐ์ด์ค: {next(model.parameters()).device}")
|
| 108 |
-
|
| 109 |
-
# ๋ชจ๋ธ ํ
์คํธ
|
| 110 |
-
test_input = "์๋
ํ์ธ์"
|
| 111 |
-
formatted_prompt = f"<|im_start|>user\n{test_input}<|im_end|>\n<|im_start|>assistant\n"
|
| 112 |
-
max_length: Optional[int] = None
|
| 113 |
-
|
| 114 |
-
inputs = tokenizer(
|
| 115 |
-
formatted_prompt,
|
| 116 |
-
return_tensors="pt",
|
| 117 |
-
padding=True,
|
| 118 |
-
truncation=True,
|
| 119 |
-
max_length=512
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
print(f"\n๐ค ๋ชจ๋ธ ์ถ๋ก ํ
์คํธ: '{test_input}'")
|
| 123 |
-
|
| 124 |
-
# Kanana์ฉ ์์ฑ ์ค์
|
| 125 |
-
max_new_tokens = max_length or 100
|
| 126 |
-
|
| 127 |
-
with torch.no_grad():
|
| 128 |
-
outputs = model.generate(
|
| 129 |
-
input_ids=inputs["input_ids"],
|
| 130 |
-
attention_mask=inputs["attention_mask"],
|
| 131 |
-
max_new_tokens=max_new_tokens,
|
| 132 |
-
repetition_penalty=1.1,
|
| 133 |
-
no_repeat_ngram_size=2,
|
| 134 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 135 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 136 |
-
use_cache=True
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
print(f" โ
๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 140 |
-
print(f" outputs ํ์
: {type(outputs)}")
|
| 141 |
-
print(f" outputs shape: {outputs.shape}")
|
| 142 |
-
|
| 143 |
-
# ๋์ฝ๋ฉ ํ
์คํธ
|
| 144 |
-
# model.generate()์ ์ถ๋ ฅ์ ์ ์ฒด ์ํ์ค์ด๋ฏ๋ก ๋ฐ๋ก ๋์ฝ๋ฉํฉ๋๋ค.
|
| 145 |
-
# outputs[0]์ ๋ฐฐ์น ์ค ์ฒซ ๋ฒ์งธ ๊ฒฐ๊ณผ๋ฅผ ์๋ฏธํฉ๋๋ค.
|
| 146 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 147 |
-
|
| 148 |
-
# ์
๋ ฅ ํ๋กฌํํธ๋ฅผ ์๋ต์์ ์ ๊ฑฐ (์ ํ์ฌํญ)
|
| 149 |
-
assistant_response = response.split("<|im_start|>assistant\n")[-1]
|
| 150 |
-
|
| 151 |
-
print(f" ์์ฑ๋ ์ ์ฒด ํ
์คํธ: '{response}'")
|
| 152 |
-
print(f" ์ด์์คํดํธ ์๋ต: '{assistant_response.strip()}'")
|
| 153 |
-
|
| 154 |
-
except Exception as e:
|
| 155 |
-
print(f"โ ๋ชจ๋ธ ํ
์คํธ ์คํจ: {e}")
|
| 156 |
-
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 157 |
-
traceback.print_exc()
|
| 158 |
-
|
| 159 |
-
print("\nโ
ํ
์คํธ ์๋ฃ!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|