Krebzonide's picture
Update app.py
00b3c84
raw
history blame contribute delete
No virus
2.45 kB
#Downloading other people's code so we don't have to write it ourselves
from diffusers import StableDiffusionPipeline
import gradio as gr
import torch
pipe = StableDiffusionPipeline.from_pretrained( #Downloading the model and naming it "pipe"
"Lykon/dreamshaper-8", #Name of the model we will download
requires_safety_checker=False, safety_checker=None) #Disables the safety checker
pipe.to("cuda") #Move the model to the GPU because it's faster than the CPU
def generate( #Creates a function called "generate" that takes creates the image using the pipeline
prompt, negative_prompt, sampling_steps, guidance_scale, #These are the parameters that control image generation
progress=gr.Progress(track_tqdm=True)): #Shows you a progress bar as your image is generated
image = pipe( #The function that uses the pipeline to generate images
prompt, 768, negative_prompt=negative_prompt, num_inference_steps=sampling_steps, guidance_scale=guidance_scale #The parameters that the pipeline will use
).images[0] #Separates the image from the other data returned
return image #Sends the image back to whoever called this function
with gr.Blocks() as demo: #Creates a page on a website that you can look at and click on
with gr.Column(): #Creates a column
prompt = gr.Textbox(label="Prompt") #A text box so you can type in the prompt
negative_prompt = gr.Textbox(label="Negative Prompt") #A text box so you can type in the negative prompt
with gr.Row(): #Creates a row
submit_btn = gr.Button("Generate") #A button for you to click to generate images
samp_steps = gr.Slider(1, 50, value=25, step=1, label="Sampling steps") #A slider to input the sampling steps you want
guide_scale = gr.Slider(1, 10, value=6, step=0.5, label="Guidance scale") #A slider to input the guidance scale you want
image = gr.Image(label="Generated images") #A gallery displays images
submit_btn.click( #When you click the button this will run
generate, #Runs the generate function
[prompt, negative_prompt, samp_steps, guide_scale], #Passes the value of these objects as inputs to the function
[image], #Gives the output of the function to this object
queue=True) #Allows users to wait in a queue until the GPU isn't busy
demo.launch(debug=True) #Tells the program to run the page so that you can look at it and click on it