skang187 commited on
Commit
2e869d4
โ€ข
1 Parent(s): 111b839

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -0
app.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from peft import PeftModel, PeftConfig
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+ from transformers import GenerationConfig
5
+
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
14
+ tokenizer1 = AutoTokenizer.from_pretrained(base_model)
15
+ ## token id ์ถ”๊ฐ€
16
+ tokenizer1.pad_token_id = 0
17
+ tokenizer1.eos_token_id = 2
18
+
19
+ """### Base Model ๋ถˆ๋Ÿฌ์˜ค๊ธฐ"""
20
+
21
+ # KoAlpaca-polyglot-5.8B
22
+
23
+ model1 = AutoModelForCausalLM.from_pretrained(
24
+ base_model,
25
+ load_in_8bit=load_8bit,
26
+ torch_dtype=torch.float16,
27
+ device_map="auto",
28
+ )
29
+
30
+ model1.config.pad_token_id = 0
31
+ model1.config.eos_token_id = 2
32
+
33
+ """### LoRA Model ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
34
+ Fine Tuningํ•œ Model
35
+ """
36
+
37
+ model1 = PeftModel.from_pretrained(
38
+ model1,
39
+ lora_weights,
40
+ torch_dtype=torch.float16,
41
+ )
42
+
43
+ model1.config.pad_token_id = 0 # unk
44
+ model1.config.bos_token_id = 0
45
+ model1.config.eos_token_id = 2
46
+
47
+ def yeollm_text(instruction, summary):
48
+
49
+ prompt = f"""์•„๋ž˜๋Š” ์ž‘์—…์„ ์„ค๋ช…ํ•˜๋Š” ์ง€์‹œ๋ฌธ๊ณผ ๋Œ€๋ณธ์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์ฐธ๊ณ ํ•  ์ค„๊ฑฐ๋ฆฌ์™€ ์ง์„ ์ด๋ฃจ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์š”์ฒญ์„ ์ ์ ˆํžˆ ๋งŒ์กฑํ•˜๋Š” ๋Œ€๋ณธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
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
+ )
79
+
80
+ # Generate text
81
+ with torch.no_grad():
82
+ generation_output = model1.generate(
83
+ input_ids=input_ids,
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)
91
+ output = output.split('### ๋Œ€๋ณธ:')[1]
92
+ if output[-13:] == '<|endoftext|>':
93
+ output = output[:-13]
94
+ return output.lstrip()
95
+
96
+ """## text davinci 003 ๋ถˆ๋Ÿฌ์˜ค๊ธฐ"""
97
+
98
+ import openai
99
+ OPENAI_API_KEY = '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'
100
+ openai.api_key = OPENAI_API_KEY
101
+
102
+ model2 = 'text-davinci-003' #'gpt-3.5-turbo'
103
+ max_tokens = 2048
104
+ temperature = 0.3
105
+ Top_p = 1
106
+
107
+ def davinci_text(instruction, summary):
108
+ prompt = f"""
109
+ ์•„๋ž˜์˜ ์ค„๊ฑฐ๋ฆฌ๋ฅผ ๋ณด๊ณ  {instruction}
110
+ ### ์ค„๊ฑฐ๋ฆฌ:
111
+ {summary}
112
+ ### ๋Œ€๋ณธ:
113
+ """
114
+
115
+ response = openai.Completion.create(
116
+ engine = model2,
117
+ prompt = prompt,
118
+ temperature = temperature,
119
+ max_tokens = max_tokens,
120
+ n=1,
121
+ )
122
+ return response.choices[0].text.strip()
123
+
124
+
125
+ """## gpt 3.5 turbo ๋ถˆ๋Ÿฌ์˜ค๊ธฐ"""
126
+
127
+ import openai
128
+ OPENAI_API_KEY = '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'
129
+ openai.api_key = OPENAI_API_KEY
130
+
131
+ model4 = 'gpt-3.5-turbo' #'gpt-3.5-turbo'
132
+ max_tokens = 2048
133
+ temperature = 0.3
134
+ Top_p = 1
135
+
136
+
137
+ def gpt_text(instruction, summary):
138
+ prompt = f"""
139
+ ### ์ง€์‹œ๋ฌธ:
140
+ {instruction}
141
+ ### ์ค„๊ฑฐ๋ฆฌ:
142
+ {summary}
143
+ ### ๋Œ€๋ณธ:
144
+ """
145
+ response = openai.ChatCompletion.create(
146
+ model = model4,
147
+ messages=[
148
+ {"role": "system", "content": "์•„๋ž˜๋Š” ์ž‘์—…์„ ์„ค๋ช…ํ•˜๋Š” ์ง€์‹œ๋ฌธ๊ณผ ๋Œ€๋ณธ์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์ฐธ๊ณ ํ•  ์ค„๊ฑฐ๋ฆฌ์™€ ์ง์„ ์ด๋ฃจ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์š”์ฒญ์„ ์ ์ ˆํžˆ ๋งŒ์กฑํ•˜๋Š” ๋Œ€๋ณธ์„ ์ž‘์„ฑํ•˜์„ธ์š”."},
149
+ {"role": "user", "content": prompt},
150
+ ],
151
+ temperature = temperature,
152
+ max_tokens = max_tokens,
153
+ n=1,
154
+ )
155
+ for choice in response["choices"]:
156
+ content = choice["message"]["content"]
157
+
158
+ return content
159
+
160
+ """# gradio"""
161
+
162
+ import gradio as gr
163
+
164
+ generator1 = gr.Interface(
165
+ fn=yeollm_text,
166
+ inputs=[
167
+ gr.inputs.Textbox(label="Instruction"),
168
+ gr.inputs.Textbox(label="Summary")
169
+ ],
170
+ outputs=gr.outputs.Textbox(label="Yeollm Scenario"),
171
+ title="Yeollm Scenario Generation",
172
+ description="Generate scenarios using the Yeollm model.",
173
+ theme="huggingface"
174
+ )
175
+
176
+ generator2 = gr.Interface(
177
+ fn=davinci_text,
178
+ inputs=[
179
+ gr.inputs.Textbox(label="Instruction"),
180
+ gr.inputs.Textbox(label="Summary")
181
+ ],
182
+ outputs=gr.outputs.Textbox(label="Davinci Scenario"),
183
+ title="Davinci Generation",
184
+ description="Generate scenarios using the Davinci model.",
185
+ theme="huggingface"
186
+ )
187
+
188
+ generator3 = gr.Interface(
189
+ fn=gpt_text,
190
+ inputs=[
191
+ gr.inputs.Textbox(label="Instruction"),
192
+ gr.inputs.Textbox(label="Summary")
193
+ ],
194
+ outputs=gr.outputs.Textbox(label="GPT Scenario"),
195
+ title="GPT Generation",
196
+ description="Generate scenarios using the GPT model.",
197
+ theme="huggingface"
198
+ )
199
+
200
+ gr.Parallel(generator1, generator2, generator3).launch(share=True, debug=True)