Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,32 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import threading
|
| 4 |
import torch
|
| 5 |
-
import gradio as gr
|
| 6 |
from huggingface_hub import login
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
import faiss
|
| 9 |
-
|
| 10 |
-
from transformers import
|
| 11 |
-
AutoModelForCausalLM,
|
| 12 |
-
AutoTokenizer,
|
| 13 |
-
TextIteratorStreamer
|
| 14 |
-
)
|
| 15 |
from peft import PeftModel
|
| 16 |
-
|
| 17 |
|
| 18 |
# ============================================================
|
| 19 |
-
#
|
| 20 |
# ============================================================
|
| 21 |
-
HF_TOKEN = os.
|
| 22 |
-
|
| 23 |
-
login(token=HF_TOKEN)
|
| 24 |
|
| 25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
-
|
| 27 |
|
| 28 |
# ============================================================
|
| 29 |
-
#
|
| 30 |
# ============================================================
|
| 31 |
BASE_MODEL = "KORMo-Team/KORMo-10B-sft"
|
| 32 |
-
LORA_DIR = "peft_lora"
|
| 33 |
-
DOC_PATH = "rule.json"
|
| 34 |
-
|
| 35 |
|
| 36 |
# ============================================================
|
| 37 |
-
#
|
| 38 |
# ============================================================
|
| 39 |
with open(DOC_PATH, "r", encoding="utf-8") as f:
|
| 40 |
documents = json.load(f)
|
|
@@ -46,23 +38,19 @@ embedding_model = SentenceTransformer(
|
|
| 46 |
device=device
|
| 47 |
)
|
| 48 |
|
| 49 |
-
import numpy as np
|
| 50 |
doc_embs = embedding_model.encode(
|
| 51 |
-
doc_texts,
|
| 52 |
-
convert_to_numpy=True,
|
| 53 |
-
show_progress_bar=True
|
| 54 |
).astype("float32")
|
| 55 |
|
| 56 |
dim = doc_embs.shape[1]
|
| 57 |
index = faiss.IndexFlatL2(dim)
|
| 58 |
index.add(doc_embs)
|
| 59 |
|
|
|
|
| 60 |
|
| 61 |
# ============================================================
|
| 62 |
-
#
|
| 63 |
# ============================================================
|
| 64 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True)
|
| 65 |
-
|
| 66 |
model = AutoModelForCausalLM.from_pretrained(
|
| 67 |
BASE_MODEL,
|
| 68 |
torch_dtype=torch.float16,
|
|
@@ -70,27 +58,30 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 70 |
trust_remote_code=True
|
| 71 |
)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
model = PeftModel.from_pretrained(
|
| 74 |
model,
|
| 75 |
LORA_DIR,
|
| 76 |
torch_dtype=torch.float16,
|
| 77 |
-
device_map="auto"
|
| 78 |
)
|
| 79 |
|
|
|
|
| 80 |
model.eval()
|
| 81 |
|
| 82 |
-
|
| 83 |
# ============================================================
|
| 84 |
-
#
|
| 85 |
# ============================================================
|
| 86 |
def retrieve(query, k=3):
|
| 87 |
q_emb = embedding_model.encode([query], convert_to_numpy=True).astype("float32")
|
| 88 |
D, I = index.search(q_emb, k)
|
| 89 |
return [documents[i] for i in I[0]]
|
| 90 |
|
| 91 |
-
|
| 92 |
# ============================================================
|
| 93 |
-
#
|
| 94 |
# ============================================================
|
| 95 |
def build_prompt(persona, instruction, query, retrieved_docs):
|
| 96 |
context = "\n".join([f"- {d['text']}" for d in retrieved_docs])
|
|
@@ -110,21 +101,25 @@ def build_prompt(persona, instruction, query, retrieved_docs):
|
|
| 110 |
### ๋ต๋ณ:
|
| 111 |
"""
|
| 112 |
|
| 113 |
-
|
| 114 |
# ============================================================
|
| 115 |
-
#
|
| 116 |
# ============================================================
|
| 117 |
-
def
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
skip_special_tokens=True
|
| 122 |
-
)
|
| 123 |
|
| 124 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
model.generate(
|
| 129 |
**inputs,
|
| 130 |
max_new_tokens=max_new_tokens,
|
|
@@ -134,51 +129,56 @@ def stream_generate(prompt, max_new_tokens=256):
|
|
| 134 |
repetition_penalty=1.2,
|
| 135 |
streamer=streamer
|
| 136 |
)
|
| 137 |
-
)
|
| 138 |
-
thread.start()
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
for token in streamer:
|
| 143 |
-
partial_text += token
|
| 144 |
-
|
| 145 |
-
# End of Answer ๊ธฐ์ค์ผ๋ก ์คํธ๋ฆผ ์ค๋จ
|
| 146 |
-
if "End of Answer" in partial_text:
|
| 147 |
-
partial_text = partial_text.split("End of Answer")[0]
|
| 148 |
-
yield partial_text.strip()
|
| 149 |
-
return
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
|
|
|
| 153 |
|
| 154 |
# ============================================================
|
| 155 |
-
#
|
| 156 |
# ============================================================
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
# ============================================================
|
| 164 |
-
#
|
| 165 |
# ============================================================
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
gr.Markdown("KORMo-10B + LoRA + RAG Streaming Demo (End-of-Answer Truncated)")
|
| 169 |
-
|
| 170 |
-
persona = gr.Textbox(label="ํ๋ฅด์๋")
|
| 171 |
-
instruction = gr.Textbox(label="๊ท์น/์ง์นจ")
|
| 172 |
-
query = gr.Textbox(label="์ง๋ฌธ")
|
| 173 |
|
| 174 |
-
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
)
|
| 183 |
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import torch
|
|
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import faiss
|
| 7 |
+
import numpy as np
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from peft import PeftModel
|
| 10 |
+
import threading
|
| 11 |
|
| 12 |
# ============================================================
|
| 13 |
+
# 1. ํ๊ฒฝ ์ค์ + ๋ก๊ทธ์ธ
|
| 14 |
# ============================================================
|
| 15 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # โ secret variable์์ ๋ถ๋ฌ์ด
|
| 16 |
+
login(token=HF_TOKEN)
|
|
|
|
| 17 |
|
| 18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
print("Device:", device)
|
| 20 |
|
| 21 |
# ============================================================
|
| 22 |
+
# 2. ๊ฒฝ๋ก ์ค์
|
| 23 |
# ============================================================
|
| 24 |
BASE_MODEL = "KORMo-Team/KORMo-10B-sft"
|
| 25 |
+
LORA_DIR = "./peft_lora" # ์๋ฒ ๊ฒฝ๋ก
|
| 26 |
+
DOC_PATH = "./rule.json" # ๋ฌธ์ ํ์ผ
|
|
|
|
| 27 |
|
| 28 |
# ============================================================
|
| 29 |
+
# 3. RAG ๋ฌธ์ ๋ก๋ + FAISS ๊ตฌ์ถ
|
| 30 |
# ============================================================
|
| 31 |
with open(DOC_PATH, "r", encoding="utf-8") as f:
|
| 32 |
documents = json.load(f)
|
|
|
|
| 38 |
device=device
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
doc_embs = embedding_model.encode(
|
| 42 |
+
doc_texts, convert_to_numpy=True
|
|
|
|
|
|
|
| 43 |
).astype("float32")
|
| 44 |
|
| 45 |
dim = doc_embs.shape[1]
|
| 46 |
index = faiss.IndexFlatL2(dim)
|
| 47 |
index.add(doc_embs)
|
| 48 |
|
| 49 |
+
print("FAISS index built:", index.ntotal)
|
| 50 |
|
| 51 |
# ============================================================
|
| 52 |
+
# 4. LLM + LoRA ๋ก๋
|
| 53 |
# ============================================================
|
|
|
|
|
|
|
| 54 |
model = AutoModelForCausalLM.from_pretrained(
|
| 55 |
BASE_MODEL,
|
| 56 |
torch_dtype=torch.float16,
|
|
|
|
| 58 |
trust_remote_code=True
|
| 59 |
)
|
| 60 |
|
| 61 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True)
|
| 62 |
+
if tokenizer.pad_token is None:
|
| 63 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 64 |
+
|
| 65 |
model = PeftModel.from_pretrained(
|
| 66 |
model,
|
| 67 |
LORA_DIR,
|
| 68 |
torch_dtype=torch.float16,
|
| 69 |
+
device_map="auto",
|
| 70 |
)
|
| 71 |
|
| 72 |
+
model = model.to(device)
|
| 73 |
model.eval()
|
| 74 |
|
|
|
|
| 75 |
# ============================================================
|
| 76 |
+
# 5. RAG ๊ฒ์ ํจ์
|
| 77 |
# ============================================================
|
| 78 |
def retrieve(query, k=3):
|
| 79 |
q_emb = embedding_model.encode([query], convert_to_numpy=True).astype("float32")
|
| 80 |
D, I = index.search(q_emb, k)
|
| 81 |
return [documents[i] for i in I[0]]
|
| 82 |
|
|
|
|
| 83 |
# ============================================================
|
| 84 |
+
# 6. ํ๋กฌํํธ ์์ฑ
|
| 85 |
# ============================================================
|
| 86 |
def build_prompt(persona, instruction, query, retrieved_docs):
|
| 87 |
context = "\n".join([f"- {d['text']}" for d in retrieved_docs])
|
|
|
|
| 101 |
### ๋ต๋ณ:
|
| 102 |
"""
|
| 103 |
|
|
|
|
| 104 |
# ============================================================
|
| 105 |
+
# 7. Streaming Chat
|
| 106 |
# ============================================================
|
| 107 |
+
def stream_chat(persona, instruction, user_query, max_new_tokens=256):
|
| 108 |
+
|
| 109 |
+
retrieved = retrieve(user_query, k=3)
|
| 110 |
+
prompt = build_prompt(persona, instruction, user_query, retrieved)
|
|
|
|
|
|
|
| 111 |
|
| 112 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 113 |
|
| 114 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 115 |
+
|
| 116 |
+
END_TOKENS = [
|
| 117 |
+
"End of Answer", "### ๊ฒํ ๊ฒฐ๊ณผ:", "### ์ต์ข
๋ต๋ณ",
|
| 118 |
+
"โป", ">", "**๋ต๋ณ**", "---", "###", "**"
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
def run_gen():
|
| 122 |
+
with torch.no_grad():
|
| 123 |
model.generate(
|
| 124 |
**inputs,
|
| 125 |
max_new_tokens=max_new_tokens,
|
|
|
|
| 129 |
repetition_penalty=1.2,
|
| 130 |
streamer=streamer
|
| 131 |
)
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
thread = threading.Thread(target=run_gen)
|
| 134 |
+
thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
full = ""
|
| 137 |
+
for tok in streamer:
|
| 138 |
+
print(tok, end="", flush=True)
|
| 139 |
+
full += tok
|
| 140 |
+
for e in END_TOKENS:
|
| 141 |
+
if e in full:
|
| 142 |
+
print()
|
| 143 |
+
return
|
| 144 |
|
| 145 |
+
print()
|
| 146 |
|
| 147 |
# ============================================================
|
| 148 |
+
# 8. ํ๋ฅด์๋ ๋ชฉ๋ก
|
| 149 |
# ============================================================
|
| 150 |
+
persona_group = [
|
| 151 |
+
("๋น์ ์ ์์น์ ์งํค๋ ์ํฉ์ ๋ฐ๋ผ ์ ์ฐํ๊ฒ ํ๋จํ๋ ์๊ฐ์ ๊ฐ์ง๊ณ ์๋ค...", "๋ฐ์ธ์ฐ"),
|
| 152 |
+
("๋น์ ์ ๊ณต์ ํ ๊ท์น๊ณผ ์์น์ ์ค์ํ๋ฉด์, ๊ฐ์ธ์ ์ฑ๊ณผ์ ๋ฅ๋ ฅ์ ์ธ์ ํด ์ฐจ๋ฑ...", "๊น์ฐฝ์ค"),
|
| 153 |
+
("๊ท์จ๊ณผ ์์จ์ ๊ท ํ์ ์งํค๋ฉฐ, ๋ฅ๋ ฅ๊ณผ ์ฑ๊ณผ๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋จํ๋ค...", "์ด์๊ธฐ"),
|
| 154 |
+
("๊ท์จ์ ๊ธฐ๋ฐ์ผ๋ก ํ์ง๋ง ์ ์ฐํ๋ฉฐ, ๋ถ๋ฐฐ๋ ์ค๋ฆฝ์ ์ด๊ณ ๊ฐ์ ์ ์ถ๊ตฌํ๋ค...", "์ฑํ"),
|
| 155 |
+
("์์จ์ ์กด์คํ๋ ์ต์ํ์ ๊ท์จ์ ์ ์งํ๋ฉฐ, ๊ธฐ์ฌ๋์ ๊ฐ์ ์ ๊ท ํ ์๊ฒ ๋ฐ์...", "์ฉ์ฐ"),
|
| 156 |
+
("๊ท์จ๊ณผ ๊ณต์ ์ ๊ธฐ๋ฐ์ผ๋ก ์์ ์ ์ธ ์ด์์ ์ถ๊ตฌํ๋ฉฐ, ๊ท ๋ฑยท๊ฐ์ ยท์น๋ชฉ ๊ฐ์ ๊ท ํ...", "ํ์ง")
|
| 157 |
+
]
|
| 158 |
|
| 159 |
# ============================================================
|
| 160 |
+
# 9. ํ๋ก๊ทธ๋จ ์คํ (์
๋ ฅ ๋ฐ๋ ๋ถ๋ถ)
|
| 161 |
# ============================================================
|
| 162 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
query = input("์ง๋ฌธ์ ์
๋ ฅํ์ธ์: ")
|
| 165 |
|
| 166 |
+
instruction = """
|
| 167 |
+
๋น์ ์ ํด๋น ํ๋ฅด์๋์ ์ฑ๊ฒฉ์ ๊ฐ์ง ์ฌํ๊ด์
๋๋ค.
|
| 168 |
+
๋ฐ๋์ 3๋ฌธ์ฅ๋ง ๋งํ์ญ์์ค.
|
| 169 |
+
๊ฐ ๋ฌธ์ฅ์ 30์ ์ด๋ด.
|
| 170 |
+
๊ท์ ์ ์ฐ์ ํ์ฌ ๋ตํ์ธ์.
|
| 171 |
+
ํ๋จ ๊ทผ๊ฑฐ ํฌํจ.
|
| 172 |
+
๋ฐ๋ณต ๊ธ์ง.
|
| 173 |
+
"""
|
| 174 |
|
| 175 |
+
for persona_text, persona_name in persona_group:
|
| 176 |
+
print("\n====================")
|
| 177 |
+
print(f"### {persona_name} ###")
|
| 178 |
+
print("====================")
|
|
|
|
| 179 |
|
| 180 |
+
stream_chat(
|
| 181 |
+
persona=persona_text,
|
| 182 |
+
instruction=instruction,
|
| 183 |
+
user_query=query
|
| 184 |
+
)
|