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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
- mistralai/Mistral-7B-v0.1
|
| 11 |
- StableDiffusionXLPipeline
|
| 12 |
- 'inference: false'
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Prompt Diffuser
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
This model was build via parameter-efficient QLoRA finetuning of [Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1) on a small custom dataset.
|
| 20 |
+
The goal of this project is to build a model which takes simple and short natural language inputs and generate a highly detailed and descriptive prompt for [Stable Diffusion](https://stability.ai/stable-diffusion) and other similar image generation models.
|
| 21 |
+
Hence making it easier for everyone to get high quality images easily.
|
| 22 |
+
|
| 23 |
+
## Note
|
| 24 |
+
|
| 25 |
+
>This is the first iteration of this project and it was to check the capabilities of Mistral-7B model.
|
| 26 |
+
>As of now the model is fine-tuned mostly on the prompts which includes humans, with description of the subject and the scene.
|
| 27 |
+
>This is a work in progress and this is just a prototype. Hence, one can expect issues with response from the model.
|
| 28 |
+
|
| 29 |
+
## Input Template
|
| 30 |
+
|
| 31 |
+
```
|
| 32 |
+
###Human:
|
| 33 |
+
generate a stable diffusion prompt for {input}
|
| 34 |
+
|
| 35 |
+
###Assistant:
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Output Template
|
| 39 |
+
|
| 40 |
+
```
|
| 41 |
+
###Human:
|
| 42 |
+
generate a stable diffusion prompt for a model in a cafe sipping coffee
|
| 43 |
+
|
| 44 |
+
###Assistant:
|
| 45 |
+
A model in a cafe sipping coffee, with a warm glow illuminating her face. She wears a stylish dress and has a book in her hand. The background is a cozy cafe with dim lights.
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Example prompts and responses
|
| 49 |
+
|
| 50 |
+
Example 1:
|
| 51 |
+
|
| 52 |
+
**User**:
|
| 53 |
+
|
| 54 |
+
>a model in a cafe sipping
|
| 55 |
+
|
| 56 |
+
**Prompt Diffusion**
|
| 57 |
+
|
| 58 |
+
>A model in a cafe sipping coffee, with a warm glow illuminating her face.
|
| 59 |
+
She wears a stylish dress and has a book in her hand. The background is a cozy cafe with dim lights.
|
| 60 |
+
|
| 61 |
+
<br>
|
| 62 |
+
|
| 63 |
+
Example 2:
|
| 64 |
+
|
| 65 |
+
**User**:
|
| 66 |
+
|
| 67 |
+
>a man skiing
|
| 68 |
+
|
| 69 |
+
**Prompt Diffusion**
|
| 70 |
+
|
| 71 |
+
>A man in a ski suit, with a helmet and goggles, is skiing down a snowy mountain, with the wind blowing in his face and the sun shining brightly.
|
| 72 |
+
The man is enjoying the thrill of skiing, as he glides down the slopes, leaving behind a trail of fresh powder.
|
| 73 |
+
|
| 74 |
+
<br>
|
| 75 |
+
|
| 76 |
+
Example 3:
|
| 77 |
+
|
| 78 |
+
**User**:
|
| 79 |
+
|
| 80 |
+
>a beautiful woman dancing in rain
|
| 81 |
+
|
| 82 |
+
**Prompt Diffusion**
|
| 83 |
+
|
| 84 |
+
>A beautiful woman, dressed in a flowing white gown, dances gracefully in the rain.
|
| 85 |
+
The raindrops fall gently around her, creating a magical and ethereal atmosphere.
|
| 86 |
+
She moves with grace and elegance, her long hair flowing in the wind.
|
| 87 |
+
The rain creates a shimmering effect on her gown, making her look like a goddess.
|
| 88 |
+
|
| 89 |
+
<br>
|
| 90 |
+
|
| 91 |
+
Example 4:
|
| 92 |
+
|
| 93 |
+
**User**:
|
| 94 |
+
|
| 95 |
+
>a man on a beach
|
| 96 |
+
|
| 97 |
+
**Prompt Diffusion**
|
| 98 |
+
|
| 99 |
+
>A man sits on a beach, the waves crashing against the shore.
|
| 100 |
+
The sun is setting, casting a warm glow over the sand and water.
|
| 101 |
+
The man is lost in thought, his mind wandering as he takes in the beauty of the scene.
|
| 102 |
+
|
| 103 |
+
<br>
|
| 104 |
+
|
| 105 |
+
## Demo
|
| 106 |
+
|
| 107 |
+
>This code can be run even on the free version of Google Colab. Change the runtime to **GPU - T4** and run the notebook below:
|
| 108 |
+
|
| 109 |
+
>[**Try on Colab**](https://colab.research.google.com/drive/1NfNA6sYWUG0Nqy42lhyJfUIqDxsrV64d?usp=sharing&authuser=1#scrollTo=c41VQmkeG8AS)
|
| 110 |
+
|
| 111 |
+
## Basic usage
|
| 112 |
+
|
| 113 |
+
```python
|
| 114 |
+
!pip install git+https://github.com/huggingface/transformers
|
| 115 |
+
!pip install git+https://github.com/huggingface/peft.git
|
| 116 |
+
!pip install torch
|
| 117 |
+
!pip install -q bitsandbytes accelerate
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
```python
|
| 121 |
+
#Importing libraries
|
| 122 |
+
|
| 123 |
+
from peft import PeftConfig, PeftModel
|
| 124 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 125 |
+
import torch
|
| 126 |
+
import re
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
```python
|
| 130 |
+
#Loading adapter model and merging it with base model for inferencing
|
| 131 |
+
|
| 132 |
+
torch.set_default_device('cuda')
|
| 133 |
+
|
| 134 |
+
peft_model_id = "abhishek7/Prompt_diffusion-v0.1"
|
| 135 |
+
|
| 136 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
| 137 |
+
|
| 138 |
+
bnb_config = BitsAndBytesConfig(
|
| 139 |
+
load_in_4bit=True,
|
| 140 |
+
bnb_4bit_quant_type="nf4",
|
| 141 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 145 |
+
config.base_model_name_or_path,
|
| 146 |
+
low_cpu_mem_usage=True,
|
| 147 |
+
load_in_4bit=True,
|
| 148 |
+
quantization_config=bnb_config,
|
| 149 |
+
torch_dtype=torch.float16,
|
| 150 |
+
device_map="auto"
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
| 154 |
+
|
| 155 |
+
model = model.merge_and_unload()
|
| 156 |
+
|
| 157 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path, trust_remote_code=True)
|
| 158 |
+
tokenizer.padding_side = "right"
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
```python
|
| 162 |
+
# Function to truncate text based on punctuation count
|
| 163 |
+
|
| 164 |
+
def truncate_text(text, max_punctuation):
|
| 165 |
+
punctuation_count = 0
|
| 166 |
+
truncated_text = ""
|
| 167 |
+
for char in text:
|
| 168 |
+
truncated_text += char
|
| 169 |
+
if char in [',', '.']:
|
| 170 |
+
punctuation_count += 1
|
| 171 |
+
if punctuation_count >= max_punctuation:
|
| 172 |
+
break
|
| 173 |
+
# Replace the last comma with a full stop if the last punctuation is a comma
|
| 174 |
+
if truncated_text.rstrip()[-1] == ',':
|
| 175 |
+
truncated_text = truncated_text.rstrip()[:-1] + '.'
|
| 176 |
+
|
| 177 |
+
return truncated_text
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
# Function to generate prompt
|
| 181 |
+
|
| 182 |
+
def generate_prompt(input, max_length, temperature):
|
| 183 |
+
input_context = f'''
|
| 184 |
+
###Human:
|
| 185 |
+
generate a stable diffusion prompt for {input}
|
| 186 |
+
|
| 187 |
+
###Assistant:
|
| 188 |
+
'''
|
| 189 |
+
|
| 190 |
+
inputs = tokenizer.encode(input_context, return_tensors="pt")
|
| 191 |
+
outputs = model.generate(inputs, max_length=max_length, temperature=temperature, num_return_sequences=1)
|
| 192 |
+
|
| 193 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens = True)
|
| 194 |
+
|
| 195 |
+
# Extract the Assistant's response using regex
|
| 196 |
+
match = re.search(r'###Assistant:(.*?)(###Human:|$)', output_text, re.DOTALL)
|
| 197 |
+
if match:
|
| 198 |
+
assistant_response = match.group(1)
|
| 199 |
+
else:
|
| 200 |
+
raise ValueError("No Assistant response found")
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
# Truncate the Assistant's response based on the criteria
|
| 205 |
+
truncated_response = truncate_text(assistant_response, max_punctuation=10)
|
| 206 |
+
|
| 207 |
+
return truncated_response
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
```python
|
| 211 |
+
# Usage:
|
| 212 |
+
|
| 213 |
+
input_text = "a beautiful woman dancing in rain"
|
| 214 |
+
|
| 215 |
+
prompt = generate_prompt(input_text, 150, 0.3)
|
| 216 |
+
|
| 217 |
+
print("\nPrompt: " + prompt)
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
## Acknowledgements
|
| 221 |
+
|
| 222 |
+
This model was finetuned by Abhishek Kalra on Sep 29, 2023 and is for research applications only.
|
| 223 |
+
|
| 224 |
+
## mistralai/Mistral-7B-v0.1 citation
|
| 225 |
+
|
| 226 |
+
```
|
| 227 |
+
coming
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
## Framework versions
|
| 231 |
+
|
| 232 |
+
- PEFT 0.6.0.dev0
|