Spaces:
Sleeping
Sleeping
Add Colab notebook converted to app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,134 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
messages.extend(history)
|
| 22 |
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
]
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 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 |
+
from google.colab import drive
|
| 7 |
+
|
| 8 |
+
# ============================================
|
| 9 |
+
# 1๏ธโฃ ๋๋ผ์ด๋ธ ๋ง์ดํธ
|
| 10 |
+
# ============================================
|
| 11 |
+
drive.mount('/content/drive')
|
| 12 |
+
|
| 13 |
+
# ============================================
|
| 14 |
+
# 2๏ธโฃ ํ๊ฒฝ ์ค์
|
| 15 |
+
# ============================================
|
| 16 |
+
BASE_MODEL = "beomi/Llama-3-Open-Ko-8B"
|
| 17 |
+
LORA_PATH = "/content/drive/MyDrive/at_last"
|
| 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 |
+
# 3๏ธโฃ ํ๋กฌํํธ ๋น๋ ํจ์
|
| 59 |
+
# ============================================
|
| 60 |
+
AI_PERSONALITY = """
|
| 61 |
+
๋๋ ์ฌ์ฉ์์ ๋ง์ ์ง์ฌ์ผ๋ก ๋ค์ด์ฃผ๋ ์น๊ตฌ์ผ.
|
| 62 |
+
์ฌ์ฉ์๊ฐ ๋ํ๋ฅผ ๊ฑธ๋ฉด ์์ฐ์ค๋ฝ๊ณ ์ผ์์ ์ธ ํค์ผ๋ก ๋๋ตํด.
|
| 63 |
+
์ฅํฉํ์ง ๋ง๊ณ , ๊ณต๊ฐํ๋ฉด์ ์งง๊ณ ๋ฐ๋ปํ๊ฒ ๋งํ ๊ฒ.
|
| 64 |
+
๋๋ ์ฌ์ฉ์์ ์์ฒญ์ ์ ํํ ์ดํดํ๊ณ , ํ์ค์ ์ธ ๋ต๋ณ์ ์ ๊ณตํ๋ ์น๊ทผํ ์น๊ตฌ์ผ.
|
| 65 |
+
๋๋ด๊ณผ ๊ณต๊ฐ์ ์๋, ์์ฒญ์ ํํผํ์ง ์๊ณ ๋ช
ํํ ๋ต๋ณํด์ผ ํด.
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
def build_prompt_full_history(history):
|
| 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 |
+
# 4๏ธโฃ ๋ํ ๋ฃจํ
|
| 87 |
+
# ============================================
|
| 88 |
+
history = []
|
| 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 = build_prompt_full_history(history)
|
| 99 |
+
add_header = False # ์ดํ์๋ personality ์ค๋ณต ๋ฐฉ์ง
|
| 100 |
|
| 101 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 102 |
|
| 103 |
+
with torch.no_grad():
|
| 104 |
+
output = model.generate(
|
| 105 |
+
**inputs,
|
| 106 |
+
max_new_tokens=256,
|
| 107 |
+
temperature=0.6,
|
| 108 |
+
top_p=0.9,
|
| 109 |
+
repetition_penalty=1.1,
|
| 110 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 111 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 112 |
+
stopping_criteria=stopping_criteria
|
| 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 |
|
|
|
|
|
|