import gradio as gr
def dmini(prompt):
from craiyon import Craiyon
import time
from datetime import datetime
import base64
from io import BytesIO
from PIL import Image
import re
# if prompt is empty, log to console an error
if prompt == "":
return ["error.png"] * 9
# get time now gmt
timestamp = time.strftime("%H:%M:%S", time.localtime())
# format it to a string
timestamp = str(timestamp)
# convert string prompt to lowercase
prompt = prompt.lower()
# create list bad_words_list from bad_words.txt and strip whitespace
with open('bad_words.txt') as f:
bad_words_list = f.readlines()
bad_words_list = [x.strip() for x in bad_words_list]
# check if bad_words_list is in prompt
if any(re.findall(r'\b{}\b'.format(bad_word), prompt) for bad_word in bad_words_list):
# check if any bad words are in prompt
blocked_words = []
for word in bad_words_list:
if word in prompt:
blocked_words.append(word)
blocked_words = ', '.join(blocked_words)
print(timestamp + ": BLOCKED PROMPT \"" + prompt + "\" BANNED WORDS: " + blocked_words)
time.sleep(8)
print("User has had their time wasted.")
return ["unsafe.png"] * 9
else:
generator = Craiyon() # Instantiates the api wrapper
result = generator.generate(prompt) # Generates 9 images by default and you cannot change that
images = result.images # A list containing image data as base64 encoded strings
imagelist = []
for i in images:
image = Image.open(BytesIO(base64.decodebytes(i.encode("utf-8"))))
imagelist.append(image)
print(timestamp + ": PROMPT \"" + prompt + "\" WAS GENERATED SUCCESSFULLY\n")
return imagelist
examples = ["Futuristic utopian city in Coruscant. god beams. white buildings. street view. golden sunset. colorful, cityscape, green trees, future, urban, megapolis, breathtaking, Imaginative, utopia, Wallpaper, beautiful design, scifi, high detail, global illumination, ArtStation, art by Sebastian Wagner, Stephan Martiniere, Leon Tukker, Vitaly-Sokol",
"A dream of a distant galaxy, by Caspar David Friedrich, matte painting trending on artstation HQ",
"Cute close portrait dainty beautiful futurstic white robotic girl, portraits, white cyberpunk inflatable formfitting tops, highly detailed sharp big eyes, white fringeshort hair, transparent intricate details, professional 3d visualisation in pastel colours, by wlop, intricate linework, trending on artstation, unreal engine 5 highly rendered, epic composition, by guweiz, shal. e, laica chrose, final fantasy",
"1990s movie, orbit space new york city street with many pedestrians bejng attacked by ufos as a loading screen, cinestill 800t 18mm, heavy grainy picture, very detailed, high quality, 4k panoramic, dramatic lightning, streetlight at night, rain, mud, foggy, anchray flags",
"Portrait of a woman by Greg Rutkowski, she is about 30 years old, pretty, blond hair with two strans around her face, slavic features, melancholic gaze, pretty aquiline nose, she is wearing a blue utilitarian jumpsuit, highly detailed portrait, digital painting, artstation, concept art, smooth, sharp foccus ilustration, Artstation HQ.",
"A small cabin on top of a snowy mountain in the style of Disney, artstation"]
article="""
If you're struggling to get started, here's a good prompt engineering guide put together by Stable Diffusion community member Graverman.
Raw prompt is the simplest way of describing what you want to generate, for instance;
This is the basic building block of any prompt. Most new people start by only using raw prompts, this is usually a mistake as the images you generate like this tend to get random and chaotic. Here are some examples that I generated with running the earlier prompts
As you can see, these images have random scenery and don’t look very aesthetically pleasing, I definitely wouldn’t consider them art. This brings me to my next point;
Style is a crucial part of the prompt. The AI, when missing a specified style, usually chooses the one it has seen the most in related images, for example, if I generated landscape, it would probably generate realistic or oil painting looking images. Having a well chosen style + raw prompt is sometimes enough, as the style influences the image the most right after the raw prompt.
The most commonly used styles include:
I’ll examine them one by one to give an overview on how you might use these styles.
In the case of a realistic image, there are various ways of making it the style, most resulting in similar images. Here are some commonly used techniques of making the image realistic:
You can of course combine these to get more and more realistic images.
To get oil painting you can just simply add “an oil painting of” to your prompt. This sometimes results in the image showing an oil painting in a frame, to fix this you can just re-run the prompt or use raw prompt + “oil painting”
To make a pencil drawing just simply add “a pencil drawing of” to your raw prompt or make your prompt raw prompt + “pencil drawing”.
The same applies to landscape art.
To make your style more specific, or the image more coherent, you can use artists’ names in your prompt. For instance, if you want a very abstract image, you can add “made by Pablo Picasso” or just simply, “Picasso”.
Below are lists of artists in different styles that you can use, but I always encourage you to search for different artists as it is a cool way of discovering new art.
PortraitMixing the artists is highly encouraged, as it can lead to interesting-looking art.
This is the part that some people take to extremes, leading to longer prompts than this article. Finishing touches are the final things that you add to your prompt to make it look like you want. For instance, if you want to make your image more artistic, add “trending on artstation”. If you want to add more realistic lighting add “Unreal Engine.” You can add anything you want, but here are some examples:
Highly detailed, surrealism, trending on art station, triadic color scheme, smooth, sharp focus, matte, elegant, the most beautiful image ever seen, illustration, digital paint, dark, gloomy, octane render, 8k, 4k, washed colors, sharp, dramatic lighting, beautiful, post processing, picture of the day, ambient lighting, epic composition
Prompt engineering allows you to have better control of what the image will look like. It (if done right) improves the image quality by a lot in every aspect. If you enjoyed this “article”, well, I’m glad I didn’t waste my time. If you see any ways that I can improve this, definitely let me know on discord (Graverman#0804)
This guide was written by Graverman, who can be found on twitter under the username @dailystablediff
This app is not run in affiliation with StabilityAI. This app uses the Stable Diffusion API to generate images.
The model is licensed with a CreativeML Open RAIL-M license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please read the license
""" with gr.Blocks(css = """ *, body, #name, #ex, #article, .border-gray-200, .gr-input, .border-gray-100 { background: #0b0f19; color: white; border-color: #4c4c4c; } .gr-samples-gallery .gr-sample-textbox, #greet_btn, #save_btn, .py-1, .gr-samples-gallery:hover .gr-sample-textbox:hover, .gr-text-input, #output, .wrap.svelte-1c38quw, .h-wrap { background: #1f2937; color: white; border-color: #4c4c4c;} """ ) as demo: output = gr.Gallery(label="Image Generation", elem_id="output").style(grid=3) name = gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate. Longer and more detailed prompts work better.", elem_id="name") greet_btn = gr.Button("Generate Image", elem_id="greet_btn") ex = gr.Examples(examples=examples, fn=dmini, inputs=name, outputs=output, cache_examples=True) ex.dataset.headers = [""] article = gr.HTML(article, elem_id="article") greet_btn.click(fn=dmini, inputs=name, outputs=output) demo.queue(concurrency_count=20, max_size=20).launch(max_threads=150)