Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
hf_token = os.environ.get("HF_TOKEN")
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
from pipeline_bria import BriaPipeline
|
7 |
+
import time
|
8 |
+
|
9 |
+
resolutions = ["1024 1024","1296,800","1360 768","768 1360","800 1296"]
|
10 |
+
|
11 |
+
# Ng
|
12 |
+
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"
|
13 |
+
|
14 |
+
# Load pipeline
|
15 |
+
# trust_remote_code = True - allows loading a transformer which is not present at the transformers library(from transformer/bria_transformer.py)
|
16 |
+
pipe = BriaPipeline.from_pretrained("briaai/BRIA-3.0-TOUCAN", torch_dtype=torch.bfloat16,trust_remote_code=True)
|
17 |
+
pipe.to(device="cuda")
|
18 |
+
|
19 |
+
@spaces.GPU(enable_queue=True)
|
20 |
+
def infer(prompt,negative_prompt,seed,resolution):
|
21 |
+
print(f"""
|
22 |
+
—/n
|
23 |
+
{prompt}
|
24 |
+
""")
|
25 |
+
|
26 |
+
# generator = torch.Generator("cuda").manual_seed(555)
|
27 |
+
t=time.time()
|
28 |
+
|
29 |
+
if seed=="-1":
|
30 |
+
generator=None
|
31 |
+
else:
|
32 |
+
try:
|
33 |
+
seed=int(seed)
|
34 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
35 |
+
except:
|
36 |
+
generator=None
|
37 |
+
|
38 |
+
w,h = resolution.split()
|
39 |
+
w,h = int(w),int(h)
|
40 |
+
image = pipe(prompt,num_inference_steps=30, negative_prompt=negative_prompt,generator=generator,width=w,height=h).images[0]
|
41 |
+
print(f'gen time is {time.time()-t} secs')
|
42 |
+
|
43 |
+
# Future
|
44 |
+
# Add amound of steps
|
45 |
+
# if nsfw:
|
46 |
+
# raise gr.Error("Generated image is NSFW")
|
47 |
+
|
48 |
+
return image
|
49 |
+
|
50 |
+
css = """
|
51 |
+
#col-container{
|
52 |
+
margin: 0 auto;
|
53 |
+
max-width: 580px;
|
54 |
+
}
|
55 |
+
"""
|
56 |
+
with gr.Blocks(css=css) as demo:
|
57 |
+
with gr.Column(elem_id="col-container"):
|
58 |
+
gr.Markdown("## BRIA 2.3")
|
59 |
+
gr.HTML('''
|
60 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
61 |
+
This is a demo for
|
62 |
+
<a href="https://huggingface.co/briaai/BRIA-2.3" target="_blank">BRIA 2.3 text-to-image </a>.
|
63 |
+
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.
|
64 |
+
</p>
|
65 |
+
''')
|
66 |
+
with gr.Group():
|
67 |
+
with gr.Column():
|
68 |
+
prompt_in = gr.Textbox(label="Prompt", value="A smiling man with wavy brown hair and a trimmed beard")
|
69 |
+
resolution = gr.Dropdown(value=resolutions[0], show_label=True, label="Resolution", choices=resolutions)
|
70 |
+
seed = gr.Textbox(label="Seed", value=-1)
|
71 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value=default_negative_prompt)
|
72 |
+
submit_btn = gr.Button("Generate")
|
73 |
+
result = gr.Image(label="BRIA-2.3 Result")
|
74 |
+
|
75 |
+
# gr.Examples(
|
76 |
+
# examples = [
|
77 |
+
# "Dragon, digital art, by Greg Rutkowski",
|
78 |
+
# "Armored knight holding sword",
|
79 |
+
# "A flat roof villa near a river with black walls and huge windows",
|
80 |
+
# "A calm and peaceful office",
|
81 |
+
# "Pirate guinea pig"
|
82 |
+
# ],
|
83 |
+
# fn = infer,
|
84 |
+
# inputs = [
|
85 |
+
# prompt_in
|
86 |
+
# ],
|
87 |
+
# outputs = [
|
88 |
+
# result
|
89 |
+
# ]
|
90 |
+
# )
|
91 |
+
|
92 |
+
submit_btn.click(
|
93 |
+
fn = infer,
|
94 |
+
inputs = [
|
95 |
+
prompt_in,
|
96 |
+
negative_prompt,
|
97 |
+
seed,
|
98 |
+
resolution
|
99 |
+
],
|
100 |
+
outputs = [
|
101 |
+
result
|
102 |
+
]
|
103 |
+
)
|
104 |
+
|
105 |
+
demo.queue().launch(show_api=False)
|