Connection timeout encounetered in streamlit app deployed using azure App services


Hi Team,
I have deployed a streamlit app for text to image using stable diffusion models to Azure using azure app services. The streamlit app got deployed successfully. But when executing the app, during the initialization of the diffusion model I encounter a connection timeout.
Could you please help me with a resolution. Please find the code below.
Folder structure

folderstructure

import streamlit as st
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import torch
import time
#
seed = 42
state = None
current_steps = 25
#
device = "cuda" if torch.cuda.is_available() else "cpu"
#
generator = torch.Generator(device).manual_seed(seed)
#
def update_state(new_state):
  global state
  state = new_state
#
def pipe_callback(step: int, timestep: int, latents: torch.FloatTensor):
    update_state(f"{step}/{current_steps} steps")#\nTime left, sec: {timestep/100:.0f}")
#
#@st.experimental_singleton
def init_diffusers():
    model_id = "stable-diffusion-v2"
    scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if device == "cuda":
        pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
    else:
        pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler)
    return pipe.to(device)

#diffusers = init_diffusers()


st.write("""
## ⚡️ Generate Image from Text ⚡️
""")

# Take a text input
prompt = st.text_input("Enter your text:")
st.sidebar.header("Parameters for the model :")
neg_prompt = st.sidebar.text_input("Enter What to be excluded from images separated by commas :",value="cartoonish, ugly, deformed,3d, animation, portraits")
guidance = st.sidebar.slider("Select a guidance scale :",min_value=7.5,max_value=15.0,value=7.5,step=0.1)

if prompt != "":
    with st.spinner(text="Initializing Diffusers...It might take a while..."):
        diffusers = init_diffusers()
    with st.spinner(text="Generating image...It might take a while..."):
        image = diffusers(prompt,
                          negative_prompt= neg_prompt,
                          generator= generator,
                          guidance_scale=guidance,
                          callback=pipe_callback
                          ).images[0]
          

    with st.spinner(text="Image Generated 🚀🚀🚀"):
        st.image(image, use_column_width=True)

What version of Python are you running?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.