File size: 8,322 Bytes
2e869d4 4fac302 4ba2d78 2e869d4 4fac302 2e869d4 4fac302 21d937f 4fac302 21d937f 4fac302 21d937f 4fac302 21d937f 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 ab3c8c5 2e869d4 4fac302 2e869d4 4fac302 2e869d4 ab3c8c5 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 2e869d4 4fac302 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
from peft import PeftModel, PeftConfig
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from transformers import GenerationConfig
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
#Base Model ๋ฐ Lora Model ์ ํ
base_model = "EleutherAI/polyglot-ko-5.8b" # "beomi/KoAlpaca-Polyglot-5.8B"
lora_weights = "KimSHine/YEOLLM_5.8B-lora_v3" # 'KimSHine/Scenario_Koalpaca_5.8B-lora'
load_8bit = True
# Base Model Tokenizer
tokenizer1 = AutoTokenizer.from_pretrained(base_model)
## token id ์ถ๊ฐ
tokenizer1.pad_token_id = 0
tokenizer1.eos_token_id = 2
"""### Base Model ๋ถ๋ฌ์ค๊ธฐ"""
# KoAlpaca-polyglot-5.8B
model1 = AutoModelForCausalLM.from_pretrained(
base_model,
load_in_8bit=load_8bit,
torch_dtype=torch.float16,
device_map="auto",
)
model1.config.pad_token_id = 0
model1.config.eos_token_id = 2
"""### LoRA Model ๋ถ๋ฌ์ค๊ธฐ
Fine Tuningํ Model
"""
model1 = PeftModel.from_pretrained(
model1,
lora_weights,
torch_dtype=torch.float16,
)
model1.config.pad_token_id = 0 # unk
model1.config.bos_token_id = 0
model1.config.eos_token_id = 2
val_dict = {"๋คํ๋ฉํฐ๋ฆฌ": {
'instruction' : "์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ๋คํ๋ฉํฐ๋ฆฌ ํ์์ ๋๋ณธ์ ๋ง๋์์ค. ๋คํ๋ฉํฐ๋ฆฌ๋ ์ง์งํ ๋ํ์
๋๋ค. ๊ฐ์ ๋ง์ ๋ฐ๋ณตํ์ง ๋ง์ธ์.",
'temperature' :0.65,
'top_p': 0.95,
'top_k':40,
'max_new_tokens':2048,
'no_repeat_ngram_size': 5,
'do_sample' : True,
'num_beams' : 5},
"์ธํฐ๋ทฐ": {
'instruction' : "์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ์ธํฐ๋ทฐ ํ์์ ๋๋ณธ์ ๋ง๋์์ค. ์ธํฐ๋ทฐ๋ ์ธํฐ๋ทฐ์ด์ ์ธํฐ๋ทฐ์ด์ ๋ํ์ด๋ฉฐ ์ธํฐ๋ทฐ์ด๊ฐ ์ง๋ฌธ์ ํ๊ณ ์ธํฐ๋ทฐ์ด๊ฐ ๋๋ต์ ํ๋ ํ์์
๋๋ค. ๊ฐ์ ๋ง์ ๋ฐ๋ณตํ์ง ๋ง์์ค.",
'temperature' :0.7,
'top_p': 0.95,
'top_k':40,
'max_new_tokens':2048,
'no_repeat_ngram_size': 5,
'do_sample' : True,
'num_beams' : 5},
"๋ด์ค": {
'instruction' : " ์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ๋ด์ค ํ์์ผ๋ก ๋๋ณธ์ ๋ง๋์์ค. ๋ด์ค ํ์์ ๋๋ณธ์ ์ต์ปค๊ฐ ์ค๊ฑฐ๋ฆฌ๋ฅผ ๋ฐํ์ผ๋ก ์ต๋ํ ์ฌ์ค์ ์ธ ๋ด์ฉ์ ์๋๊ฐ์๊ฒ ์ค๋ช
ํ๋ ๋๋ณธ์
๋๋ค. ๋ด์ค๋ ์ต์ปค๊ฐ ์ธ์ฌ๋ง๊ณผ ๋ณธ๋ก , ๋ง์ง๋ง ์ธ์ฌ๋ง๋ก ๊ตฌ์ฑ๋์ด ์๋ค. ๊ฐ์ ๋ง์ ๋ฐ๋ณตํ์ง ๋ง์์ค.",
'temperature' :0.7,
'top_p': 0.95,
'top_k':40,
'max_new_tokens':2048,
'no_repeat_ngram_size': 5,
'do_sample' : True,
'num_beams' : 5},
"ํ๋๋๋ผ๋ง": {
'instruction' : "์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ๋๋ง๋ผ ํ์์ผ๋ก ๋๋ณธ์ ๋ง๋์์ค.",
'temperature' :0.8,
'top_p': 0.95,
'top_k':40,
'max_new_tokens':2048,
'no_repeat_ngram_size': 5,
'do_sample' : True,
'num_beams' : 5},
"์ฌ๊ทน": {
'instruction' : "์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ๋๋ผ๋ง ํ์์ผ๋ก ๋๋ณธ์ ๋ง๋์์ค.",
'temperature' :0.8,
'top_p': 0.95,
'top_k':40,
'max_new_tokens':2048,
'no_repeat_ngram_size': 5,
'do_sample' : True,
'num_beams' : 5}
}
def yeollm_text(selected_value, summary):
prompt = f"""์๋๋ ์์
์ ์ค๋ช
ํ๋ ์ง์๋ฌธ๊ณผ ๋๋ณธ์ ์์ฑํ๋๋ฐ ์ฐธ๊ณ ํ ์ค๊ฑฐ๋ฆฌ์
๋๋ค.\n
### ์ง์๋ฌธ:
{val_dict[selected_value]['instruction']}
### ์ค๊ฑฐ๋ฆฌ:
{summary}
### ๋๋ณธ:
"""
inputs = tokenizer1(prompt, return_tensors="pt")
input_ids = inputs["input_ids"].to(DEVICE)
generation_config = GenerationConfig(
do_sample = val_dict[selected_value]['do_sample'],
temperature=val_dict[selected_value]['temperature'],
top_p=val_dict[selected_value]['top_p'],
top_k=val_dict[selected_value]['top_k'],
pad_token_id = 0, # pad token ์ถ๊ฐ
no_repeat_ngram_size = val_dict[selected_value]['no_repeat_ngram_size'],
# num_beams=num_beams,
# **kwargs,
)
# Generate text
with torch.no_grad():
generation_output = model1.generate(
input_ids=input_ids,
generation_config=generation_config,
return_dict_in_generate=True,
output_scores=True,
max_new_tokens=val_dict[selected_value]['max_new_tokens'],
)
s = generation_output.sequences[0]
output = tokenizer1.decode(s)
output = output.split('### ๋๋ณธ:')[1]
if output[-13:] == '<|endoftext|>':
output = output[:-13]
return output.lstrip()
"""## text davinci 003 ๋ถ๋ฌ์ค๊ธฐ"""
import openai
OPENAI_API_KEY = ''
openai.api_key = OPENAI_API_KEY
model2 = 'text-davinci-003' #'gpt-3.5-turbo'
max_tokens = 2048
temperature = 0.3
Top_p = 1
def davinci_text(selected_value, summary):
prompt = f"""
์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ {val_dict[selected_value]['instruction']} ํ์์ ๋๋ณธ์ ๋ง๋ค์ด์ค.
### ์ค๊ฑฐ๋ฆฌ:
{summary}
### ๋๋ณธ:
"""
response = openai.Completion.create(
engine = model2,
prompt = prompt,
temperature = temperature,
max_tokens = max_tokens,
n=1,
)
return response.choices[0].text.strip()
"""## gpt 3.5 turbo ๋ถ๋ฌ์ค๊ธฐ"""
import openai
OPENAI_API_KEY = ''
openai.api_key = OPENAI_API_KEY
model4 = 'gpt-3.5-turbo' #'gpt-3.5-turbo'
max_tokens = 2048
temperature = 0.3
Top_p = 1
def gpt_text(selected_value, summary):
prompt = f"""
### ์ง์๋ฌธ:
์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ {val_dict[selected_value]['instruction']} ํ์์ ๋๋ณธ์ ๋ง๋ค์ด์ค.
### ์ค๊ฑฐ๋ฆฌ:
{summary}
### ๋๋ณธ:
"""
response = openai.ChatCompletion.create(
model = model4,
messages=[
{"role": "system", "content": "์๋๋ ์์
์ ์ค๋ช
ํ๋ ์ง์๋ฌธ๊ณผ ๋๋ณธ์ ์์ฑํ๋๋ฐ ์ฐธ๊ณ ํ ์ค๊ฑฐ๋ฆฌ์ ์ง์ ์ด๋ฃจ๋ ์์ ์
๋๋ค. ์์ฒญ์ ์ ์ ํ ๋ง์กฑํ๋ ๋๋ณธ์ ์์ฑํ์ธ์."},
{"role": "user", "content": prompt},
],
temperature = temperature,
max_tokens = max_tokens,
n=1,
)
for choice in response["choices"]:
content = choice["message"]["content"]
return content.lstrip()
"""# gradio"""
import gradio as gr
generator1 = gr.Interface(
fn=yeollm_text,
inputs=[
gr.Dropdown(["๋คํ๋ฉํฐ๋ฆฌ", "์ธํฐ๋ทฐ", "๋ด์ค", 'ํ๋๋๋ผ๋ง', '์ฌ๊ทน'], label="ํ์"),
#gr.inputs.Textbox(label="Instruction",placeholder="์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ํ๋ ๋๋ผ๋ง ํ์์ ๋๋ณธ์ ๋ง๋ค์ด์ค"),
gr.inputs.Textbox(label="Summary",placeholder="๋๋ณธ์ผ๋ก ๋ฐ๊พธ๊ณ ์ถ์ ์ค๊ฑฐ๋ฆฌ"),
],
outputs=gr.outputs.Textbox(label="Yeollm Scenario"),
title="Yeollm Scenario Generation",
description="Generate scenarios using the Yeollm model.",
theme="huggingface"
)
generator2 = gr.Interface(
fn=davinci_text,
inputs=[
gr.Dropdown(["๋คํ๋ฉํฐ๋ฆฌ", "์ธํฐ๋ทฐ", "๋ด์ค", 'ํ๋๋๋ผ๋ง', '์ฌ๊ทน'], label="ํ์"),
gr.inputs.Textbox(label="Summary")
],
outputs=gr.outputs.Textbox(label="Davinci Scenario"),
title="Davinci Generation",
description="Generate scenarios using the Davinci model.",
theme="huggingface"
)
generator3 = gr.Interface(
fn=gpt_text,
inputs=[
gr.Dropdown(["๋คํ๋ฉํฐ๋ฆฌ", "์ธํฐ๋ทฐ", "๋ด์ค", 'ํ๋๋๋ผ๋ง', '์ฌ๊ทน'], label="ํ์"),
gr.inputs.Textbox(label="Summary")
],
outputs=gr.outputs.Textbox(label="GPT Scenario"),
title="GPT Generation",
description="Generate scenarios using the GPT model.",
theme="huggingface"
)
gr.Parallel(generator1, generator2, generator3).launch()
|