File size: 4,745 Bytes
e7b3cb7
 
 
 
 
 
 
 
 
3205c32
e7b3cb7
 
 
b9382ad
e7b3cb7
 
3205c32
b9382ad
e7b3cb7
 
 
 
 
3205c32
 
b9382ad
 
3205c32
b9382ad
29455bd
b9382ad
 
29455bd
b9382ad
d3ff7d8
c70e7b9
29455bd
c70e7b9
d3ff7d8
c70e7b9
29455bd
c70e7b9
d3ff7d8
3205c32
e7b3cb7
 
 
 
 
 
b9382ad
 
 
3205c32
 
 
 
 
 
 
 
 
b9382ad
e7b3cb7
b9382ad
e7b3cb7
3205c32
 
e7b3cb7
 
 
 
 
 
 
 
 
 
 
3205c32
e7b3cb7
 
 
 
 
 
 
 
 
 
 
3c3b193
e7b3cb7
 
 
 
3c3b193
e7b3cb7
 
 
 
 
 
 
 
 
3205c32
e7b3cb7
d3ff7d8
 
29455bd
3205c32
 
a735354
 
3205c32
e7b3cb7
3205c32
 
8b393ee
e7b3cb7
 
e6cf8cf
 
 
 
e7b3cb7
 
3205c32
 
b9382ad
 
e7b3cb7
3205c32
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import gradio as gr
import numpy as np
import random
from diffusers import DiffusionPipeline
import torch
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"

LORA=False

if torch.cuda.is_available():
    torch.cuda.max_memory_allocated(device=device)
    pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
    pipe.enable_xformers_memory_efficient_attention()
    pipe = pipe.to(device)
else: 
    pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_safetensors=True)
    pipe = pipe.to(device)

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024





def infer(prompt, negative_prompt, seed, randomize_seed, image_style,cfg,lora_scale):
    pipe.unload_lora_weights()
    if image_style == "No style":
        pipe.unload_lora_weights()
        
    elif image_style == "style_detailed":
        pipe.load_lora_weights("ohkarim/LoRA_logos", weight_name="OH_logos.safetensors", adapter_name="OH_logos")
        prompt=prompt+", detailed, close up, unique background color, OH_logos"
        
    elif image_style == "style_blacknwhite":
        pipe.load_lora_weights("ohkarim/lora_logo_blacknwhite", weight_name="bel_blacknwhite_lora.safetensors", adapter_name="bel_blacknwhite")
        prompt=prompt+", black and white, balck and white, minimalist, unique background color, bel_blacknwhite"

    elif image_style == "style_cartoon":
        pipe.load_lora_weights("ohkarim/lora_logo_modern", weight_name="lora_modern.safetensors", adapter_name="oh_bel_modern")
        prompt=prompt+", simple, modern, unique background color, oh_bel_modern"


    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
        
    generator = torch.Generator().manual_seed(seed)
    
    griddd = []
    for _ in range(4):
        image = pipe(
            prompt = prompt, 
            negative_prompt = negative_prompt,
            cross_attention_kwargs={"scale": lora_scale},
            guidance_scale = cfg, 
            num_inference_steps = 20, 
            width = 512, 
            height = 512,
            generator = generator
        ).images[0] 
        griddd.append(image)
    
    return griddd


css="""
#col-container {
    margin: 0 auto;
    max-width: 520px;
}
"""

if torch.cuda.is_available():
    power_device = "GPU"
else:
    power_device = "CPU"


# Function to read CSS from file
def read_css_from_file(filename):
    with open(filename, "r") as file:
        return file.read()

# Read CSS from file
css = read_css_from_file("style.css")

PTI_SD_DESCRIPTION = '''
<div id="content_align">
  <span style="color:darkred;font-size:32px;font-weight:bold">  
    Create your own logo now!
  </span>
</div>
<div id="content_align">
  <span style="color:blue;font-size:16px;font-weight:bold">  
    There is 3 styles so far, detailed, black & white and cartoon
  </span>
</div>
<div id="content_align" style="margin-top: 10px;">
</div>
'''

# Creating Gradio interface
with gr.Blocks(css=css) as demo:
    gr.Markdown(PTI_SD_DESCRIPTION)
    with gr.Row():   
        with gr.Column():
            text_prompt = gr.Textbox(label="Input Prompt", placeholder="Example: logo of a coffee shop, cup of coffee, mug, brown shades ", lines=2)
            negative_prompt = gr.Textbox(label="Negative Prompt (optional)", placeholder="Example: blurry, unfocused, complicated", lines=2)
            image_style = gr.Dropdown(label="Choose a style", choices=["No style", "style_detailed", "style_blacknwhite", "style_cartoon"], value="No LoRA")

        with gr.Column():
            cfg = gr.Slider(label="Prompt guidance", minimum=1, maximum=20, step=1, value=4) 
            lora_scale = gr.Slider(label="Style guidance", minimum=0, maximum=2, step=0.01, value=0.9) 
            seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0) 
            
            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
                      
    generate_button = gr.Button("Generate Image", variant='primary')
        
    with gr.Row():
        output_image1 = gr.Image(type="pil", label="Image 1", width=256, height=400)
        output_image2 = gr.Image(type="pil", label="Image 2", width=256, height=400)  
        output_image3 = gr.Image(type="pil", label="Image 3", width=256, height=400)
        output_image4 = gr.Image(type="pil", label="Image 4", width=256, height=400)

    generate_button.click(
        fn = infer,
        inputs = [text_prompt, negative_prompt, seed, randomize_seed,image_style,cfg,lora_scale],
        outputs = [output_image1,output_image2,output_image3,output_image4],
        show_progress=True
    )
demo.queue().launch()