Let’s install the topical image generation AI in your apps and services ~ Use Stable Diffusion on iPhone and Macbook ~

MLBoy
3 min readMar 13, 2023

Install image generation AI in your app

Image generation AI ( AI that generates images from text ) is a hot topic right now , but it would be nice if you could quickly incorporate it into your own app and use it for your service .
For example, you can automatically generate user profile icons and header images.
But how should we adopt AI?
In fact, you can include it in apps for Mac, iPhone, and iPad .
This way, you don’t need to prepare a server, you can generate images on the user’s device, so you can easily incorporate it into the service.

I tried to generate “Cat Princess” on iPhone
I tried to generate “Super Hero Cat” on Macbook

If you want to try it as a sample app, it is uploaded to the AppStore, so you can download it.

How to use image generation AI in iPhone / Mac application

To include image generation AI in your app, convert it to a format called CoreML.
Apple has provided a CoreML version of their image generation AI Stable Diffusion. Use this.

Download and run the sample project provided by HuggingFace.

You can generate an image by entering text and pressing the Generate button.

You can choose from several versions of the image generation model.
There is also an on/off function for SafetyCheck, which blocks adult and harmful content.

How to import into your own app

You can easily add the AI ​​image generation function by extracting the code of the CoreML model download part and the AI ​​execution part of the sample application and importing them into your own application.

AI execution

Below, the input text is vectorized with the textEncoder model.

let promptEmbedding = try textEncoder.encode(prompt)
let negativePromptEmbedding = try textEncoder.encode(negativePrompt)

Generate noise from Latent and text vectors in UNet.

var noise = try unet.predictNoise(
latents: latentUnetInput,
timeStep: t,
hiddenStates: hiddenStates
)

Finally, generate the image with the image generation model.

try decodeToImages(latents, disableSafety: disableSafety)
I tried to generate “Dark Enemy Cat”

🐣

I am a freelance engineer.
If you have any of the following requests, please feel free to contact us.
I want to develop AI services, I want to incorporate AI into my business to make it more efficient, I want to develop a smartphone app that uses AI, I want to create an
application that uses AR, I want to create a smartphone app but I don’t know who to consult, image generation AI, etc. I want to use ChatGPT for my own service…

All of them can be ordered at a reasonable price without intermediate costs.

For work inquiries, please contact

rockyshikoku@gmail.com

I am creating applications using machine learning and AR technology.

I send machine learning / AR related information.

GitHub

Twitter
Medium

--

--