Build A Text-To-Image Application with Python and Open Journey in A Few Lines of Code

Ifeanyi Idiaye
5 min readAug 3, 2023

--

Little girl image generated by Open Journey
Little girl by Open Journey

In this post, I will show you how you can easily build your own text-to-image application with Python and Open Journey by PromptHero in few lines of code.

Open Journey is an AI text-to-image model that makes AI art in the style of Mid Journey. It is like an open-source version of Mid Journey.

Let’s now see how we can use the model to easily generate images.

Install Python Libraries

In order to be able to run the Open Journey AI model, we will need a computer with a GPU. If your computer has a GPU, then you are good to go. But if you do not have a GPU computer, you can make use of free GPU providers like Google Colab and Amazon SageMaker Studio Lab.

There, you can create a Python environment and install all the necessary libraries. However, if you are using the Google Colab notebook, the first thing you will need to do is to change the runtime from CPU to GPU.

Therefore, go to “Runtime” in the menu bar above and select “Change runtime type”.

Google Colab notebook

Then under “Hardware accelerator”, select GPU and click save. Once you do that, you now have access to a GPU computer with 15GB GPU RAM and 78GB Hard disk space.

With the GPU all set up, let’s now begin to install the requisite Python libraries. In the cell of your Colab notebook, run the line of code below to install the necessary libraries

!pip install -q diffusers transformers accelerate gradio

The transformer and accelerate libraries are both dependency libraries without which the application will not work well. So, although we do not directly make use of them, we still have to install them.

We will use gradio to build a simple front-end for the application.

After installing the libraries, the next thing to do is to import Stable Diffusion Pipeline from the diffusers library and torch. Torch is a built-in Python module, and so we do not need to install it like the others; we only need to import it.

from diffusers import StableDiffusionPipeline
import torch

The Stable Diffusion Pipeline is the method we will use to download the Open Journey model. So, run the next lines of code in your notebook to download the model

model_id = "prompthero/openjourney"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

In the Stable Diffusion Pipeline method above, we passed the model ID and torch type to it. The “model ID” is the name of the model we are downloading while “torch.float16” to the “torch_dtype” argument helps to conserve memory in our GPU. After that, we piped to CUDA in order to make the model run faster.

Generate Image

Having downloaded the model, we will now pass a text prompt to our newly created pipeline in order to generate an image

prompt = "beautiful house made of juniper berries, a medium shot anime portrait by Stanley Artgerm Lau, WLOP, Rossdraws, James Jean, Andrei Riabovitchev, Marc Simonetti, and Sakimi chan"
image = pipe(prompt).images[0]
print(image)
Image generated by Open Journey
House made of juniper berries by Open Journey

Not a bad result at all! Now we are using the Open Journey AI model to generate images. You can run several iterations of the same prompt or tweak the prompt in order to get a better image. Remember that an image is only as good as the prompt you give to the model.

For help with writing text-to-image prompts, you can make use of the free Prompt Generator tool. There, you only need to supply the software with a phrase and it will intelligently build a prompt for you using that phrase. However, you can always modify the prompt before you run it in a text-to-image application if you wish.

Let’s generate another image with Open Journey

prompt = "Pixar style little boy holding ice cream cone smiling super detailed unreal engine 5 realistic intricate elegant highly detailed digital painting artstation concept art by Mark Brooks and Brad Kunkle detailed"
Little boy holding ice cream cones by Open Journey
Little boy holding ice cream cones by Open Journey

Again, not a bad result from Open Journey. You can add more detail to the prompt or play around with different prompts.

Let us now build a front-end for the application.

Gradio Front-end

For this, we will need to import the gradio library we installed earlier

import gradio as gr

Next, we will write the Python function for our application which we will pass to gradio’s interface method

def openArt(prompt):
model_id = "prompthero/openjourney"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

prompt = prompt
image = pipe(prompt).images[0]
return image

What we have done is to put together all the lines of code for the application that we had written earlier into a Python function.

Next, we will build the interface for the application by passing the function to gradio’s interface method.

app = gr.Interface(openArt,
inputs = "text",
outputs = "image",
theme = gr.themes.Soft(),
title = "Open Art")

Very simple isn’t it? Let’s now launch the app

app.launch()

You should see something like this in your notebook

Gradio front-end for text-to-image application

If you do, then you have successfully built a front-end for your text-to-image Python application. Now, you can more easily play with prompts, generate images, and download the generated images.

You can go on to deploy this application to Hugging Face as a Space, and access it via a URL, but that is beyond the scope of this post.

Conclusion

So, in just a few lines of Python code, we have built a text-to-image application using the Open Journey AI model.

Would love to see what kinds of images you generate! 😃

I hope you enjoyed reading this post!

You can follow me on Medium: Ifeanyi Idiaye

You can follow me on Twitter: @Ifeanyidiaye

You can also read my other posts on Medium below:

How to Deploy an R Shiny App as a Hugging Face Space | by Ifeanyi Idiaye | Jun, 2023 | Medium

Easily Convert PDF File to Word Using {Convert2Docx} R Package | by Ifeanyi Idiaye | Jul, 2023 | Medium

How to Connect Local Folder on Your Computer to A GitHub Repository | by Ifeanyi Idiaye | Jul, 2023 | Medium

--

--

Ifeanyi Idiaye

I am a data scientist, who is passionate about AI and building AI-driven applications. I am also a data analytics developer and writer for Dev Genius.