ESC
React Native Starter AI logo
Menu
On this page

Features > Stable Diffusion Integration with RunPod

There are many ways of deploying Stable Diffusion models through different set of Cloud Providers but with React Native Starter AI, we are deploying Stable Diffusion, the AI image generation model, through RunPod because it is one of the most cost-effective ways of doing it thanks to their Serverless Instances. By serverless instances, we are only paying what we use in seconds.


Running Stable Diffusion on Serverless RunPod Instances

Main deployment steps of Stable Diffusion to RunPod serverless instances is explained in details in AI Services Docs. Here we are going to dive deep into how we are using the Stable Diffusion API for generating images.


Text To Image Flow

In text to image generation flow, we are using Stable diffusion deployed on a RunPod serverless instance. The RunPod API token is secured in Firebase functions backend as explained in AI Services Docs. The image generation code is just like below:


react-native-starter-backend/functions/src/index.ts
1 logger.info("Started text to image", { structuredData: true }); 2 const requestBody = request.body; 3 4 const style = requestBody.style; 5 const prompt = requestBody.prompt; 6 7 const aiRequestBody = constructTextToImageBody({ prompt, style }); 8 9 const config = { 10 method: "post", 11 maxBodyLength: Infinity, 12 url: process.env.RUNPOD_TEXT_TO_IMAGE_ENDPOINT, 13 headers: { 14 Authorization: `Bearer ${process.env.RUNPOD_API_TOKEN}`, 15 "Content-Type": "application/json", 16 }, 17 data: aiRequestBody, 18 }; 19 logger.info(`Starting to hit RunPod`, { structuredData: true }); 20 axios.interceptors.response.use( 21 function (response) { 22 // Convert status code to 500 if 200 RESPONSE RETURNS "FAILED" or "IN_QUEUE" status to retry 23 if ( 24 response.data.status === "FAILED" || 25 response.data.status === "IN_QUEUE" 26 ) { 27 response.status = 500; 28 throw response; 29 } 30 response.status = 200; 31 return response; 32 }, 33 function (error) { 34 return error; 35 } 36 ); 37 38 // Custom retry delay 39 axiosRetry(axios, { 40 retryDelay: (retryCount) => { 41 return retryCount * 1; 42 }, 43 retries: 15, 44 }); 45 46 const result = await axios.request(config); 47 logger.info(`RunPod response is successful!`, { structuredData: true }); 48 response.json({ result: result.data });

The text to image generation endpoint gets 2 input arguments as style and prompt to be used in image generation flow but these can be extended depending on your needs. The example request body of the endpoint is just like below:


1{ 2 "style": "3d", 3 "prompt": "Astronaut on the sun", 4}

Image to Image Flow

Image to image flow is also managed through Stable Diffusion AI model which is deployed on RunPod serverless instance. The same as Text to Image flow, the RunPod API key is secured through doing to image generation in Firebase functions backend. The image to image generation code is just like below:


react-native-starter-backend/functions/src/index.ts
1// Image to image 2export const imageToImage = onRequest(async (request, response) => { 3 logger.info("Started image to image", { structuredData: true }); 4 const requestBody = request.body; 5 6 const style = requestBody.style; 7 const prompt = requestBody.prompt; 8 const inputImg = requestBody.inputImg; 9 10 const aiRequestBody = constructImageToImageBody({ prompt, style, inputImg }); 11 12 const config = { 13 method: "post", 14 maxBodyLength: Infinity, 15 url: process.env.RUNPOD_TEXT_TO_IMAGE_ENDPOINT, 16 headers: { 17 Authorization: `Bearer ${process.env.RUNPOD_API_TOKEN}`, 18 "Content-Type": "application/json", 19 }, 20 data: aiRequestBody, 21 }; 22 logger.info(`Starting to hit RunPod`, { structuredData: true }); 23 axios.interceptors.response.use( 24 function (response) { 25 // Convert status code to 500 if 200 RESPONSE RETURNS "FAILED" or "IN_QUEUE" status to retry 26 if ( 27 response.data.status === "FAILED" || 28 response.data.status === "IN_QUEUE" 29 ) { 30 response.status = 500; 31 throw response; 32 } 33 response.status = 200; 34 return response; 35 }, 36 function (error) { 37 return error; 38 } 39 ); 40 41 // Custom retry delay 42 axiosRetry(axios, { 43 retryDelay: (retryCount) => { 44 return retryCount * 1; 45 }, 46 retries: 15, 47 }); 48 49 const result = await axios.request(config); 50 logger.info(`RunPod response is successful!`, { structuredData: true }); 51 response.json({ result: result.data }); 52});

The image to image generation endpoint gets 3 input arguments as style, prompt and inputImg to be used in image generation flow but these can be extended depending on your needs. The example request body of the endpoint is just like below:


1{ 2 "style": "3d", 3 "prompt": "Astronaut on the sun", 4 "inputImg": "<BASE_64_IMG>" 5}