Add application file
Browse files
app.py
CHANGED
@@ -1,15 +1,17 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
|
4 |
-
import fire
|
5 |
-
import gradio as gr
|
6 |
import torch
|
7 |
-
import transformers
|
8 |
from peft import PeftModel
|
9 |
-
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
if torch.cuda.is_available():
|
15 |
device = "cuda"
|
@@ -19,203 +21,119 @@ else:
|
|
19 |
try:
|
20 |
if torch.backends.mps.is_available():
|
21 |
device = "mps"
|
22 |
-
except:
|
23 |
pass
|
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 |
else:
|
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 |
**kwargs,
|
99 |
-
)
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
num_beams=num_beams,
|
108 |
-
**kwargs,
|
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 |
-
# new_tokens = len(output) - len(input_ids[0])
|
142 |
-
decoded_output = tokenizer.decode(output)
|
143 |
-
|
144 |
-
if output[-1] in [tokenizer.eos_token_id]:
|
145 |
-
break
|
146 |
-
|
147 |
-
yield prompter.get_response(decoded_output)
|
148 |
-
return # early return for stream_output
|
149 |
-
|
150 |
-
# Without streaming
|
151 |
-
with torch.no_grad():
|
152 |
-
generation_output = model.generate(
|
153 |
-
input_ids=input_ids,
|
154 |
-
generation_config=generation_config,
|
155 |
-
return_dict_in_generate=True,
|
156 |
-
output_scores=True,
|
157 |
-
max_new_tokens=max_new_tokens,
|
158 |
-
)
|
159 |
-
s = generation_output.sequences[0]
|
160 |
-
output = tokenizer.decode(s)
|
161 |
-
yield prompter.get_response(output)
|
162 |
-
|
163 |
-
gr.Interface(
|
164 |
-
fn=evaluate,
|
165 |
-
inputs=[
|
166 |
-
gr.components.Textbox(
|
167 |
-
lines=2,
|
168 |
-
label="Instruction",
|
169 |
-
placeholder="Generate an Ad for the iPhone 14.",
|
170 |
-
),
|
171 |
-
gr.components.Textbox(lines=2, label="Input", placeholder="none"),
|
172 |
-
gr.components.Slider(
|
173 |
-
minimum=0, maximum=1, value=0.1, label="Temperature"
|
174 |
-
),
|
175 |
-
gr.components.Slider(
|
176 |
-
minimum=0, maximum=1, value=0.75, label="Top p"
|
177 |
-
),
|
178 |
-
gr.components.Slider(
|
179 |
-
minimum=0, maximum=100, step=1, value=40, label="Top k"
|
180 |
-
),
|
181 |
-
gr.components.Slider(
|
182 |
-
minimum=1, maximum=4, step=1, value=4, label="Beams"
|
183 |
-
),
|
184 |
-
gr.components.Slider(
|
185 |
-
minimum=1, maximum=2000, step=1, value=128, label="Max tokens"
|
186 |
-
),
|
187 |
-
gr.components.Checkbox(label="Stream output"),
|
188 |
-
],
|
189 |
-
outputs=[
|
190 |
-
gr.inputs.Textbox(
|
191 |
-
lines=5,
|
192 |
-
label="Output",
|
193 |
-
)
|
194 |
-
],
|
195 |
-
title="🦙🛍️ LLaMA-E",
|
196 |
-
description="LLaMA-E is a series of fine-tuned LLaMA model following the E-commerce instructions. It is developed by DSMI (http://dsmi.tech/) @ University of Technology Sydney, and trained on the 120k instruction set. This model is for academic research use only. For more details please contact: Kaize.Shi@uts.edu.au",
|
197 |
-
# noqa: E501
|
198 |
-
).queue().launch(server_name="0.0.0.0", share=share_gradio)
|
199 |
-
# Old testing code follows.
|
200 |
-
|
201 |
-
"""
|
202 |
-
# testing code for readme
|
203 |
-
for instruction in [
|
204 |
-
"Tell me about alpacas.",
|
205 |
-
"Tell me about the president of Mexico in 2019.",
|
206 |
-
"Tell me about the king of France in 2019.",
|
207 |
-
"List all Canadian provinces in alphabetical order.",
|
208 |
-
"Write a Python program that prints the first 10 Fibonacci numbers.",
|
209 |
-
"Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.", # noqa: E501
|
210 |
-
"Tell me five words that rhyme with 'shock'.",
|
211 |
-
"Translate the sentence 'I have no mouth but I must scream' into Spanish.",
|
212 |
-
"Count up from 1 to 500.",
|
213 |
-
]:
|
214 |
-
print("Instruction:", instruction)
|
215 |
-
print("Response:", evaluate(instruction))
|
216 |
-
print()
|
217 |
-
"""
|
218 |
-
|
219 |
-
|
220 |
-
if __name__ == "__main__":
|
221 |
-
fire.Fire(main)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
2 |
from peft import PeftModel
|
3 |
+
import transformers
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
assert (
|
7 |
+
"LlamaTokenizer" in transformers._import_structure["models.llama"]
|
8 |
+
), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"
|
9 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
|
10 |
+
|
11 |
+
tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
|
12 |
+
|
13 |
+
BASE_MODEL = "meta-llama/Llama-2-7b-hf"
|
14 |
+
LORA_WEIGHTS = "DSMI/LLaMA-E"
|
15 |
|
16 |
if torch.cuda.is_available():
|
17 |
device = "cuda"
|
|
|
21 |
try:
|
22 |
if torch.backends.mps.is_available():
|
23 |
device = "mps"
|
24 |
+
except:
|
25 |
pass
|
26 |
|
27 |
+
if device == "cuda":
|
28 |
+
model = LlamaForCausalLM.from_pretrained(
|
29 |
+
BASE_MODEL,
|
30 |
+
load_in_8bit=False,
|
31 |
+
torch_dtype=torch.float16,
|
32 |
+
device_map="auto",
|
33 |
+
)
|
34 |
+
model = PeftModel.from_pretrained(
|
35 |
+
model, LORA_WEIGHTS, torch_dtype=torch.float16, force_download=True
|
36 |
+
)
|
37 |
+
elif device == "mps":
|
38 |
+
model = LlamaForCausalLM.from_pretrained(
|
39 |
+
BASE_MODEL,
|
40 |
+
device_map={"": device},
|
41 |
+
torch_dtype=torch.float16,
|
42 |
+
)
|
43 |
+
model = PeftModel.from_pretrained(
|
44 |
+
model,
|
45 |
+
LORA_WEIGHTS,
|
46 |
+
device_map={"": device},
|
47 |
+
torch_dtype=torch.float16,
|
48 |
+
)
|
49 |
+
else:
|
50 |
+
model = LlamaForCausalLM.from_pretrained(
|
51 |
+
BASE_MODEL, device_map={"": device}, low_cpu_mem_usage=True
|
52 |
+
)
|
53 |
+
model = PeftModel.from_pretrained(
|
54 |
+
model,
|
55 |
+
LORA_WEIGHTS,
|
56 |
+
device_map={"": device},
|
57 |
+
)
|
58 |
+
|
59 |
+
|
60 |
+
def generate_prompt(instruction, input=None):
|
61 |
+
if input:
|
62 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
63 |
+
### Instruction:
|
64 |
+
{instruction}
|
65 |
+
### Input:
|
66 |
+
{input}
|
67 |
+
### Response:"""
|
|
|
68 |
else:
|
69 |
+
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
70 |
+
### Instruction:
|
71 |
+
{instruction}
|
72 |
+
### Response:"""
|
73 |
+
|
74 |
+
if device != "cpu":
|
75 |
+
model.half()
|
76 |
+
model.eval()
|
77 |
+
if torch.__version__ >= "2":
|
78 |
+
model = torch.compile(model)
|
79 |
+
|
80 |
+
|
81 |
+
def evaluate(
|
82 |
+
instruction,
|
83 |
+
input=None,
|
84 |
+
temperature=0.1,
|
85 |
+
top_p=0.75,
|
86 |
+
top_k=40,
|
87 |
+
num_beams=4,
|
88 |
+
max_new_tokens=128,
|
89 |
+
**kwargs,
|
90 |
+
):
|
91 |
+
prompt = generate_prompt(instruction, input)
|
92 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
93 |
+
input_ids = inputs["input_ids"].to(device)
|
94 |
+
generation_config = GenerationConfig(
|
95 |
+
temperature=temperature,
|
96 |
+
top_p=top_p,
|
97 |
+
top_k=top_k,
|
98 |
+
num_beams=num_beams,
|
99 |
**kwargs,
|
100 |
+
)
|
101 |
+
with torch.no_grad():
|
102 |
+
generation_output = model.generate(
|
103 |
+
input_ids=input_ids,
|
104 |
+
generation_config=generation_config,
|
105 |
+
return_dict_in_generate=True,
|
106 |
+
output_scores=True,
|
107 |
+
max_new_tokens=max_new_tokens,
|
|
|
|
|
108 |
)
|
109 |
+
s = generation_output.sequences[0]
|
110 |
+
output = tokenizer.decode(s)
|
111 |
+
return output.split("### Response:")[1].strip()
|
112 |
+
|
113 |
+
|
114 |
+
g = gr.Interface(
|
115 |
+
fn=evaluate,
|
116 |
+
inputs=[
|
117 |
+
gr.components.Textbox(
|
118 |
+
lines=2, label="Instruction", placeholder="Tell me about alpacas."
|
119 |
+
),
|
120 |
+
gr.components.Textbox(lines=2, label="Input", placeholder="none"),
|
121 |
+
gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
|
122 |
+
gr.components.Slider(minimum=0, maximum=1, value=0.75, label="Top p"),
|
123 |
+
gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
|
124 |
+
gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
|
125 |
+
gr.components.Slider(
|
126 |
+
minimum=1, maximum=512, step=1, value=128, label="Max tokens"
|
127 |
+
),
|
128 |
+
],
|
129 |
+
outputs=[
|
130 |
+
gr.inputs.Textbox(
|
131 |
+
lines=5,
|
132 |
+
label="Output",
|
133 |
+
)
|
134 |
+
],
|
135 |
+
title="🦙🛍️ LLaMA-E",
|
136 |
+
description="LLaMA-E is a series of fine-tuned LLaMA model following the E-commerce instructions. It is developed by DSMI (http://dsmi.tech/) @ University of Technology Sydney, and trained on the 120k instruction set. This model is for academic research use only. For more details please contact: Kaize.Shi@uts.edu.au",
|
137 |
+
)
|
138 |
+
g.queue(concurrency_count=1)
|
139 |
+
g.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|