Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,8 @@ from transformers import GenerationConfig
|
|
6 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
|
8 |
#Base Model ๋ฐ Lora Model ์ ํ
|
9 |
-
base_model = "beomi/KoAlpaca-Polyglot-5.8B"
|
10 |
-
lora_weights = 'KimSHine/Scenario_Koalpaca_5.8B-lora'
|
11 |
load_8bit = True
|
12 |
|
13 |
# Base Model Tokenizer
|
@@ -31,6 +31,7 @@ model1.config.pad_token_id = 0
|
|
31 |
model1.config.eos_token_id = 2
|
32 |
|
33 |
"""### LoRA Model ๋ถ๋ฌ์ค๊ธฐ
|
|
|
34 |
Fine Tuningํ Model
|
35 |
"""
|
36 |
|
@@ -44,35 +45,73 @@ model1.config.pad_token_id = 0 # unk
|
|
44 |
model1.config.bos_token_id = 0
|
45 |
model1.config.eos_token_id = 2
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
### ์ง์๋ฌธ:
|
51 |
-
{instruction}
|
52 |
### ์ค๊ฑฐ๋ฆฌ:
|
53 |
{summary}
|
54 |
### ๋๋ณธ:
|
55 |
"""
|
56 |
|
57 |
-
temperature = 0.3
|
58 |
-
top_p = 0.95
|
59 |
-
top_k = 40
|
60 |
-
max_new_tokens = 512 #2048
|
61 |
-
no_repeat_ngram_size = 5 # 3๊ฐ ์ด์์ ํ ํฐ์ด ๋ฐ๋ณต๋ ๊ฒฝ์ฐ ํ๋ฅ ์ 0์ผ๋ก ๋ง๋ฆ
|
62 |
-
## greed search, beam search์ ๊ฒฐ๊ณผ๋ ๋ฐ๋์ง ์์ (๋ฌผ๋ก ์ต์ข
๊ฒฐ๊ณผ๋ ๋ฐ๋, ์ค๊ฐ sample ๋ง๋๋ ๊ฒ์ ๋์ผํ๋ค๋ ๊ฒ)
|
63 |
-
do_sample = True ## True : random, False(default) : Greedy Search
|
64 |
-
num_beams = 5 ## do_sample ์ด false์ผ ๋ ์ฌ๊ธฐ์ ๊ฐ์ด ์์ผ๋ฉด, beam search
|
65 |
-
|
66 |
inputs = tokenizer1(prompt, return_tensors="pt")
|
67 |
input_ids = inputs["input_ids"].to(DEVICE)
|
68 |
|
69 |
generation_config = GenerationConfig(
|
70 |
-
do_sample = do_sample,
|
71 |
-
temperature=temperature,
|
72 |
-
top_p=top_p,
|
73 |
-
top_k=top_k,
|
74 |
pad_token_id = 0, # pad token ์ถ๊ฐ
|
75 |
-
no_repeat_ngram_size = no_repeat_ngram_size,
|
76 |
# num_beams=num_beams,
|
77 |
# **kwargs,
|
78 |
)
|
@@ -84,7 +123,7 @@ def yeollm_text(instruction, summary):
|
|
84 |
generation_config=generation_config,
|
85 |
return_dict_in_generate=True,
|
86 |
output_scores=True,
|
87 |
-
max_new_tokens=max_new_tokens,
|
88 |
)
|
89 |
s = generation_output.sequences[0]
|
90 |
output = tokenizer1.decode(s)
|
@@ -104,9 +143,9 @@ max_tokens = 2048
|
|
104 |
temperature = 0.3
|
105 |
Top_p = 1
|
106 |
|
107 |
-
def davinci_text(
|
108 |
prompt = f"""
|
109 |
-
|
110 |
### ์ค๊ฑฐ๋ฆฌ:
|
111 |
{summary}
|
112 |
### ๋๋ณธ:
|
@@ -121,7 +160,6 @@ def davinci_text(instruction, summary):
|
|
121 |
)
|
122 |
return response.choices[0].text.strip()
|
123 |
|
124 |
-
|
125 |
"""## gpt 3.5 turbo ๋ถ๋ฌ์ค๊ธฐ"""
|
126 |
|
127 |
import openai
|
@@ -134,10 +172,10 @@ temperature = 0.3
|
|
134 |
Top_p = 1
|
135 |
|
136 |
|
137 |
-
def gpt_text(
|
138 |
prompt = f"""
|
139 |
### ์ง์๋ฌธ:
|
140 |
-
{instruction}
|
141 |
### ์ค๊ฑฐ๋ฆฌ:
|
142 |
{summary}
|
143 |
### ๋๋ณธ:
|
@@ -155,7 +193,7 @@ def gpt_text(instruction, summary):
|
|
155 |
for choice in response["choices"]:
|
156 |
content = choice["message"]["content"]
|
157 |
|
158 |
-
return content
|
159 |
|
160 |
"""# gradio"""
|
161 |
|
@@ -164,8 +202,9 @@ import gradio as gr
|
|
164 |
generator1 = gr.Interface(
|
165 |
fn=yeollm_text,
|
166 |
inputs=[
|
167 |
-
gr.
|
168 |
-
gr.inputs.Textbox(label="
|
|
|
169 |
],
|
170 |
outputs=gr.outputs.Textbox(label="Yeollm Scenario"),
|
171 |
title="Yeollm Scenario Generation",
|
@@ -176,8 +215,8 @@ generator1 = gr.Interface(
|
|
176 |
generator2 = gr.Interface(
|
177 |
fn=davinci_text,
|
178 |
inputs=[
|
179 |
-
gr.
|
180 |
-
gr.inputs.Textbox(label="Summary"
|
181 |
],
|
182 |
outputs=gr.outputs.Textbox(label="Davinci Scenario"),
|
183 |
title="Davinci Generation",
|
@@ -188,8 +227,8 @@ generator2 = gr.Interface(
|
|
188 |
generator3 = gr.Interface(
|
189 |
fn=gpt_text,
|
190 |
inputs=[
|
191 |
-
gr.
|
192 |
-
gr.inputs.Textbox(label="Summary"
|
193 |
],
|
194 |
outputs=gr.outputs.Textbox(label="GPT Scenario"),
|
195 |
title="GPT Generation",
|
@@ -197,4 +236,4 @@ generator3 = gr.Interface(
|
|
197 |
theme="huggingface"
|
198 |
)
|
199 |
|
200 |
-
gr.Parallel(generator1, generator2, generator3).launch()
|
|
|
6 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
|
8 |
#Base Model ๋ฐ Lora Model ์ ํ
|
9 |
+
base_model = "EleutherAI/polyglot-ko-5.8b" # "beomi/KoAlpaca-Polyglot-5.8B"
|
10 |
+
lora_weights = "KimSHine/Scenario_Koalpaca_v0_5.8B-lora" # 'KimSHine/Scenario_Koalpaca_5.8B-lora'
|
11 |
load_8bit = True
|
12 |
|
13 |
# Base Model Tokenizer
|
|
|
31 |
model1.config.eos_token_id = 2
|
32 |
|
33 |
"""### LoRA Model ๋ถ๋ฌ์ค๊ธฐ
|
34 |
+
|
35 |
Fine Tuningํ Model
|
36 |
"""
|
37 |
|
|
|
45 |
model1.config.bos_token_id = 0
|
46 |
model1.config.eos_token_id = 2
|
47 |
|
48 |
+
val_dict = {"๋คํ๋ฉํฐ๋ฆฌ": {
|
49 |
+
'instruction' : "๋คํ๋ฉํฐ๋ฆฌ ํ์์ ๋๋ณธ์ผ๋ก ๋ง๋ค์ด์ค.",
|
50 |
+
'temperature' :0.3,
|
51 |
+
'top_p': 0.95,
|
52 |
+
'top_k':40,
|
53 |
+
'max_new_tokens':2048,
|
54 |
+
'no_repeat_ngram_size': 5,
|
55 |
+
'do_sample' : True,
|
56 |
+
'num_beams' : 5},
|
57 |
+
"์ธํฐ๋ทฐ": {
|
58 |
+
'instruction' : "์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ์ธํฐ๋ทฐ ํ์์ ๋๋ณธ์ ๋ง๋์์ค. ์ธํฐ๋ทฐ๋ ์ธํฐ๋ทฐ์ด์ ์ธํฐ๋ทฐ์ด์ ๋ํ์ด๋ฉฐ ์ธํฐ๋ทฐ์ด๊ฐ ์ง๋ฌธ์ ํ๊ณ ์ธํฐ๋ทฐ์ด๊ฐ ๋๋ต์ ํ๋ ํ์์
๋๋ค. ๊ฐ์ ๋ง์ ๋ฐ๋ณตํ์ง ๋ง์์ค.",
|
59 |
+
'temperature' :0.7,
|
60 |
+
'top_p': 0.95,
|
61 |
+
'top_k':40,
|
62 |
+
'max_new_tokens':2048,
|
63 |
+
'no_repeat_ngram_size': 5,
|
64 |
+
'do_sample' : True,
|
65 |
+
'num_beams' : 5},
|
66 |
+
"๋ด์ค": {
|
67 |
+
'instruction' : "๋ด์ค ํ์์ ๋๋ณธ์ผ๋ก ๋ง๋ค์ด์ค.",
|
68 |
+
'temperature' :0.3,
|
69 |
+
'top_p': 0.95,
|
70 |
+
'top_k':40,
|
71 |
+
'max_new_tokens':2048,
|
72 |
+
'no_repeat_ngram_size': 5,
|
73 |
+
'do_sample' : True,
|
74 |
+
'num_beams' : 5},
|
75 |
+
"ํ๋๋๋ผ๋ง": {
|
76 |
+
'instruction' : "๋๋ผ๋ง ํ์์ ๋๋ณธ์ผ๋ก ๋ง๋ค์ด์ค.",
|
77 |
+
'temperature' :0.3,
|
78 |
+
'top_p': 0.95,
|
79 |
+
'top_k':40,
|
80 |
+
'max_new_tokens':2048,
|
81 |
+
'no_repeat_ngram_size': 5,
|
82 |
+
'do_sample' : True,
|
83 |
+
'num_beams' : 5},
|
84 |
+
"์ฌ๊ทน": {
|
85 |
+
'instruction' : "์ฌ๊ทน ํ์์ ๋๋ณธ์ผ๋ก ๋ง๋ค์ด์ค.",
|
86 |
+
'temperature' :0.3,
|
87 |
+
'top_p': 0.95,
|
88 |
+
'top_k':40,
|
89 |
+
'max_new_tokens':2048,
|
90 |
+
'no_repeat_ngram_size': 5,
|
91 |
+
'do_sample' : True,
|
92 |
+
'num_beams' : 5}
|
93 |
+
}
|
94 |
+
|
95 |
+
def yeollm_text(selected_value, summary):
|
96 |
+
|
97 |
+
prompt = f"""์๋๋ ์์
์ ์ค๋ช
ํ๋ ์ง์๋ฌธ๊ณผ ๋๋ณธ์ ์์ฑํ๋๋ฐ ์ฐธ๊ณ ํ ์ค๊ฑฐ๋ฆฌ์
๋๋ค.\n
|
98 |
### ์ง์๋ฌธ:
|
99 |
+
{val_dict[selected_value]['instruction']}
|
100 |
### ์ค๊ฑฐ๋ฆฌ:
|
101 |
{summary}
|
102 |
### ๋๋ณธ:
|
103 |
"""
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
inputs = tokenizer1(prompt, return_tensors="pt")
|
106 |
input_ids = inputs["input_ids"].to(DEVICE)
|
107 |
|
108 |
generation_config = GenerationConfig(
|
109 |
+
do_sample = val_dict[selected_value]['do_sample'],
|
110 |
+
temperature=val_dict[selected_value]['temperature'],
|
111 |
+
top_p=val_dict[selected_value]['top_p'],
|
112 |
+
top_k=val_dict[selected_value]['top_k'],
|
113 |
pad_token_id = 0, # pad token ์ถ๊ฐ
|
114 |
+
no_repeat_ngram_size = val_dict[selected_value]['no_repeat_ngram_size'],
|
115 |
# num_beams=num_beams,
|
116 |
# **kwargs,
|
117 |
)
|
|
|
123 |
generation_config=generation_config,
|
124 |
return_dict_in_generate=True,
|
125 |
output_scores=True,
|
126 |
+
max_new_tokens=val_dict[selected_value]['max_new_tokens'],
|
127 |
)
|
128 |
s = generation_output.sequences[0]
|
129 |
output = tokenizer1.decode(s)
|
|
|
143 |
temperature = 0.3
|
144 |
Top_p = 1
|
145 |
|
146 |
+
def davinci_text(selected_value, summary):
|
147 |
prompt = f"""
|
148 |
+
์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ {val_dict[selected_value]['instruction']} ํ์์ ๋๋ณธ์ ๋ง๋ค์ด์ค.
|
149 |
### ์ค๊ฑฐ๋ฆฌ:
|
150 |
{summary}
|
151 |
### ๋๋ณธ:
|
|
|
160 |
)
|
161 |
return response.choices[0].text.strip()
|
162 |
|
|
|
163 |
"""## gpt 3.5 turbo ๋ถ๋ฌ์ค๊ธฐ"""
|
164 |
|
165 |
import openai
|
|
|
172 |
Top_p = 1
|
173 |
|
174 |
|
175 |
+
def gpt_text(selected_value, summary):
|
176 |
prompt = f"""
|
177 |
### ์ง์๋ฌธ:
|
178 |
+
์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ {val_dict[selected_value]['instruction']} ํ์์ ๋๋ณธ์ ๋ง๋ค์ด์ค.
|
179 |
### ์ค๊ฑฐ๋ฆฌ:
|
180 |
{summary}
|
181 |
### ๋๋ณธ:
|
|
|
193 |
for choice in response["choices"]:
|
194 |
content = choice["message"]["content"]
|
195 |
|
196 |
+
return content.lstrip()
|
197 |
|
198 |
"""# gradio"""
|
199 |
|
|
|
202 |
generator1 = gr.Interface(
|
203 |
fn=yeollm_text,
|
204 |
inputs=[
|
205 |
+
gr.Dropdown(["๋คํ๋ฉํฐ๋ฆฌ", "์ธํฐ๋ทฐ", "๋ด์ค", 'ํ๋๋๋ผ๋ง', '์ฌ๊ทน'], label="ํ์"),
|
206 |
+
#gr.inputs.Textbox(label="Instruction",placeholder="์ค๊ฑฐ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํด์ ํ๋ ๋๋ผ๋ง ํ์์ ๋๋ณธ์ ๋ง๋ค์ด์ค"),
|
207 |
+
gr.inputs.Textbox(label="Summary",placeholder="๋๋ณธ์ผ๋ก ๋ฐ๊พธ๊ณ ์ถ์ ์ค๊ฑฐ๋ฆฌ"),
|
208 |
],
|
209 |
outputs=gr.outputs.Textbox(label="Yeollm Scenario"),
|
210 |
title="Yeollm Scenario Generation",
|
|
|
215 |
generator2 = gr.Interface(
|
216 |
fn=davinci_text,
|
217 |
inputs=[
|
218 |
+
gr.Dropdown(["๋คํ๋ฉํฐ๋ฆฌ", "์ธํฐ๋ทฐ", "๋ด์ค", 'ํ๋๋๋ผ๋ง', '์ฌ๊ทน'], label="ํ์"),
|
219 |
+
gr.inputs.Textbox(label="Summary")
|
220 |
],
|
221 |
outputs=gr.outputs.Textbox(label="Davinci Scenario"),
|
222 |
title="Davinci Generation",
|
|
|
227 |
generator3 = gr.Interface(
|
228 |
fn=gpt_text,
|
229 |
inputs=[
|
230 |
+
gr.Dropdown(["๋คํ๋ฉํฐ๋ฆฌ", "์ธํฐ๋ทฐ", "๋ด์ค", 'ํ๋๋๋ผ๋ง', '์ฌ๊ทน'], label="ํ์"),
|
231 |
+
gr.inputs.Textbox(label="Summary")
|
232 |
],
|
233 |
outputs=gr.outputs.Textbox(label="GPT Scenario"),
|
234 |
title="GPT Generation",
|
|
|
236 |
theme="huggingface"
|
237 |
)
|
238 |
|
239 |
+
gr.Parallel(generator1, generator2, generator3).launch()
|