File size: 5,193 Bytes
6b1d376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Untitled31 (2).ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1jx1zW74zl2vFolee01ukC1b11uyTJDZ4
"""


import os
from datasets import load_dataset

# download dataset
dataset = load_dataset("neuralwork/fashion-style-instruct")
print(dataset)

# print a sample triplet
print(dataset["train"][0])

def format_instruction(sample):
    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:
        {sample["input"]}

        ### Context:
        {sample["context"]}

        ### Response:
        {sample["completion"]}
    """

sample = dataset["train"][0]
print(format_instruction(sample))

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,

    ):
        torch.manual_seed(1347)
        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=1500,
                min_new_tokens=10,
                do_sample=True,
                top_p=0.9,
                temperature=.9,
            )

        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"
                )

                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,

            ],
            outputs=response,
        )

    demo.launch(share=True)


if __name__ == "__main__":
    main()