Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Promptist: reinforcement learning for automatic prompt optimization
|
2 |
+
|
3 |
+
## News
|
4 |
+
- [Demo Release] Dec, 2022: [Demo at HuggingFace Space](https://aka.ms/promptist-demo)
|
5 |
+
- [Model Release] Dec, 2022: [link](#load-pretrained-model-for-stable-diffusion-v14)
|
6 |
+
- [Paper Release] Dec, 2022: [Optimizing Prompts for Text-to-Image Generation](https://aka.ms/promptist-paper)
|
7 |
+
|
8 |
+
> - Language models serve as a prompt interface that optimizes user input into model-preferred prompts.
|
9 |
+
|
10 |
+
> - Learn a language model for automatic prompt optimization via reinforcement learning.
|
11 |
+
|
12 |
+
![image](https://user-images.githubusercontent.com/1070872/207856962-02f08d92-f2bf-441a-b1c3-efff1a4b6187.png)
|
13 |
+
|
14 |
+
|
15 |
+
## Load Pretrained Model for [Stable Diffusion v1.4](https://huggingface.co/CompVis/stable-diffusion-v1-4)
|
16 |
+
|
17 |
+
You can try the online demo at [https://huggingface.co/spaces/microsoft/Promptist](https://huggingface.co/spaces/microsoft/Promptist).
|
18 |
+
|
19 |
+
`[Note]` the online demo at HuggingFace Space is using CPU, so slow generation speed would be expected. Please load the model locally with GPUs for faster generation.
|
20 |
+
|
21 |
+
```python
|
22 |
+
import gradio as grad
|
23 |
+
import torch
|
24 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
25 |
+
|
26 |
+
def load_prompter():
|
27 |
+
prompter_model = AutoModelForCausalLM.from_pretrained("microsoft/Promptist")
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
29 |
+
tokenizer.pad_token = tokenizer.eos_token
|
30 |
+
tokenizer.padding_side = "left"
|
31 |
+
return prompter_model, tokenizer
|
32 |
+
|
33 |
+
prompter_model, prompter_tokenizer = load_prompter()
|
34 |
+
|
35 |
+
def generate(plain_text):
|
36 |
+
input_ids = prompter_tokenizer(plain_text.strip()+" Rephrase:", return_tensors="pt").input_ids
|
37 |
+
eos_id = prompter_tokenizer.eos_token_id
|
38 |
+
outputs = prompter_model.generate(input_ids, do_sample=False, max_new_tokens=75, num_beams=8, num_return_sequences=8, eos_token_id=eos_id, pad_token_id=eos_id, length_penalty=-1.0)
|
39 |
+
output_texts = prompter_tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
40 |
+
res = output_texts[0].replace(plain_text+" Rephrase:", "").strip()
|
41 |
+
return res
|
42 |
+
|
43 |
+
txt = grad.Textbox(lines=1, label="Initial Text", placeholder="Input Prompt")
|
44 |
+
out = grad.Textbox(lines=1, label="Optimized Prompt")
|
45 |
+
examples = ["A rabbit is wearing a space suit", "Several railroad tracks with one train passing by", "The roof is wet from the rain", "Cats dancing in a space club"]
|
46 |
+
|
47 |
+
grad.Interface(fn=generate,
|
48 |
+
inputs=txt,
|
49 |
+
outputs=out,
|
50 |
+
title="Promptist Demo",
|
51 |
+
description="Promptist is a prompt interface for Stable Diffusion v1-4 (https://huggingface.co/CompVis/stable-diffusion-v1-4) that optimizes user input into model-preferred prompts.",
|
52 |
+
examples=examples,
|
53 |
+
allow_flagging='never',
|
54 |
+
cache_examples=False,
|
55 |
+
theme="default").launch(enable_queue=True, debug=True)
|
56 |
+
```
|