-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
183 lines (163 loc) · 5.67 KB
/
app.py
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
import os
import random
import torch
import gradio as gr
from peft import AutoPeftModelForCausalLM
from transformers import AutoTokenizer
events = [
"nature retreat",
"work / office event",
"wedding as a guest",
"tropical vacation",
"conference",
"sports event",
"winter vacation",
"beach",
"play / concert",
"picnic",
"night club",
"national parks",
"music festival",
"job interview",
"city tour",
"halloween party",
"graduation",
"gala / exhibition opening",
"fancy date",
"cruise",
"casual gathering",
"concert",
"cocktail party",
"casual date",
"business meeting",
"camping / hiking",
"birthday party",
"bar",
"business lunch",
"bachelorette / bachelor party",
"semi-casual event",
]
def format_instruction(input, context):
return f"""You are a personal stylist recommending fashion advice and clothing combinations. Use the self body and style description below, combined with the event described in the context to generate 5 self-contained and complete outfit combinations.
### Input:
{input}
### Context:
I'm going to a {context}.
### Response:
"""
def main():
# load base LLM model, LoRA params and tokenizer
model = AutoPeftModelForCausalLM.from_pretrained(
"neuralwork/mistral-7b-style-instruct",
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
load_in_4bit=True,
)
tokenizer = AutoTokenizer.from_pretrained("neuralwork/mistral-7b-style-instruct")
def postprocess(outputs, prompt):
outputs = outputs.detach().cpu().numpy()
output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
output = output[len(prompt) :]
return output
def generate(
prompt: str,
event: str,
top_p: float,
temperature: float,
max_new_tokens: int,
min_new_tokens: int,
seed: int,
):
torch.manual_seed(seed)
prompt = format_instruction(str(prompt), str(event))
input_ids = tokenizer(
prompt, return_tensors="pt", truncation=True
).input_ids.cuda()
with torch.inference_mode():
outputs = model.generate(
input_ids=input_ids,
max_new_tokens=max_new_tokens,
min_new_tokens=min_new_tokens,
do_sample=True,
top_p=top_p,
temperature=temperature,
)
output = postprocess(outputs, prompt)
return output
with gr.Blocks() as demo:
gr.HTML(
"""
<h1 style="font-weight: 900; margin-bottom: 7px;">
Instruct Fine-tune Mistral-7B-v0
</h1>
<p>Mistral-7B-v0 fine-tuned on the <a href="https://huggingface.co/datasets/neuralwork/fashion-style-instruct">neuralwork/style-instruct</a> dataset.
To use the model, simply describe your body type and personal style and select the type of event you're planning to go.
<br/>
See our <a href="https://neuralwork.ai/">blog post</a> for a detailed tutorial to fine-tune Mistral on your own dataset.
<p/>"""
)
with gr.Row():
with gr.Column(scale=1):
prompt = gr.Textbox(
lines=4,
label="Style prompt, describe your body type and fashion style.",
interactive=True,
value="I'm an above average height athletic woman with slightly of broad shoulders and a medium sized bust. I generally prefer a casual but sleek look with dark colors and jeans.",
)
event = gr.Dropdown(
choices=events, value="semi-casual event", label="Event type"
)
seed = gr.Number(
value=1371,
precision=0,
interactive=True,
label="Seed for reproducibility, set to -1 to randomize seed",
)
top_p = gr.Slider(
value=0.9,
label="Top p (nucleus sampling)",
minimum=0.0,
maximum=1.0,
step=0.01,
)
max_new_tokens = gr.Slider(
minimum=1,
maximum=2048,
value=1500,
label="Maximum new tokens",
)
min_new_tokens = gr.Slider(
minimum=-1, maximum=2048, value=-1, label="Minimum new tokens"
)
temperature = gr.Slider(
minimum=0.01, maximum=5, value=0.9, step=0.01, label="Temperature"
)
repetition_penalty = gr.Slider(
label="Repetition penalty",
minimum=1.0,
maximum=2.0,
step=0.05,
value=1.2,
)
generate_button = gr.Button("Get outfit suggestions")
with gr.Column(scale=2):
response = gr.Textbox(
lines=6, label="Outfit suggestions", interactive=False
)
gr.Markdown("From [neuralwork](https://neuralwork.ai/) with :heart:")
generate_button.click(
fn=generate,
inputs=[
prompt,
event,
top_p,
temperature,
max_new_tokens,
min_new_tokens,
seed,
],
outputs=response,
)
demo.launch(share=True)
if __name__ == "__main__":
main()