Spaces:
Sleeping
Sleeping
Upload 9 files (#1)
Browse files- Upload 9 files (275b713f4ba3e07d08053d8448397e16e6c6a07b)
- app.py +32 -95
- lora/README.md +3 -0
- lora/adapter_config.json +3 -0
- lora/adapter_model.safetensors +3 -0
- lora/chat_template.jinja +3 -0
- lora/special_tokens_map.json +3 -0
- lora/tokenizer.json +3 -0
- lora/tokenizer_config.json +3 -0
- requirements.txt +6 -0
app.py
CHANGED
|
@@ -1,134 +1,71 @@
|
|
| 1 |
-
!pip install -q -U transformers peft accelerate bitsandbytes
|
| 2 |
-
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from peft import PeftModel
|
| 6 |
-
|
| 7 |
|
| 8 |
# ============================================
|
| 9 |
-
#
|
| 10 |
# ============================================
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# ============================================
|
| 14 |
-
#
|
| 15 |
# ============================================
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
print("๐ ๋ชจ๋ธ ๋ก๋ ์ค...")
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
BASE_MODEL,
|
| 22 |
torch_dtype=torch.bfloat16,
|
| 23 |
device_map="auto",
|
| 24 |
trust_remote_code=True
|
| 25 |
)
|
| 26 |
-
|
| 27 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 28 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 29 |
-
|
| 30 |
-
print("๐ LoRA ๋ณํฉ ์ค...")
|
| 31 |
model = PeftModel.from_pretrained(model, LORA_PATH, is_local=True)
|
| 32 |
-
|
| 33 |
-
# โ
<|eot_id|> ํ ํฐ์ EOS๋ก ์ง์
|
| 34 |
model.config.eos_token_id = tokenizer.eos_token_id
|
| 35 |
model.config.pad_token_id = tokenizer.pad_token_id
|
| 36 |
|
| 37 |
-
print("โ
๋ชจ๋ธ + LoRA ์ค๋น ์๋ฃ!")
|
| 38 |
-
|
| 39 |
-
from transformers import StoppingCriteria, StoppingCriteriaList
|
| 40 |
-
|
| 41 |
-
class StopOnTokens(StoppingCriteria):
|
| 42 |
-
def __init__(self, stop_ids):
|
| 43 |
-
self.stop_ids = stop_ids
|
| 44 |
-
|
| 45 |
-
def __call__(self, input_ids, scores, **kwargs):
|
| 46 |
-
last_token = input_ids[0, -1].item()
|
| 47 |
-
return last_token in self.stop_ids
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# โ
์ข
๋ฃ ํ ํฐ ํ๋ณด๋ฅผ ๋ชจ๋ ๋ฑ๋ก
|
| 51 |
-
stop_words = ["<|eot|>", "</s>", "<|end_of_text|>"]
|
| 52 |
-
stop_ids = [tokenizer.convert_tokens_to_ids(w) for w in stop_words if tokenizer.convert_tokens_to_ids(w) is not None]
|
| 53 |
-
stopping_criteria = StoppingCriteriaList([StopOnTokens(stop_ids)])
|
| 54 |
-
|
| 55 |
-
stopping_criteria = StoppingCriteriaList([StopOnTokens(stop_ids)])
|
| 56 |
-
|
| 57 |
# ============================================
|
| 58 |
-
#
|
| 59 |
# ============================================
|
| 60 |
AI_PERSONALITY = """
|
| 61 |
๋๋ ์ฌ์ฉ์์ ๋ง์ ์ง์ฌ์ผ๋ก ๋ค์ด์ฃผ๋ ์น๊ตฌ์ผ.
|
| 62 |
์ฌ์ฉ์๊ฐ ๋ํ๋ฅผ ๊ฑธ๋ฉด ์์ฐ์ค๋ฝ๊ณ ์ผ์์ ์ธ ํค์ผ๋ก ๋๋ตํด.
|
| 63 |
์ฅํฉํ์ง ๋ง๊ณ , ๊ณต๊ฐํ๋ฉด์ ์งง๊ณ ๋ฐ๋ปํ๊ฒ ๋งํ ๊ฒ.
|
| 64 |
-
๋๋ ์ฌ์ฉ์์ ์์ฒญ์ ์ ํํ ์ดํดํ๊ณ , ํ์ค์ ์ธ ๋ต๋ณ์ ์ ๊ณตํ๋ ์น๊ทผํ ์น๊ตฌ์ผ.
|
| 65 |
-
๋๋ด๊ณผ ๊ณต๊ฐ์ ์๋, ์์ฒญ์ ํํผํ์ง ์๊ณ ๋ช
ํํ ๋ต๋ณํด์ผ ํด.
|
| 66 |
"""
|
| 67 |
|
| 68 |
-
|
| 69 |
-
"""
|
| 70 |
-
- history๋ user/assistant ๋ชจ๋ ๋ํ ํฌํจ
|
| 71 |
-
- ๋ง์ง๋ง user ๋ฐํ๋ง generate ๋์
|
| 72 |
-
"""
|
| 73 |
-
prompt = "<|begin_of_text|>\n" + AI_PERSONALITY.strip() + "\n\n"
|
| 74 |
-
for turn in history:
|
| 75 |
-
role = turn["role"]
|
| 76 |
-
content = turn["content"].strip()
|
| 77 |
-
prompt += f"<|start_header_id|>{role}<|end_header_id|>\n{content}<|eot|>\n"
|
| 78 |
-
|
| 79 |
-
# ๋ง์ง๋ง user ์ดํ์ assistant placeholder ์ถ๊ฐ
|
| 80 |
-
prompt += "<|start_header_id|>assistant<|end_header_id|>\n"
|
| 81 |
-
return prompt
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
# ============================================
|
| 86 |
-
#
|
| 87 |
# ============================================
|
| 88 |
-
|
| 89 |
-
add_header = True # ์ฒซ ํด๋ง personality ํฌํจ
|
| 90 |
-
|
| 91 |
-
while True:
|
| 92 |
-
user_input = input("๐ค ์ฌ์ฉ์: ").strip()
|
| 93 |
-
if user_input.lower() in ["์ข
๋ฃ", "exit", "quit"]:
|
| 94 |
-
print("๐ ๋ํ ์ข
๋ฃ!")
|
| 95 |
-
break
|
| 96 |
-
|
| 97 |
history.append({"role": "user", "content": user_input})
|
| 98 |
-
prompt =
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 102 |
-
|
| 103 |
with torch.no_grad():
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
response_full = tokenizer.decode(
|
| 116 |
-
output[0][inputs["input_ids"].shape[1]:],
|
| 117 |
-
skip_special_tokens=True
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
response = response_full.split("<|eot|>")[0].strip()
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# <|eot_id|> ๊ธฐ์ค์ผ๋ก ์๋ฅด๊ธฐ
|
| 124 |
-
if "<|eot_id|>" in response_full:
|
| 125 |
-
response = response_full.split("<|eot_id|>")[0].strip()
|
| 126 |
-
else:
|
| 127 |
-
response = response_full.strip()
|
| 128 |
-
|
| 129 |
-
print(f"๐ค AI: {response}\n")
|
| 130 |
-
|
| 131 |
history.append({"role": "assistant", "content": response})
|
| 132 |
if len(history) > 10:
|
| 133 |
-
history = history[-10:]
|
|
|
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
from peft import PeftModel
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
# ============================================
|
| 7 |
+
# ๋ชจ๋ธ + LoRA ๊ฒฝ๋ก
|
| 8 |
# ============================================
|
| 9 |
+
BASE_MODEL = "beomi/Llama-3-Open-Ko-8B"
|
| 10 |
+
LORA_PATH = "./lora" # Space repo์ lora ํด๋ ์
๋ก๋
|
| 11 |
|
| 12 |
# ============================================
|
| 13 |
+
# ํ ํฌ๋์ด์ ๋ฐ ๋ชจ๋ธ ๋ก๋
|
| 14 |
# ============================================
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 16 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 17 |
|
|
|
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
BASE_MODEL,
|
| 20 |
torch_dtype=torch.bfloat16,
|
| 21 |
device_map="auto",
|
| 22 |
trust_remote_code=True
|
| 23 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
model = PeftModel.from_pretrained(model, LORA_PATH, is_local=True)
|
|
|
|
|
|
|
| 25 |
model.config.eos_token_id = tokenizer.eos_token_id
|
| 26 |
model.config.pad_token_id = tokenizer.pad_token_id
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# ============================================
|
| 29 |
+
# AI ์ฑ๊ฒฉ ์ค์
|
| 30 |
# ============================================
|
| 31 |
AI_PERSONALITY = """
|
| 32 |
๋๋ ์ฌ์ฉ์์ ๋ง์ ์ง์ฌ์ผ๋ก ๋ค์ด์ฃผ๋ ์น๊ตฌ์ผ.
|
| 33 |
์ฌ์ฉ์๊ฐ ๋ํ๋ฅผ ๊ฑธ๋ฉด ์์ฐ์ค๋ฝ๊ณ ์ผ์์ ์ธ ํค์ผ๋ก ๋๋ตํด.
|
| 34 |
์ฅํฉํ์ง ๋ง๊ณ , ๊ณต๊ฐํ๋ฉด์ ์งง๊ณ ๋ฐ๋ปํ๊ฒ ๋งํ ๊ฒ.
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
|
| 37 |
+
history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# ============================================
|
| 40 |
+
# ๋ํ ํจ์
|
| 41 |
# ============================================
|
| 42 |
+
def chat(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
history.append({"role": "user", "content": user_input})
|
| 44 |
+
prompt = "<|begin_of_text|>\n" + AI_PERSONALITY.strip() + "\n\n"
|
| 45 |
+
for turn in history:
|
| 46 |
+
prompt += f"<|start_header_id|>{turn['role']}<|end_header_id|>\n{turn['content']}<|eot|>\n"
|
| 47 |
+
prompt += "<|start_header_id|>assistant<|end_header_id|>\n"
|
| 48 |
|
| 49 |
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
|
|
|
| 50 |
with torch.no_grad():
|
| 51 |
+
output = model.generate(
|
| 52 |
+
**inputs,
|
| 53 |
+
max_new_tokens=256,
|
| 54 |
+
temperature=0.6,
|
| 55 |
+
top_p=0.9,
|
| 56 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 57 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
response_full = tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
response = response_full.split("<|eot|>")[0].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
history.append({"role": "assistant", "content": response})
|
| 63 |
if len(history) > 10:
|
| 64 |
+
history[:] = history[-10:] # ์ต๊ทผ 10ํด๋ง ์ ์ง
|
| 65 |
+
return response
|
| 66 |
|
| 67 |
+
# ============================================
|
| 68 |
+
# Gradio ์ธํฐํ์ด์ค ์คํ
|
| 69 |
+
# ============================================
|
| 70 |
+
iface = gr.Interface(fn=chat, inputs="text", outputs="text")
|
| 71 |
+
iface.launch()
|
lora/README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ea1662cd8eeef0905f555018d524a759a6b55de446b34bf87fd760b2c71fdb0b
|
| 3 |
+
size 1513
|
lora/adapter_config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:798f804077d56c53d7c16fb297db7352ab4e19fee933c5aa02ad409cc63eb15a
|
| 3 |
+
size 859
|
lora/adapter_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c51fefb4ab1859d25ab9378941efd1b63ecbd7cd9a7f947fc9715a54c7fa2083
|
| 3 |
+
size 54543184
|
lora/chat_template.jinja
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba03a121d097859c7b5b9cd03af99aafe95275210d2876f642ad9929a150f122
|
| 3 |
+
size 389
|
lora/special_tokens_map.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:849070cae53bd45439e64ce5b1ddd650a66081b1bd47895c5a58939a05055579
|
| 3 |
+
size 335
|
lora/tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c5cf44023714fb39b05e71e425f8d7b92805ff73f7988b083b8c87f0bf87393
|
| 3 |
+
size 17209961
|
lora/tokenizer_config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c3b1e945bb39b585d9fd6a12b21aec73e8545eae873e8968cb265e1e3bf9074
|
| 3 |
+
size 50630
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
peft
|
| 4 |
+
accelerate
|
| 5 |
+
bitsandbytes
|
| 6 |
+
gradio
|