Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,29 @@
|
|
| 1 |
-
import
|
| 2 |
import torch
|
| 3 |
import json
|
| 4 |
import threading
|
| 5 |
-
import time
|
| 6 |
-
import faiss
|
| 7 |
-
import numpy as np
|
| 8 |
-
import gradio as gr
|
| 9 |
-
|
| 10 |
-
from sentence_transformers import SentenceTransformer
|
| 11 |
from transformers import (
|
| 12 |
-
AutoModelForCausalLM,
|
| 13 |
AutoTokenizer,
|
| 14 |
-
|
|
|
|
| 15 |
)
|
| 16 |
from peft import PeftModel
|
| 17 |
-
from
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# ============================================================
|
| 20 |
-
# 1.
|
| 21 |
# ============================================================
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
-
print("Device:", device)
|
| 28 |
|
| 29 |
-
# ============================================================
|
| 30 |
-
# 2. ๋ชจ๋ธ ๋ฐ ๊ฒฝ๋ก ์ค์
|
| 31 |
-
# ============================================================
|
| 32 |
-
BASE_MODEL = "KORMo-Team/KORMo-10B-sft"
|
| 33 |
-
LORA_DIR = "./peft_lora" # Space์ ์
๋ก๋ํ LoRA ํด๋
|
| 34 |
-
DOC_PATH = "./rule.json" # Space์ ์
๋ก๋ํ rule.json
|
| 35 |
-
|
| 36 |
-
print("Paths:")
|
| 37 |
-
print("Model:", BASE_MODEL)
|
| 38 |
-
print("LoRA:", LORA_DIR)
|
| 39 |
-
print("Documents:", DOC_PATH)
|
| 40 |
|
| 41 |
# ============================================================
|
| 42 |
-
#
|
| 43 |
# ============================================================
|
| 44 |
with open(DOC_PATH, "r", encoding="utf-8") as f:
|
| 45 |
documents = json.load(f)
|
|
@@ -47,58 +31,42 @@ with open(DOC_PATH, "r", encoding="utf-8") as f:
|
|
| 47 |
doc_texts = [d["text"] for d in documents]
|
| 48 |
|
| 49 |
embedding_model = SentenceTransformer("jhgan/ko-sroberta-multitask", device=device)
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
doc_texts,
|
| 53 |
-
convert_to_numpy=True,
|
| 54 |
-
show_progress_bar=True
|
| 55 |
-
).astype("float32")
|
| 56 |
-
|
| 57 |
-
dim = doc_embs.shape[1]
|
| 58 |
-
index = faiss.IndexFlatL2(dim)
|
| 59 |
index.add(doc_embs)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# ============================================================
|
| 64 |
-
#
|
| 65 |
# ============================================================
|
|
|
|
| 66 |
model = AutoModelForCausalLM.from_pretrained(
|
| 67 |
BASE_MODEL,
|
| 68 |
-
torch_dtype=torch.float16,
|
| 69 |
device_map="auto",
|
|
|
|
| 70 |
trust_remote_code=True
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True)
|
| 74 |
-
|
| 75 |
-
if tokenizer.pad_token is None:
|
| 76 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 77 |
-
|
| 78 |
-
print("Loading LoRA...")
|
| 79 |
model = PeftModel.from_pretrained(
|
| 80 |
model,
|
| 81 |
LORA_DIR,
|
| 82 |
-
torch_dtype=torch.float16,
|
| 83 |
device_map="auto",
|
|
|
|
| 84 |
)
|
| 85 |
model.eval()
|
| 86 |
|
| 87 |
-
print("Model + LoRA loaded successfully.")
|
| 88 |
-
|
| 89 |
-
# ============================================================
|
| 90 |
-
# 5. RAG ๊ฒ์ ํจ์
|
| 91 |
-
# ============================================================
|
| 92 |
-
def retrieve(query, k=3):
|
| 93 |
-
q_emb = embedding_model.encode([query], convert_to_numpy=True).astype("float32")
|
| 94 |
-
D, I = index.search(q_emb, k)
|
| 95 |
-
return [documents[i] for i in I[0]]
|
| 96 |
|
| 97 |
# ============================================================
|
| 98 |
-
#
|
| 99 |
# ============================================================
|
| 100 |
def build_prompt(persona, instruction, query, retrieved_docs):
|
| 101 |
context = "\n".join([f"- {d['text']}" for d in retrieved_docs])
|
|
|
|
| 102 |
return f"""
|
| 103 |
### ํ๋ฅด์๋:
|
| 104 |
{persona}
|
|
@@ -115,73 +83,82 @@ def build_prompt(persona, instruction, query, retrieved_docs):
|
|
| 115 |
### ๋ต๋ณ:
|
| 116 |
"""
|
| 117 |
|
|
|
|
| 118 |
# ============================================================
|
| 119 |
-
#
|
| 120 |
# ============================================================
|
| 121 |
-
def
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
retrieved = retrieve(user_query, k=k)
|
| 124 |
-
prompt = build_prompt(persona, instruction, user_query, retrieved)
|
| 125 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 126 |
|
| 127 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
thread = threading.Thread(target=run_generation)
|
| 146 |
thread.start()
|
| 147 |
|
| 148 |
-
|
| 149 |
-
for
|
| 150 |
-
|
| 151 |
-
yield
|
|
|
|
| 152 |
|
| 153 |
# ============================================================
|
| 154 |
-
#
|
| 155 |
# ============================================================
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import json
|
| 4 |
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import (
|
|
|
|
| 6 |
AutoTokenizer,
|
| 7 |
+
AutoModelForCausalLM,
|
| 8 |
+
TextIteratorStreamer
|
| 9 |
)
|
| 10 |
from peft import PeftModel
|
| 11 |
+
from sentence_transformers import SentenceTransformer
|
| 12 |
+
import faiss
|
| 13 |
+
import numpy as np
|
| 14 |
|
| 15 |
# ============================================================
|
| 16 |
+
# 1. ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
| 17 |
# ============================================================
|
| 18 |
+
BASE_MODEL = "KORMo-Team/KORMo-10B-sft"
|
| 19 |
+
LORA_DIR = "kormo_lora_checkpoints/peft_lora"
|
| 20 |
+
DOC_PATH = "rule.json"
|
| 21 |
|
| 22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# ============================================================
|
| 26 |
+
# 2. ๋ฌธ์ ๋ก๋ + ์๋ฒ ๋ฉ + FAISS
|
| 27 |
# ============================================================
|
| 28 |
with open(DOC_PATH, "r", encoding="utf-8") as f:
|
| 29 |
documents = json.load(f)
|
|
|
|
| 31 |
doc_texts = [d["text"] for d in documents]
|
| 32 |
|
| 33 |
embedding_model = SentenceTransformer("jhgan/ko-sroberta-multitask", device=device)
|
| 34 |
+
doc_embs = embedding_model.encode(doc_texts, convert_to_numpy=True).astype("float32")
|
| 35 |
|
| 36 |
+
index = faiss.IndexFlatL2(doc_embs.shape[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
index.add(doc_embs)
|
| 38 |
|
| 39 |
+
def retrieve(query, k=3):
|
| 40 |
+
q = embedding_model.encode([query], convert_to_numpy=True).astype("float32")
|
| 41 |
+
D, I = index.search(q, k)
|
| 42 |
+
return [documents[i] for i in I[0]]
|
| 43 |
+
|
| 44 |
|
| 45 |
# ============================================================
|
| 46 |
+
# 3. ๋ชจ๋ธ ๋ก๋
|
| 47 |
# ============================================================
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 49 |
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
BASE_MODEL,
|
|
|
|
| 51 |
device_map="auto",
|
| 52 |
+
torch_dtype=torch.float16,
|
| 53 |
trust_remote_code=True
|
| 54 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
model = PeftModel.from_pretrained(
|
| 56 |
model,
|
| 57 |
LORA_DIR,
|
|
|
|
| 58 |
device_map="auto",
|
| 59 |
+
torch_dtype=torch.float16,
|
| 60 |
)
|
| 61 |
model.eval()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# ============================================================
|
| 65 |
+
# 4. ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 66 |
# ============================================================
|
| 67 |
def build_prompt(persona, instruction, query, retrieved_docs):
|
| 68 |
context = "\n".join([f"- {d['text']}" for d in retrieved_docs])
|
| 69 |
+
|
| 70 |
return f"""
|
| 71 |
### ํ๋ฅด์๋:
|
| 72 |
{persona}
|
|
|
|
| 83 |
### ๋ต๋ณ:
|
| 84 |
"""
|
| 85 |
|
| 86 |
+
|
| 87 |
# ============================================================
|
| 88 |
+
# 5. Streaming generator (Gradio ์ฉ)
|
| 89 |
# ============================================================
|
| 90 |
+
def generate_stream(persona, instruction, query):
|
| 91 |
+
retrieved = retrieve(query)
|
| 92 |
+
prompt = build_prompt(persona, instruction, query, retrieved)
|
| 93 |
|
|
|
|
|
|
|
| 94 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 95 |
|
| 96 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 97 |
+
|
| 98 |
+
def run():
|
| 99 |
+
model.generate(
|
| 100 |
+
**inputs,
|
| 101 |
+
max_new_tokens=256,
|
| 102 |
+
do_sample=True,
|
| 103 |
+
top_p=0.9,
|
| 104 |
+
temperature=0.7,
|
| 105 |
+
repetition_penalty=1.2,
|
| 106 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 107 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 108 |
+
streamer=streamer
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
thread = threading.Thread(target=run)
|
|
|
|
|
|
|
|
|
|
| 112 |
thread.start()
|
| 113 |
|
| 114 |
+
partial = ""
|
| 115 |
+
for text in streamer:
|
| 116 |
+
partial += text
|
| 117 |
+
yield partial
|
| 118 |
+
|
| 119 |
|
| 120 |
# ============================================================
|
| 121 |
+
# 6. ํ๋ฅด์๋ 6๊ฐ ์๋ ์คํ ํจ์
|
| 122 |
# ============================================================
|
| 123 |
+
persona_group = [
|
| 124 |
+
("๋น์ ์ ์์น์ ์งํค๋ ์ํฉ์ ๋ฐ๋ผ ์ ์ฐํ๊ฒ ํ๋จํ๋ ์๊ฐ์ ๊ฐ์ง๊ณ ์๋ค. ๊ฐ์ธ์ ๋ฅ๋ ฅ๊ณผ ๊ธฐ์ฌ๋๋ฅผ ์ค์ํ๊ฒ ์๊ฐํ๋ฉฐ...", "๋ฐ์ธ์ฐ"),
|
| 125 |
+
("๋น์ ์ ๊ณต์ ํ ๊ท์น๊ณผ ์์น์ ์ค์ํ๋ฉด์, ๊ฐ์ธ์ ์ฑ๊ณผ์ ๋ฅ๋ ฅ์ ์ธ์ ํด ์ฐจ๋ฑ์ ๋๊ณ ๋ฐฐ๋ถํฉ๋๋ค...", "๊น์ฐฝ์ค"),
|
| 126 |
+
("๊ท์จ๊ณผ ์์จ์ ๊ท ํ์ ์งํค๋ฉฐ, ๋ฅ๋ ฅ๊ณผ ์ฑ๊ณผ๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋จํ๋ค...", "์ด์๊ธฐ"),
|
| 127 |
+
("๊ท์จ์ ๊ธฐ๋ฐ์ผ๋ก ํ์ง๋ง ์ ์ฐํ๋ฉฐ, ๋ถ๋ฐฐ๋ ์ค๋ฆฝ์ ์ด๊ณ ๊ฐ์ ์ ์ถ๊ตฌํ๋ค...", "์ฑํ"),
|
| 128 |
+
("์์จ์ ์กด์คํ๋ ์ต์ํ์ ๊ท์จ์ ์ ์งํ๋ฉฐ, ๊ธฐ์ฌ๋์ ๊ฐ์ ์ ๊ท ํ ์๊ฒ ๋ฐ์ํ๋ค...", "์ฉ์ฐ"),
|
| 129 |
+
("๊ท์จ๊ณผ ๊ณต์ ์ ๊ธฐ๋ฐ์ผ๋ก ์์ ์ ์ธ ์ด์์ ์ถ๊ตฌํ๋ฉฐ...", "ํ์ง")
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
instruction_text = """
|
| 133 |
+
๋น์ ์ ํด๋น ํ๋ฅด์๋์ ์ฑ๊ฒฉ์ ๊ฐ์ง ์ฌํ๊ด์
๋๋ค.
|
| 134 |
+
๋ฐ๋์ 3๋ฌธ์ฅ๋ง ๋งํ์ญ์์ค.
|
| 135 |
+
๊ฐ ๋ฌธ์ฅ์ 30์ ์ด๋ด๋ก ์ ํํฉ๋๋ค.
|
| 136 |
+
๊ท์ ์ ์ฐ์ ์ผ๋ก ๊ทผ๊ฑฐํ์ฌ ๋ตํ์์ค.
|
| 137 |
+
๋ฐ๋ณต ๊ธ์ง, ํ๋จ ๊ทผ๊ฑฐ ํ์.
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
+
def run_all_personas(query):
|
| 141 |
+
for persona, name in persona_group:
|
| 142 |
+
yield f"## ๐ค {name}\n"
|
| 143 |
+
stream = generate_stream(persona, instruction_text, query)
|
| 144 |
+
for chunk in stream:
|
| 145 |
+
yield chunk
|
| 146 |
+
yield "\n\n---\n\n"
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# ============================================================
|
| 150 |
+
# 7. Gradio UI
|
| 151 |
+
# ============================================================
|
| 152 |
+
with gr.Blocks() as demo:
|
| 153 |
+
gr.Markdown("# ๐ฅ KORMo 10B + LoRA Streaming Judge")
|
| 154 |
+
|
| 155 |
+
user_input = gr.Textbox(label="์ง๋ฌธ ์
๋ ฅ", value="3๋ฒ ์ด์ ๊ฒฐ์ํ์ง๋ง ์ค๋ ฅ์ ๋ฐ์ด๋ ์ ํ์์ ์ด๋ป๊ฒ ํด์ผ ํ ๊น?")
|
| 156 |
+
output = gr.Markdown()
|
| 157 |
+
|
| 158 |
+
def start(query):
|
| 159 |
+
return run_all_personas(query)
|
| 160 |
+
|
| 161 |
+
run_btn = gr.Button("๐ ์คํํ๊ธฐ")
|
| 162 |
+
run_btn.click(start, inputs=user_input, outputs=output)
|
| 163 |
+
|
| 164 |
+
demo.launch()
|