Eyalgut commited on
Commit
bccf6a9
1 Parent(s): 3c05c92

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ hf_token = os.environ.get("HF_TOKEN")
4
+ import spaces
5
+ from diffusers import DiffusionPipeline
6
+ from huggingface_hub import snapshot_download
7
+ import torch
8
+ import os, sys
9
+ import time
10
+
11
+ class Dummy():
12
+ pass
13
+
14
+
15
+ pipeline_path = snapshot_download(repo_id='briaai/BRIA-2.4')
16
+ sys.path.append(pipeline_path)
17
+ from ella_xl_pipeline import EllaXLPipeline
18
+
19
+ resolutions = ["1024 1024","1280 768","1344 768","768 1344","768 1280"]
20
+
21
+ # Ng
22
+ default_negative_prompt= "Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"
23
+
24
+ # Load pipeline
25
+
26
+ pipe = DiffusionPipeline.from_pretrained("briaai/BRIA-2.3", torch_dtype=torch.float16, use_safetensors=True)
27
+ pipe.load_lora_weights(f'{pipeline_path}/pytorch_lora_weights.safetensors')
28
+ pipe.fuse_lora()
29
+ pipe.unload_lora_weights()
30
+ pipe.to("cuda")
31
+ pipe = EllaXLPipeline(pipe,f'{pipeline_path}/pytorch_model.bin')
32
+ pipe.force_zeros_for_empty_prompt = False
33
+
34
+ # print("Optimizing BRIA-2.3 - this could take a while")
35
+ # t=time.time()
36
+ # pipe.unet = torch.compile(
37
+ # pipe.unet, mode="reduce-overhead", fullgraph=True # 600 secs compilation
38
+ # )
39
+ # with torch.no_grad():
40
+ # outputs = pipe(
41
+ # prompt="an apple",
42
+ # num_inference_steps=30,
43
+ # )
44
+
45
+ # # This will avoid future compilations on different shapes
46
+ # unet_compiled = torch._dynamo.run(pipe.unet)
47
+ # unet_compiled.config=pipe.unet.config
48
+ # unet_compiled.add_embedding = Dummy()
49
+ # unet_compiled.add_embedding.linear_1 = Dummy()
50
+ # unet_compiled.add_embedding.linear_1.in_features = pipe.unet.add_embedding.linear_1.in_features
51
+ # pipe.unet = unet_compiled
52
+
53
+ # print(f"Optimizing finished successfully after {time.time()-t} secs")
54
+
55
+ @spaces.GPU(enable_queue=True)
56
+ def infer(prompt,negative_prompt,seed,resolution):
57
+ print(f"""
58
+ —/n
59
+ {prompt}
60
+ """)
61
+
62
+ # generator = torch.Generator("cuda").manual_seed(555)
63
+ t=time.time()
64
+
65
+ if seed=="-1":
66
+ generator=None
67
+ else:
68
+ try:
69
+ seed=int(seed)
70
+ generator = torch.Generator("cuda").manual_seed(seed)
71
+ except:
72
+ generator=None
73
+
74
+ w,h = resolution.split()
75
+ w,h = int(w),int(h)
76
+ image = pipe(prompt,num_inference_steps=30, negative_prompt=negative_prompt,generator=generator,width=w,height=h).images[0]
77
+ print(f'gen time is {time.time()-t} secs')
78
+
79
+ # Future
80
+ # Add amound of steps
81
+ # if nsfw:
82
+ # raise gr.Error("Generated image is NSFW")
83
+
84
+ return image
85
+
86
+ css = """
87
+ #col-container{
88
+ margin: 0 auto;
89
+ max-width: 580px;
90
+ }
91
+ """
92
+ with gr.Blocks(css=css) as demo:
93
+ with gr.Column(elem_id="col-container"):
94
+ gr.Markdown("## BRIA 2.3")
95
+ gr.HTML('''
96
+ <p style="margin-bottom: 10px; font-size: 94%">
97
+ This is a demo for
98
+ <a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">BRIA 2.3 text-to-image </a>.
99
+ BRIA 2.3 improve the generation of humans and illustrations compared to BRIA 2.2 while still trained on licensed data, and so provide full legal liability coverage for copyright and privacy infringement.
100
+ </p>
101
+ ''')
102
+ with gr.Group():
103
+ with gr.Column():
104
+ prompt_in = gr.Textbox(label="Prompt", value="A smiling man with wavy brown hair and a trimmed beard")
105
+ resolution = gr.Dropdown(value=resolutions[0], show_label=True, label="Resolution", choices=resolutions)
106
+ seed = gr.Textbox(label="Seed", value=-1)
107
+ negative_prompt = gr.Textbox(label="Negative Prompt", value=default_negative_prompt)
108
+ submit_btn = gr.Button("Generate")
109
+ result = gr.Image(label="BRIA-2.3 Result")
110
+
111
+ # gr.Examples(
112
+ # examples = [
113
+ # "Dragon, digital art, by Greg Rutkowski",
114
+ # "Armored knight holding sword",
115
+ # "A flat roof villa near a river with black walls and huge windows",
116
+ # "A calm and peaceful office",
117
+ # "Pirate guinea pig"
118
+ # ],
119
+ # fn = infer,
120
+ # inputs = [
121
+ # prompt_in
122
+ # ],
123
+ # outputs = [
124
+ # result
125
+ # ]
126
+ # )
127
+
128
+ submit_btn.click(
129
+ fn = infer,
130
+ inputs = [
131
+ prompt_in,
132
+ negative_prompt,
133
+ seed,
134
+ resolution
135
+ ],
136
+ outputs = [
137
+ result
138
+ ]
139
+ )
140
+
141
+ demo.queue().launch(show_api=False)