Spaces:
Runtime error
Runtime error
import os | |
import io | |
import IPython.display | |
from PIL import Image | |
import base64 | |
from diffusers import DiffusionPipeline | |
hf_api_key = "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" | |
from diffusers import DiffusionPipeline | |
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5") | |
def get_completion(prompt): | |
return pipeline(prompt).images[0] | |
import gradio as gr | |
#A helper function to convert the PIL image to base64 | |
#so you can send it to the API | |
#A helper function to convert the PIL image to base64 | |
# so you can send it to the API | |
def base64_to_pil(img_base64): | |
base64_decoded = base64.b64decode(img_base64) | |
byte_stream = io.BytesIO(base64_decoded) | |
pil_image = Image.open(byte_stream) | |
return pil_image | |
def generate(prompt, negative_prompt, steps, guidance, width, height): | |
params = { | |
"negative_prompt": negative_prompt, | |
"num_inference_steps": steps, | |
"guidance_scale": guidance, | |
"width": width, | |
"height": height | |
} | |
output = get_completion(prompt, params) | |
pil_image = base64_to_pil(output) | |
return pil_image | |
gr.close_all() | |
with gr.Blocks() as demo: | |
gr.Markdown("# Image Generation with Stable Diffusion") | |
with gr.Row(): | |
with gr.Column(scale=4): | |
prompt = gr.Textbox(label="Your prompt") #Give prompt some real estate | |
with gr.Column(scale=1, min_width=50): | |
btn = gr.Button("Submit") #Submit button side by side! | |
with gr.Accordion("Advanced options", open=False): #Let's hide the advanced options! | |
negative_prompt = gr.Textbox(label="Negative prompt") | |
with gr.Row(): | |
with gr.Column(): | |
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=100, value=25, | |
info="In many steps will the denoiser denoise the image?") | |
guidance = gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7, | |
info="Controls how much the text prompt influences the result") | |
with gr.Column(): | |
width = gr.Slider(label="Width", minimum=64, maximum=512, step=64, value=512) | |
height = gr.Slider(label="Height", minimum=64, maximum=512, step=64, value=512) | |
output = gr.Image(label="Result") #Move the output up too | |
btn.click(fn=generate, inputs=[prompt,negative_prompt,steps,guidance,width,height], outputs=[output]) | |
gr.close_all() | |
demo.launch() |