Instructions to use microsoft/Promptist with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Promptist with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/Promptist")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/Promptist") model = AutoModelForCausalLM.from_pretrained("microsoft/Promptist") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Promptist with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Promptist" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Promptist", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/Promptist
- SGLang
How to use microsoft/Promptist with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "microsoft/Promptist" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Promptist", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "microsoft/Promptist" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Promptist", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/Promptist with Docker Model Runner:
docker model run hf.co/microsoft/Promptist
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 |
+

|
| 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 |
+
```
|