Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py
#1
by
John6666
- opened
app.py
CHANGED
|
@@ -1,33 +1,274 @@
|
|
| 1 |
-
from datetime import datetime
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
|
|
|
| 5 |
import torch
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
fn=generate_image,
|
| 29 |
-
inputs=gr.Text(label="Prompt"),
|
| 30 |
-
outputs=gr.Gallery(),
|
| 31 |
-
examples=["A cat playing with a ball of yarn"],
|
| 32 |
-
cache_examples=False,
|
| 33 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
import torch
|
| 5 |
+
import spaces
|
| 6 |
+
import re
|
| 7 |
+
import os
|
| 8 |
+
from diffusers import (
|
| 9 |
+
DiffusionPipeline,
|
| 10 |
+
AutoencoderTiny,
|
| 11 |
+
)
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
+
#from feifeilib.feifeichat import feifeichat
|
| 14 |
+
|
| 15 |
+
IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
|
| 16 |
+
IS_GPU_MODE = True if IS_ZERO_GPU else (True if torch.cuda.is_available() else False)
|
| 17 |
+
if IS_ZERO_GPU:
|
| 18 |
+
import subprocess
|
| 19 |
+
subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
|
| 20 |
+
torch.set_float32_matmul_precision("high")
|
| 21 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 22 |
+
|
| 23 |
+
import config
|
| 24 |
+
styles_name = [style["name"] for style in config.style_list]
|
| 25 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 26 |
+
MAX_IMAGE_SIZE = 2240
|
| 27 |
+
|
| 28 |
+
def feifeimodload():
|
| 29 |
+
|
| 30 |
+
dtype = torch.bfloat16
|
| 31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
+
|
| 33 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 34 |
+
"aifeifei798/DarkIdol-flux-v1", torch_dtype=dtype
|
| 35 |
+
).to(device)
|
| 36 |
+
|
| 37 |
+
pipe.load_lora_weights(
|
| 38 |
+
hf_hub_download("aifeifei798/feifei-flux-lora-v1.1", "feifei-v1.1.safetensors"),
|
| 39 |
+
adapter_name="feifei",
|
| 40 |
+
)
|
| 41 |
+
pipe.load_lora_weights(
|
| 42 |
+
hf_hub_download("aifeifei798/sldr_flux_nsfw_v2-studio", "sldr_flux_nsfw_v2-studio.safetensors"),
|
| 43 |
+
adapter_name="sldr_flux_nsfw_v2",
|
| 44 |
+
)
|
| 45 |
|
| 46 |
+
pipe.load_lora_weights(
|
| 47 |
+
hf_hub_download(
|
| 48 |
+
"aifeifei798/big-boobs-clothed",
|
| 49 |
+
"big-boobs-clothed-v2.safetensors",
|
| 50 |
+
),
|
| 51 |
+
adapter_name="big-boobs-clothed-v2",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
pipe.vae.enable_slicing()
|
| 55 |
+
pipe.vae.enable_tiling()
|
| 56 |
+
torch.cuda.empty_cache()
|
| 57 |
+
return pipe
|
| 58 |
+
|
| 59 |
+
pipe = feifeimodload()
|
| 60 |
|
| 61 |
+
if IS_ZERO_GPU:
|
| 62 |
+
from optimization import optimize_pipeline_
|
| 63 |
+
optimize_pipeline_(pipe, "prompt")
|
| 64 |
|
| 65 |
+
def get_duration(prompt, styles_Radio, feife_select, bigboobs_select, seed, randomize_seed, width, height, num_inference_steps, guidancescale, num_feifei, nsfw_select, nsfw_slider, progress):
|
| 66 |
+
def_duration = 30.
|
| 67 |
+
def_steps = 4.
|
| 68 |
+
return int(def_duration * float(num_inference_steps) / def_steps)
|
| 69 |
|
| 70 |
+
@spaces.GPU(duration=get_duration)
|
| 71 |
+
def infer(prompt="", styles_Radio="(None)", feife_select = False, bigboobs_select = True, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidancescale=3.5, num_feifei=0.35, nsfw_select=False, nsfw_slider=1, progress=gr.Progress(track_tqdm=True)):
|
| 72 |
+
|
| 73 |
+
Duke86Syl_lora_name=[]
|
| 74 |
+
adapter_weights_num=[]
|
| 75 |
+
|
| 76 |
+
if feife_select:
|
| 77 |
+
Duke86Syl_lora_name.append("feifei")
|
| 78 |
+
adapter_weights_num.append(num_feifei)
|
| 79 |
+
|
| 80 |
+
if bigboobs_select:
|
| 81 |
+
Duke86Syl_lora_name.append("big-boobs-clothed-v2")
|
| 82 |
+
adapter_weights_num.append(0.45)
|
| 83 |
|
| 84 |
+
if nsfw_select:
|
| 85 |
+
Duke86Syl_lora_name.append("sldr_flux_nsfw_v2")
|
| 86 |
+
adapter_weights_num.append(nsfw_slider)
|
| 87 |
+
|
| 88 |
+
pipe.set_adapters(
|
| 89 |
+
Duke86Syl_lora_name,
|
| 90 |
+
adapter_weights=adapter_weights_num,
|
| 91 |
+
)
|
| 92 |
+
pipe.fuse_lora(
|
| 93 |
+
adapter_name=Duke86Syl_lora_name,
|
| 94 |
+
lora_scale=1.0,
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
#if feife_select:
|
| 98 |
+
# # Define the replacement string
|
| 99 |
+
# replacement = " feifei, A beautiful, 18 yo kpop idol, large-busted Japanese slim girl, with light makeup, gazing deeply into the camera, "
|
| 100 |
+
#
|
| 101 |
+
# # Perform the replacements with re.IGNORECASE
|
| 102 |
+
# prompt = re.sub(r"girl", replacement, prompt, flags=re.IGNORECASE)
|
| 103 |
+
# prompt = re.sub(r"young woman", replacement, prompt, flags=re.IGNORECASE)
|
| 104 |
+
# prompt = re.sub(r"woman", replacement, prompt, flags=re.IGNORECASE)
|
| 105 |
+
# prompt = re.sub(r"model", replacement, prompt, flags=re.IGNORECASE)
|
| 106 |
+
|
| 107 |
+
if styles_Radio:
|
| 108 |
+
style_name = styles_Radio
|
| 109 |
+
for style in config.style_list:
|
| 110 |
+
if style["name"] == style_name:
|
| 111 |
+
prompt = style["prompt"].replace("{prompt}", prompt)
|
| 112 |
+
if randomize_seed:
|
| 113 |
+
seed = random.randint(0, MAX_SEED)
|
| 114 |
+
generator = torch.Generator().manual_seed(seed)
|
| 115 |
+
|
| 116 |
+
image = pipe(
|
| 117 |
+
prompt = "flux, 8k, ",
|
| 118 |
+
prompt_2 = prompt,
|
| 119 |
+
width = width,
|
| 120 |
+
height = height,
|
| 121 |
+
num_inference_steps = num_inference_steps,
|
| 122 |
+
generator = generator,
|
| 123 |
+
guidance_scale=guidancescale
|
| 124 |
+
).images[0]
|
| 125 |
+
return image, seed
|
| 126 |
+
|
| 127 |
+
examples = [
|
| 128 |
+
"this photo is a girl",
|
| 129 |
+
"this photo is a girl in bikini",
|
| 130 |
+
"this photo is a cute girl in cute bikini",
|
| 131 |
+
"girl, sunrise",
|
| 132 |
+
"DarkIdol flux girl",
|
| 133 |
+
"a sexy girl,poses,look at camera,Slim figure, gigantic breasts,poses,natural,High-quality photography, creative composition, fashion foresight, a strong visual style, and an aura of luxury and sophistication collectively define the distinctive aesthetic of Vogue magazine.",
|
| 134 |
+
"real model slight smile girl in real life",
|
| 135 |
+
"real model smile girl in real life",
|
| 136 |
+
"real model girl in real life",
|
| 137 |
+
"A high-resolution photograph of girl in a serene, natural setting, with soft, warm lighting, and a minimalist aesthetic, showcasing a elegant fragrance bottle and the model's effortless, emotive expression, with impeccable styling, and a muted color palette, evoking a sense of understated luxury and refinement."
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
css="""
|
| 141 |
+
#col-container {
|
| 142 |
+
margin: 0 auto;
|
| 143 |
+
max-width: 520px;
|
| 144 |
+
}
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
with gr.Blocks(css=css) as demo:
|
| 148 |
+
|
| 149 |
+
with gr.Column(elem_id="col-container"):
|
| 150 |
+
result = gr.Image(label="Result", show_label=False,height=500,format="png")
|
| 151 |
+
with gr.Row():
|
| 152 |
+
prompt = gr.Text(
|
| 153 |
+
label="Prompt",
|
| 154 |
+
show_label=False,
|
| 155 |
+
max_lines=12,
|
| 156 |
+
placeholder="Enter your prompt",
|
| 157 |
+
value="",
|
| 158 |
+
container=False,
|
| 159 |
+
)
|
| 160 |
+
run_button = gr.Button("Run")
|
| 161 |
+
with gr.Row():
|
| 162 |
+
styles_Radio = gr.Dropdown(
|
| 163 |
+
styles_name,
|
| 164 |
+
label="Styles",
|
| 165 |
+
multiselect=False,
|
| 166 |
+
value="(None)",
|
| 167 |
+
)
|
| 168 |
+
#feifei_chat_text1 = gr.Textbox(
|
| 169 |
+
# label="输入您的问题1:", show_label=False, container=False, lines=1, value="")
|
| 170 |
+
#feifei_chat_text2 = gr.Textbox(
|
| 171 |
+
# label="输入您的问题2:", show_label=False, container=False, lines=3, value="")
|
| 172 |
+
|
| 173 |
+
# 定义模型选择下拉框
|
| 174 |
+
#feifei_chat_Dropdown = gr.Dropdown(
|
| 175 |
+
# [
|
| 176 |
+
# "meta/llama-3.3-70b-instruct",
|
| 177 |
+
# "nvidia/llama-3.3-nemotron-super-49b-v1",
|
| 178 |
+
# "mistralai/Mistral-Nemo-Instruct-2411",
|
| 179 |
+
# ],
|
| 180 |
+
# value="mistralai/Mistral-Nemo-Instruct-2411",
|
| 181 |
+
# label="选择模型", show_label=False, container=False
|
| 182 |
+
#)
|
| 183 |
+
# 定义提交按钮
|
| 184 |
+
#feifei_chat_btn = gr.Button(value="Gen Prompt")
|
| 185 |
+
feife_select = gr.Checkbox(label="FeiFei Expansion", value=True)
|
| 186 |
+
bigboobs_select = gr.Checkbox(label="bigboobs", value=True)
|
| 187 |
+
nsfw_select = gr.Checkbox(label="NSFW")
|
| 188 |
+
nsfw_slider = gr.Slider(
|
| 189 |
+
label="NSFW",
|
| 190 |
+
minimum=0,
|
| 191 |
+
maximum=2,
|
| 192 |
+
step=0.05,
|
| 193 |
+
value=0.75,
|
| 194 |
+
)
|
| 195 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 196 |
+
|
| 197 |
+
seed = gr.Slider(
|
| 198 |
+
label="Seed",
|
| 199 |
+
minimum=0,
|
| 200 |
+
maximum=MAX_SEED,
|
| 201 |
+
step=1,
|
| 202 |
+
value=0,
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 206 |
|
| 207 |
+
with gr.Row():
|
| 208 |
+
gr.Markdown(''' - 21:9 2240x1024
|
| 209 |
+
- 16:9 1856x1024
|
| 210 |
+
- 9:7 1344x1024 ''')
|
| 211 |
+
width = gr.Slider(
|
| 212 |
+
label="Width",
|
| 213 |
+
minimum=256,
|
| 214 |
+
maximum=MAX_IMAGE_SIZE,
|
| 215 |
+
step=64,
|
| 216 |
+
value=1024,
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
height = gr.Slider(
|
| 220 |
+
label="Height",
|
| 221 |
+
minimum=256,
|
| 222 |
+
maximum=MAX_IMAGE_SIZE,
|
| 223 |
+
step=64,
|
| 224 |
+
value=1856,
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
with gr.Row():
|
| 228 |
+
num_inference_steps = gr.Slider(
|
| 229 |
+
label="Number of inference steps",
|
| 230 |
+
minimum=1,
|
| 231 |
+
maximum=50,
|
| 232 |
+
step=1,
|
| 233 |
+
value=4,
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
with gr.Row():
|
| 237 |
+
guidancescale = gr.Slider(
|
| 238 |
+
label="Guidance scale",
|
| 239 |
+
minimum=0,
|
| 240 |
+
maximum=10,
|
| 241 |
+
step=0.1,
|
| 242 |
+
value=3.5,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
with gr.Row():
|
| 246 |
+
num_feifei = gr.Slider(
|
| 247 |
+
label="FeiFei",
|
| 248 |
+
minimum=0,
|
| 249 |
+
maximum=2,
|
| 250 |
+
step=0.05,
|
| 251 |
+
value=0.35,
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
gr.Examples(
|
| 256 |
+
examples = examples,
|
| 257 |
+
fn = infer,
|
| 258 |
+
inputs = [prompt],
|
| 259 |
+
outputs = [result, seed],
|
| 260 |
+
cache_examples=False
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
run_button.click(
|
| 264 |
+
fn = infer,
|
| 265 |
+
inputs = [prompt, styles_Radio, feife_select, bigboobs_select, seed, randomize_seed, width, height, num_inference_steps, guidancescale, num_feifei, nsfw_select, nsfw_slider],
|
| 266 |
+
outputs = [result, seed]
|
| 267 |
+
)
|
| 268 |
+
#feifei_chat_btn.click(
|
| 269 |
+
# fn=feifeichat,
|
| 270 |
+
# inputs=[feifei_chat_text1, feifei_chat_text2, feifei_chat_Dropdown],
|
| 271 |
+
# outputs=prompt
|
| 272 |
+
#)
|
| 273 |
|
| 274 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|