Scroll till the end of the page if you just want code
Prerequisites
For this tutorial you would need Ollama and AUTOMATIC1111 locally installed. You can easily find instructions online how to do it for your system. I'm using Docker images for both of those. Also you would need about 32GB of RAM installed in your PC since two models are "eating" a lot of it.
The problem
Making a prompt for image generation models like Stable Diffusion is not a simple task. Instead of just asking what you need in simple sentence, you would need to describe all the small details about object and environment using quite big set of keywords.
Let's try to solve this problem!
Setup
Create new console app and add nuget packages:
1 2 3 |
|
Creating models
Ollama model
We will use latest version of llama3.1
for our task. If you don't have mistral yet - it will be downloaded.
1 2 3 4 5 6 7 8 |
|
Here we are stopping generation after \n
symbol appears. Mistral will put a new line(\n
) symbol after prompt is generated.
We are using Temperature of 0 to always have the same result for the same prompt.
assigning events to model is a good way to see what is going on.
Stable diffusion model
You can select your model in Automatic1111 UI.
1 2 3 4 5 6 7 8 9 10 11 |
|
You should be familiar with these parameters if you were using SD before. But in simple words we're asking SD to generate a portrait without anything bad on it.
Prompt
At this point we are ready to start our LLM magic.
We will be using a special prompting technique to explain our expectations to mistral.
I took it from here with some minor modifications.
Basically, we are showing some examples so model could understand a principle of prompt generation. You can play around with examples and instructions to better match your preferences.
Now let's build a chain!
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 |
|
If everything done correctly - you should have image.png
in your bin directory.
Complete code
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 |
|