Eyalgut commited on
Commit
b611dce
1 Parent(s): 66e6266

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ hf_token = os.environ.get("HF_TOKEN")
4
+ import spaces
5
+ from diffusers import DiffusionPipeline, UNet2DConditionModel, LCMScheduler, AutoencoderKL
6
+ import torch
7
+ import time
8
+
9
+ class Dummy():
10
+ pass
11
+
12
+ resolutions = ["1024 1024","1280 768","1344 768","768 1344","768 1280" ]
13
+
14
+ # Load pipeline
15
+
16
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
17
+ pipe = DiffusionPipeline.from_pretrained("briaai/BRIA-2.3", torch_dtype=torch.float16, vae=vae)
18
+ pipe.load_lora_weights("briaai/BRIA-2.3-FAST-LORA")
19
+ pipe.fuse_lora()
20
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
21
+ pipe.to('cuda')
22
+ del vae
23
+
24
+
25
+ pipe.force_zeros_for_empty_prompt = False
26
+
27
+ # print("Optimizing BRIA 2.3 FAST LORA - this could take a while")
28
+ # t=time.time()
29
+ # pipe.unet = torch.compile(
30
+ # pipe.unet, mode="reduce-overhead", fullgraph=True # 600 secs compilation
31
+ # )
32
+ # with torch.no_grad():
33
+ # outputs = pipe(
34
+ # prompt="an apple",
35
+ # num_inference_steps=8,
36
+ # )
37
+
38
+ # # This will avoid future compilations on different shapes
39
+ # unet_compiled = torch._dynamo.run(pipe.unet)
40
+ # unet_compiled.config=pipe.unet.config
41
+ # unet_compiled.add_embedding = Dummy()
42
+ # unet_compiled.add_embedding.linear_1 = Dummy()
43
+ # unet_compiled.add_embedding.linear_1.in_features = pipe.unet.add_embedding.linear_1.in_features
44
+ # pipe.unet = unet_compiled
45
+
46
+ # print(f"Optimizing finished successfully after {time.time()-t} secs")
47
+
48
+ @spaces.GPU(enable_queue=True)
49
+ def infer(prompt,seed,resolution):
50
+ print(f"""
51
+ —/n
52
+ {prompt}
53
+ """)
54
+
55
+ # generator = torch.Generator("cuda").manual_seed(555)
56
+ t=time.time()
57
+
58
+ if seed=="-1":
59
+ generator=None
60
+ else:
61
+ try:
62
+ seed=int(seed)
63
+ generator = torch.Generator("cuda").manual_seed(seed)
64
+ except:
65
+ generator=None
66
+
67
+ w,h = resolution.split()
68
+ w,h = int(w),int(h)
69
+ image = pipe(prompt,num_inference_steps=8,generator=generator,width=w,height=h,guidance_scale=0).images[0]
70
+ print(f'gen time is {time.time()-t} secs')
71
+
72
+ # Future
73
+ # Add amound of steps
74
+ # if nsfw:
75
+ # raise gr.Error("Generated image is NSFW")
76
+
77
+ return image
78
+
79
+ css = """
80
+ #col-container{
81
+ margin: 0 auto;
82
+ max-width: 580px;
83
+ }
84
+ """
85
+ with gr.Blocks(css=css) as demo:
86
+ with gr.Column(elem_id="col-container"):
87
+ gr.Markdown("## BRIA 2.3 FAST LORA")
88
+ gr.HTML('''
89
+ <p style="margin-bottom: 10px; font-size: 94%">
90
+ This is a demo for
91
+ <a href="https://huggingface.co/briaai/BRIA-2.3-FAST-LORA" target="_blank">BRIA 2.3 FAST LORA </a>.
92
+ This is a fast version of BRIA 2.3 text-to-image model, still trained on licensed data, and so provides full legal liability coverage for copyright and privacy infringement.
93
+ You can also try it for free in our <a href="https://labs.bria.ai/" target="_blank">webapp demo </a>.
94
+ Are you a startup or a student? We encourage you to apply for our
95
+ <a href="https://pages.bria.ai/the-visual-generative-ai-platform-for-builders-startups-plan?_gl=1*cqrl81*_ga*MTIxMDI2NzI5OC4xNjk5NTQ3MDAz*_ga_WRN60H46X4*MTcwOTM5OTMzNC4yNzguMC4xNzA5Mzk5MzM0LjYwLjAuMA..) target="_blank">Startup Plan </a>
96
+ This program are designed to support emerging businesses and academic pursuits with our cutting-edge technology.
97
+ </p>
98
+ ''')
99
+ with gr.Group():
100
+ with gr.Column():
101
+ prompt_in = gr.Textbox(label="Prompt", value="A smiling man with wavy brown hair and a trimmed beard")
102
+ resolution = gr.Dropdown(value=resolutions[0], show_label=True, label="Resolution", choices=resolutions)
103
+ seed = gr.Textbox(label="Seed", value=-1)
104
+ submit_btn = gr.Button("Generate")
105
+ result = gr.Image(label="BRIA 2.3 FAST LORA Result")
106
+
107
+ # gr.Examples(
108
+ # examples = [
109
+ # "Dragon, digital art, by Greg Rutkowski",
110
+ # "Armored knight holding sword",
111
+ # "A flat roof villa near a river with black walls and huge windows",
112
+ # "A calm and peaceful office",
113
+ # "Pirate guinea pig"
114
+ # ],
115
+ # fn = infer,
116
+ # inputs = [
117
+ # prompt_in
118
+ # ],
119
+ # outputs = [
120
+ # result
121
+ # ]
122
+ # )
123
+
124
+ submit_btn.click(
125
+ fn = infer,
126
+ inputs = [
127
+ prompt_in,
128
+ seed,
129
+ resolution
130
+ ],
131
+ outputs = [
132
+ result
133
+ ]
134
+ )
135
+
136
+ demo.queue().launch(show_api=False)