File size: 2,987 Bytes
66a73ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a568f
66a73ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
import torch
from PIL import Image

def text_to_image(prompt, negative_prompt, guidance_scale, num_inference_steps):
    pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
    image = pipe(prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images[0]
    return image

def image_to_image(prompt, negative_prompt, init_image, strength, guidance_scale, num_inference_steps):
    pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
    init_image = init_image.convert("RGB").resize((512, 512))
    image = pipe(prompt, negative_prompt=negative_prompt, init_image=init_image, strength=strength, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images[0]
    return image

with gr.Blocks(theme='Respair/[email protected]') as demo:
    gr.Markdown("# Text-to-Image and Image-to-Image")

    with gr.Tab("Text-to-Image"):
        with gr.Row():
            text_prompt = gr.Textbox(label="Prompt", placeholder="Enter your text here...")
            text_negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter what to avoid...")
        with gr.Row():
            guidance_scale = gr.Slider(1, 20, value=7.5, step=0.1, label="Guidance Scale")
            num_inference_steps = gr.Slider(10, 100, value=50, step=1, label="Inference Steps")
        with gr.Row():
            generate_btn = gr.Button("Generate", elem_classes=["primary-button"])
        with gr.Row():
            text_output = gr.Image(label="Generated Image")
        
        generate_btn.click(text_to_image, inputs=[text_prompt, text_negative_prompt, guidance_scale, num_inference_steps], outputs=text_output)

    with gr.Tab("Image-to-Image"):
        with gr.Row():
            init_image = gr.Image(source="upload", tool="editor", type="pil", label="Initial Image")
        with gr.Row():
            img_prompt = gr.Textbox(label="Prompt", placeholder="Describe modifications...")
            img_negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter what to avoid...")
        with gr.Row():
            strength = gr.Slider(0.1, 1.0, value=0.75, step=0.05, label="Strength")
            img_guidance_scale = gr.Slider(1, 20, value=7.5, step=0.1, label="Guidance Scale")
            img_num_inference_steps = gr.Slider(10, 100, value=50, step=1, label="Inference Steps")
        with gr.Row():
            img_generate_btn = gr.Button("Generate", elem_classes=["primary-button"])
        with gr.Row():
            img_output = gr.Image(label="Modified Image")

        img_generate_btn.click(image_to_image, inputs=[img_prompt, img_negative_prompt, init_image, strength, img_guidance_scale, img_num_inference_steps], outputs=img_output)

demo.launch(share=True)