Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,20 @@
|
|
1 |
-
import
|
2 |
-
command = 'pip install git+https://github.com/snekkenull/diffusers.git'
|
3 |
-
subprocess.run(command, shell=True)
|
4 |
-
|
5 |
import os
|
6 |
import gradio as gr
|
7 |
import torch
|
8 |
import numpy as np
|
9 |
import random
|
10 |
-
from diffusers import
|
11 |
-
import spaces
|
12 |
-
from diffusers.utils import load_image
|
13 |
-
from PIL import Image
|
14 |
-
import requests
|
15 |
-
import transformers
|
16 |
-
from transformers import AutoTokenizer, T5EncoderModel
|
17 |
from translatepy import Translator
|
18 |
-
|
|
|
|
|
19 |
|
20 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
21 |
translator = Translator()
|
22 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
23 |
# Constants
|
24 |
-
model = "
|
25 |
-
repo= "stabilityai/stable-diffusion-3-medium-diffusers"
|
26 |
MAX_SEED = np.iinfo(np.int32).max
|
27 |
|
28 |
CSS = """
|
@@ -38,143 +30,108 @@ JS = """function () {
|
|
38 |
}
|
39 |
}"""
|
40 |
|
|
|
|
|
41 |
|
42 |
-
vae = AutoencoderKL.from_pretrained(
|
43 |
-
repo,
|
44 |
-
subfolder="vae",
|
45 |
-
torch_dtype=torch.float16,
|
46 |
-
)
|
47 |
-
|
48 |
-
transformer = SD3Transformer2DModel.from_pretrained(
|
49 |
-
repo,
|
50 |
-
subfolder="transformer",
|
51 |
-
torch_dtype=torch.float16,
|
52 |
-
)
|
53 |
-
|
54 |
-
|
55 |
-
# text_encoder_3 = T5EncoderModel.from_pretrained(
|
56 |
-
# repo,
|
57 |
-
# subfolder="text_encoder_3",
|
58 |
-
# )
|
59 |
-
|
60 |
-
# tokenizer_3 = AutoTokenizer.from_pretrained(
|
61 |
-
# repo,
|
62 |
-
# subfolder="tokenizer_3",
|
63 |
-
# torch_dtype=torch.float16,
|
64 |
-
# )
|
65 |
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
pipe = StableDiffusion3Pipeline.from_pretrained(
|
70 |
-
repo,
|
71 |
-
vae=vae,
|
72 |
-
transformer=transformer,
|
73 |
-
torch_dtype=torch.float16).to("cuda")
|
74 |
-
pipe2 = StableDiffusion3Img2ImgPipeline.from_pretrained(
|
75 |
-
repo,
|
76 |
-
vae=vae,
|
77 |
-
transformer=transformer,
|
78 |
-
torch_dtype=torch.float16).to("cuda")
|
79 |
|
|
|
|
|
|
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
|
|
84 |
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
# Function
|
88 |
@spaces.GPU()
|
89 |
def generate_image(
|
90 |
-
prompt,
|
91 |
-
|
92 |
-
width=
|
93 |
-
height=1024,
|
94 |
-
scales=5,
|
95 |
-
steps=
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
|
101 |
if seed == -1:
|
102 |
seed = random.randint(0, MAX_SEED)
|
103 |
seed = int(seed)
|
104 |
print(f'prompt:{prompt}')
|
105 |
|
106 |
-
text = str(translator.translate(prompt
|
107 |
|
108 |
-
|
109 |
-
if prompt['files']:
|
110 |
-
#images = Image.open(prompt['files'][-1]).convert('RGB')
|
111 |
-
init_image = load_image(prompt['files'][-1]).resize((height, width))
|
112 |
-
else:
|
113 |
-
init_image = None
|
114 |
generator = torch.Generator().manual_seed(seed)
|
115 |
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
else:
|
129 |
-
image = pipe(
|
130 |
-
prompt=text,
|
131 |
-
negative_prompt=negative,
|
132 |
-
width=width,
|
133 |
-
height=height,
|
134 |
-
guidance_scale=scales,
|
135 |
-
num_inference_steps=steps,
|
136 |
-
generator = generator,
|
137 |
-
num_images_per_prompt = nums,
|
138 |
-
).images
|
139 |
-
|
140 |
-
print(image)
|
141 |
-
print(seed)
|
142 |
-
return image, seed
|
143 |
-
|
144 |
|
|
|
|
|
145 |
examples = [
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
[{"text": "A vibrant street wall covered in colorful graffiti, the centerpiece spells \"SD3 MEDIUM\", in a storm of colors", "files": []}],
|
151 |
-
[{"text": "photo of a young woman with long, wavy brown hair tied in a bun and glasses. She has a fair complexion and is wearing subtle makeup, emphasizing her eyes and lips. She is dressed in a black top. The background appears to be an urban setting with a building facade, and the sunlight casts a warm glow on her face.", "files": []}],
|
152 |
-
[{"text": "anime art of a steampunk inventor in their workshop, surrounded by gears, gadgets, and steam. He is holding a blue potion and a red potion, one in each hand", "files": []}],
|
153 |
-
[{"text": "photo of picturesque scene of a road surrounded by lush green trees and shrubs. The road is wide and smooth, leading into the distance. On the right side of the road, there's a blue sports car parked with the license plate spelling \"SD32B\". The sky above is partly cloudy, suggesting a pleasant day. The trees have a mix of green and brown foliage. There are no people visible in the image. The overall composition is balanced, with the car serving as a focal point.", "files": []}],
|
154 |
-
[{"text": "photo of young man in a black suit, white shirt, and black tie. He has a neatly styled haircut and is looking directly at the camera with a neutral expression. The background consists of a textured wall with horizontal lines. The photograph is in black and white, emphasizing contrasts and shadows. The man appears to be in his late twenties or early thirties, with fair skin and short, dark hair.", "files": []}],
|
155 |
-
[{"text": "photo of a woman on the beach, shot from above. She is facing the sea, while wearing a white dress. She has long blonde hair", "files": []}],
|
156 |
]
|
157 |
|
158 |
|
159 |
-
|
160 |
# Gradio Interface
|
161 |
|
162 |
-
with gr.Blocks(css=CSS, js=JS, theme="
|
163 |
-
gr.HTML("<h1><center>
|
164 |
-
gr.HTML("<p><center
|
165 |
with gr.Row():
|
166 |
with gr.Column(scale=4):
|
167 |
-
img = gr.Gallery(label='
|
168 |
-
|
|
|
|
|
169 |
with gr.Accordion("Advanced Options", open=True):
|
170 |
with gr.Column(scale=1):
|
171 |
-
negative = gr.Textbox(label="Negative prompt", value="low quality, ugly, blurry, poor face, bad anatomy")
|
172 |
width = gr.Slider(
|
173 |
label="Width",
|
174 |
minimum=512,
|
175 |
maximum=1280,
|
176 |
step=8,
|
177 |
-
value=
|
178 |
)
|
179 |
height = gr.Slider(
|
180 |
label="Height",
|
@@ -188,29 +145,21 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
|
188 |
minimum=3.5,
|
189 |
maximum=7,
|
190 |
step=0.1,
|
191 |
-
value=5,
|
192 |
)
|
193 |
steps = gr.Slider(
|
194 |
label="Steps",
|
195 |
minimum=1,
|
196 |
-
maximum=
|
197 |
step=1,
|
198 |
-
value=
|
199 |
-
)
|
200 |
-
strength = gr.Slider(
|
201 |
-
label="Strength",
|
202 |
-
minimum=0.0,
|
203 |
-
maximum=1.0,
|
204 |
-
step=0.1,
|
205 |
-
value=0.7,
|
206 |
)
|
207 |
seed = gr.Slider(
|
208 |
-
label="
|
209 |
minimum=-1,
|
210 |
maximum=MAX_SEED,
|
211 |
step=1,
|
212 |
value=-1,
|
213 |
-
scale=2,
|
214 |
)
|
215 |
nums = gr.Slider(
|
216 |
label="Image Numbers",
|
@@ -218,21 +167,57 @@ with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
|
218 |
maximum=4,
|
219 |
step=1,
|
220 |
value=1,
|
221 |
-
|
222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
gr.Examples(
|
224 |
examples=examples,
|
225 |
-
inputs=prompt,
|
226 |
-
|
227 |
-
fn=generate_image,
|
228 |
-
cache_examples="lazy",
|
229 |
examples_per_page=4,
|
230 |
)
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
|
237 |
|
238 |
demo.queue().launch()
|
|
|
1 |
+
import spaces
|
|
|
|
|
|
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
import numpy as np
|
6 |
import random
|
7 |
+
from diffusers import FluxPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from translatepy import Translator
|
9 |
+
from huggingface_hub import hf_hub_download
|
10 |
+
import requests
|
11 |
+
import re
|
12 |
|
13 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
14 |
translator = Translator()
|
15 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
16 |
# Constants
|
17 |
+
model = "black-forest-labs/FLUX.1-dev"
|
|
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
|
20 |
CSS = """
|
|
|
30 |
}
|
31 |
}"""
|
32 |
|
33 |
+
if torch.cuda.is_available():
|
34 |
+
pipe = FluxPipeline.from_pretrained(model, torch_dtype=torch.bfloat16)
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
def scrape_lora_link(url):
|
38 |
+
try:
|
39 |
+
# Send a GET request to the URL
|
40 |
+
response = requests.get(url)
|
41 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
42 |
|
43 |
+
# Get the content of the page
|
44 |
+
content = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Use regular expression to find the link
|
47 |
+
pattern = r'href="(.*?lora.*?\.safetensors\?download=true)"'
|
48 |
+
match = re.search(pattern, content)
|
49 |
|
50 |
+
if match:
|
51 |
+
safetensors_url = match.group(1)
|
52 |
+
filename = safetensors_url.split('/')[-1].split('?')[0] # Extract the filename from the URL
|
53 |
+
return filename
|
54 |
+
else:
|
55 |
+
return None
|
56 |
|
57 |
+
except requests.RequestException as e:
|
58 |
+
print(f"An error occurred while fetching the URL: {e}")
|
59 |
+
return None
|
60 |
|
61 |
+
def enable_lora(lora_scale,lora_add):
|
62 |
+
if not lora_add:
|
63 |
+
gradio.Info("No Lora Loaded, Use basemodel", duration=5)
|
64 |
+
else:
|
65 |
+
url = f'https://huggingface.co/{lora_add}/tree/main'
|
66 |
+
lora_name = scrape_lora_link(url)
|
67 |
+
pipe.load_lora_weights(lora_add, weight_name=lora_name)
|
68 |
+
pipe.fuse_lora(lora_scale=lora_scale)
|
69 |
+
gradio.Info(f"{lora_add} Loaded", duration=5)
|
70 |
|
|
|
71 |
@spaces.GPU()
|
72 |
def generate_image(
|
73 |
+
prompt:str,
|
74 |
+
lora_word:str,
|
75 |
+
width:int=768,
|
76 |
+
height:int=1024,
|
77 |
+
scales:float=3.5,
|
78 |
+
steps:int=24,
|
79 |
+
seed:int=-1,
|
80 |
+
nums:int=1):
|
81 |
+
|
82 |
+
pipe.to(device="cuda")
|
83 |
|
84 |
if seed == -1:
|
85 |
seed = random.randint(0, MAX_SEED)
|
86 |
seed = int(seed)
|
87 |
print(f'prompt:{prompt}')
|
88 |
|
89 |
+
text = str(translator.translate(prompt, 'English')) + "," + lora_word
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
generator = torch.Generator().manual_seed(seed)
|
92 |
|
93 |
|
94 |
+
image = pipe(
|
95 |
+
prompt=text,
|
96 |
+
height=height,
|
97 |
+
width=width,
|
98 |
+
guidance_scale=scales,
|
99 |
+
output_type="pil",
|
100 |
+
num_inference_steps=steps,
|
101 |
+
max_sequence_length=512,
|
102 |
+
num_images_per_prompt=nums,
|
103 |
+
generator=generator,
|
104 |
+
).images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
return image, seed
|
107 |
+
|
108 |
examples = [
|
109 |
+
["close up portrait, Amidst the interplay of light and shadows in a photography studio,a soft spotlight traces the contours of a face,highlighting a figure clad in a sleek black turtleneck. The garment,hugging the skin with subtle luxury,complements the Caucasian model's understated makeup,embodying minimalist elegance. Behind,a pale gray backdrop extends,its fine texture shimmering subtly in the dim light,artfully balancing the composition and focusing attention on the subject. In a palette of black,gray,and skin tones,simplicity intertwines with profundity,as every detail whispers untold stories.",0.9,"Shakker-Labs/AWPortrait-FL",""],
|
110 |
+
["Caucasian,The image features a young woman of European descent standing in an studio setting,surrounded by silk. (She is wearing a silk dress),paired with a bold. Her brown hair is wet and tousled,falling naturally around her face,giving her a raw and edgy look. The woman has an intense and direct gaze,adding to the dramatic feel of the image. The backdrop is flowing silk,big silk. The overall composition blends elements of fashion and nature,creating a striking and powerful visual",0.9,"Shakker-Labs/AWPortrait-FL".""],
|
111 |
+
["A black and white portrait of a young woman with a captivating gaze. She's bundled up in a cozy black sweater,hands gently cupped near her face. The monochromatic tones highlight her delicate features and the contemplative mood of the image",0.9,"Shakker-Labs/AWPortrait-FL",""],
|
112 |
+
["Fashion photography portrait,close up portrait,(a woman of European descent is surrounded by lava rock and magma from head to neck, red magma hair, wear volcanic lava rock magma outfit coat lava rock magma fashion costume with ruffled layers",0.9,"Shakker-Labs/AWPortrait-FL",""]
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
]
|
114 |
|
115 |
|
|
|
116 |
# Gradio Interface
|
117 |
|
118 |
+
with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
|
119 |
+
gr.HTML("<h1><center>Flux Labs</center></h1>")
|
120 |
+
gr.HTML("<p><center>Choose the LoRA model on the right menu</center></p>")
|
121 |
with gr.Row():
|
122 |
with gr.Column(scale=4):
|
123 |
+
img = gr.Gallery(label='flux Generated Image', columns = 1, preview=True, height=600)
|
124 |
+
with gr.Row():
|
125 |
+
prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
|
126 |
+
sendBtn = gr.Button(scale=1, variant='primary')
|
127 |
with gr.Accordion("Advanced Options", open=True):
|
128 |
with gr.Column(scale=1):
|
|
|
129 |
width = gr.Slider(
|
130 |
label="Width",
|
131 |
minimum=512,
|
132 |
maximum=1280,
|
133 |
step=8,
|
134 |
+
value=768,
|
135 |
)
|
136 |
height = gr.Slider(
|
137 |
label="Height",
|
|
|
145 |
minimum=3.5,
|
146 |
maximum=7,
|
147 |
step=0.1,
|
148 |
+
value=3.5,
|
149 |
)
|
150 |
steps = gr.Slider(
|
151 |
label="Steps",
|
152 |
minimum=1,
|
153 |
+
maximum=100,
|
154 |
step=1,
|
155 |
+
value=24,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
)
|
157 |
seed = gr.Slider(
|
158 |
+
label="Seeds",
|
159 |
minimum=-1,
|
160 |
maximum=MAX_SEED,
|
161 |
step=1,
|
162 |
value=-1,
|
|
|
163 |
)
|
164 |
nums = gr.Slider(
|
165 |
label="Image Numbers",
|
|
|
167 |
maximum=4,
|
168 |
step=1,
|
169 |
value=1,
|
170 |
+
)
|
171 |
+
lora_scale = gr.Slider(
|
172 |
+
label="LoRA Scale",
|
173 |
+
minimum=0.1,
|
174 |
+
maximum=1.0,
|
175 |
+
step=0.1,
|
176 |
+
value=1.0,
|
177 |
+
)
|
178 |
+
lora_add = gr.Textbox(
|
179 |
+
label="Add Flux LoRA",
|
180 |
+
info="Copy the HF LoRA model name here",
|
181 |
+
lines=1,
|
182 |
+
value="Shakker-Labs/AWPortrait-FL",
|
183 |
+
)
|
184 |
+
lora_word = gr.Textbox(
|
185 |
+
label="Add Flux LoRA Trigger Word",
|
186 |
+
info="Add the Trigger Word",
|
187 |
+
lines=1,
|
188 |
+
value="",
|
189 |
+
)
|
190 |
+
load_lora = gr.Button(value="Load LoRA", variant='secondary')
|
191 |
+
|
192 |
gr.Examples(
|
193 |
examples=examples,
|
194 |
+
inputs=[prompt,lora_scale,lora_add,lora_word],
|
195 |
+
cache_examples=False,
|
|
|
|
|
196 |
examples_per_page=4,
|
197 |
)
|
198 |
|
199 |
+
load_lora.click(fn=enable_lora, inputs=[lora_scale,lora_add])
|
200 |
+
|
201 |
+
gr.on(
|
202 |
+
triggers=[
|
203 |
+
prompt.submit,
|
204 |
+
sendBtn.click,
|
205 |
+
],
|
206 |
+
fn=generate_image,
|
207 |
+
inputs=[
|
208 |
+
prompt,
|
209 |
+
lora_word,
|
210 |
+
width,
|
211 |
+
height,
|
212 |
+
scales,
|
213 |
+
steps,
|
214 |
+
seed,
|
215 |
+
nums
|
216 |
+
],
|
217 |
+
outputs=[img, seed],
|
218 |
+
api_name="run",
|
219 |
+
)
|
220 |
+
|
221 |
|
222 |
|
223 |
demo.queue().launch()
|