|
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 = "EleutherAI/polyglot-ko-5.8b" |
|
lora_weights = "KimSHine/YEOLLM_5.8B-lora_v3" |
|
load_8bit = True |
|
|
|
|
|
tokenizer1 = AutoTokenizer.from_pretrained(base_model) |
|
|
|
tokenizer1.pad_token_id = 0 |
|
tokenizer1.eos_token_id = 2 |
|
|
|
"""### Base Model ๋ถ๋ฌ์ค๊ธฐ""" |
|
|
|
|
|
|
|
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 |
|
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, |
|
no_repeat_ngram_size = val_dict[selected_value]['no_repeat_ngram_size'], |
|
|
|
|
|
) |
|
|
|
|
|
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' |
|
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' |
|
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="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() |
|
|