Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,13 @@ import random
|
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
|
| 9 |
-
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
from gradio_client import Client, handle_file
|
| 12 |
import os
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
dtype = torch.bfloat16
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -24,9 +27,9 @@ if not hf_token:
|
|
| 24 |
MAX_SEED = np.iinfo(np.int32).max
|
| 25 |
MAX_IMAGE_SIZE = 2048
|
| 26 |
|
| 27 |
-
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16, token=hf_token).to(device)
|
| 28 |
|
| 29 |
-
@spaces.GPU()
|
| 30 |
def infer(prompt, seed=0, randomize_seed=True, width=640, height=1024, guidance_scale=0.0, num_inference_steps=5, lora_model="AlekseyCalvin/RCA_Agitprop_Manufactory", progress=gr.Progress(track_tqdm=True)):
|
| 31 |
global pipe
|
| 32 |
|
|
@@ -55,18 +58,39 @@ def infer(prompt, seed=0, randomize_seed=True, width=640, height=1024, guidance_
|
|
| 55 |
if lora_model:
|
| 56 |
pipe.unload_lora_weights()
|
| 57 |
|
| 58 |
-
return image, seed, "Image generated successfully."
|
| 59 |
except Exception as e:
|
| 60 |
return None, seed, f"Error during image generation: {str(e)}"
|
| 61 |
|
| 62 |
|
| 63 |
-
return image, seed
|
| 64 |
|
| 65 |
|
| 66 |
examples = [
|
| 67 |
"RCA style communist party poster with the words Ready for REVOLUTION? in large black consistent constructivist font alongside a red Soviet hammer and a red Soviet sickle over the background of planet earth, over the North American continent",
|
| 68 |
]
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
css="""
|
| 71 |
#col-container {
|
| 72 |
margin: 0 auto;
|
|
@@ -74,7 +98,8 @@ css="""
|
|
| 74 |
}
|
| 75 |
"""
|
| 76 |
|
| 77 |
-
with gr.Blocks(css=
|
|
|
|
| 78 |
|
| 79 |
with gr.Column(elem_id="col-container"):
|
| 80 |
gr.Markdown(f"""# RCA Agitprop Manufactory: pre-phrase prompts with 'RCA style' to activate custom model """)
|
|
@@ -84,14 +109,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 84 |
prompt = gr.Text(
|
| 85 |
label="Prompt",
|
| 86 |
show_label=False,
|
| 87 |
-
max_lines=
|
| 88 |
placeholder="RCA style communist poster of ",
|
| 89 |
container=False,
|
| 90 |
)
|
| 91 |
|
| 92 |
run_button = gr.Button("Run", scale=0)
|
| 93 |
|
| 94 |
-
|
| 95 |
|
| 96 |
with gr.Accordion("Advanced Settings", open=True):
|
| 97 |
|
|
@@ -112,7 +137,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 112 |
minimum=256,
|
| 113 |
maximum=MAX_IMAGE_SIZE,
|
| 114 |
step=32,
|
| 115 |
-
value=
|
| 116 |
)
|
| 117 |
|
| 118 |
height = gr.Slider(
|
|
@@ -120,7 +145,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 120 |
minimum=256,
|
| 121 |
maximum=MAX_IMAGE_SIZE,
|
| 122 |
step=32,
|
| 123 |
-
value=
|
| 124 |
)
|
| 125 |
|
| 126 |
with gr.Row():
|
|
@@ -131,14 +156,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 131 |
minimum=1,
|
| 132 |
maximum=50,
|
| 133 |
step=1,
|
| 134 |
-
value=
|
| 135 |
)
|
| 136 |
|
| 137 |
gr.Examples(
|
| 138 |
examples = examples,
|
| 139 |
fn = infer,
|
| 140 |
inputs = [prompt],
|
| 141 |
-
outputs = [
|
| 142 |
cache_examples="lazy"
|
| 143 |
)
|
| 144 |
|
|
@@ -146,7 +171,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 146 |
triggers=[run_button.click, prompt.submit],
|
| 147 |
fn = infer,
|
| 148 |
inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 149 |
-
outputs = [
|
| 150 |
)
|
| 151 |
|
| 152 |
-
demo.launch()
|
|
|
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
|
| 9 |
+
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast, AutoProcessor, pipeline
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
from gradio_client import Client, handle_file
|
| 12 |
import os
|
| 13 |
+
import subprocess
|
| 14 |
+
|
| 15 |
+
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 16 |
|
| 17 |
dtype = torch.bfloat16
|
| 18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 27 |
MAX_SEED = np.iinfo(np.int32).max
|
| 28 |
MAX_IMAGE_SIZE = 2048
|
| 29 |
|
| 30 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16, revision="refs/pr/1", token=hf_token).to(device)
|
| 31 |
|
| 32 |
+
@spaces.GPU(duration=60)
|
| 33 |
def infer(prompt, seed=0, randomize_seed=True, width=640, height=1024, guidance_scale=0.0, num_inference_steps=5, lora_model="AlekseyCalvin/RCA_Agitprop_Manufactory", progress=gr.Progress(track_tqdm=True)):
|
| 34 |
global pipe
|
| 35 |
|
|
|
|
| 58 |
if lora_model:
|
| 59 |
pipe.unload_lora_weights()
|
| 60 |
|
| 61 |
+
return image, prompt, seed, "Image generated successfully."
|
| 62 |
except Exception as e:
|
| 63 |
return None, seed, f"Error during image generation: {str(e)}"
|
| 64 |
|
| 65 |
|
| 66 |
+
return image, prompt, seed
|
| 67 |
|
| 68 |
|
| 69 |
examples = [
|
| 70 |
"RCA style communist party poster with the words Ready for REVOLUTION? in large black consistent constructivist font alongside a red Soviet hammer and a red Soviet sickle over the background of planet earth, over the North American continent",
|
| 71 |
]
|
| 72 |
|
| 73 |
+
custom_css = """
|
| 74 |
+
#col-container {
|
| 75 |
+
margin: 0 auto;
|
| 76 |
+
max-width: 520px;
|
| 77 |
+
}
|
| 78 |
+
.input-group, .output-group {
|
| 79 |
+
border: 1px solid #eb3109;
|
| 80 |
+
border-radius: 10px;
|
| 81 |
+
padding: 20px;
|
| 82 |
+
margin-bottom: 20px;
|
| 83 |
+
background-color: #f9f9f9;
|
| 84 |
+
}
|
| 85 |
+
.submit-btn {
|
| 86 |
+
background-color: #2980b9 !important;
|
| 87 |
+
color: white !important;
|
| 88 |
+
}
|
| 89 |
+
.submit-btn:hover {
|
| 90 |
+
background-color: #3498db !important;
|
| 91 |
+
}
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
css="""
|
| 95 |
#col-container {
|
| 96 |
margin: 0 auto;
|
|
|
|
| 98 |
}
|
| 99 |
"""
|
| 100 |
|
| 101 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="red", secondary_hue="gray")) as demo:
|
| 102 |
+
gr.HTML(title)
|
| 103 |
|
| 104 |
with gr.Column(elem_id="col-container"):
|
| 105 |
gr.Markdown(f"""# RCA Agitprop Manufactory: pre-phrase prompts with 'RCA style' to activate custom model """)
|
|
|
|
| 109 |
prompt = gr.Text(
|
| 110 |
label="Prompt",
|
| 111 |
show_label=False,
|
| 112 |
+
max_lines=2,
|
| 113 |
placeholder="RCA style communist poster of ",
|
| 114 |
container=False,
|
| 115 |
)
|
| 116 |
|
| 117 |
run_button = gr.Button("Run", scale=0)
|
| 118 |
|
| 119 |
+
output_image = gr.Image(label="Result", elem_id="gallery", show_label=False)
|
| 120 |
|
| 121 |
with gr.Accordion("Advanced Settings", open=True):
|
| 122 |
|
|
|
|
| 137 |
minimum=256,
|
| 138 |
maximum=MAX_IMAGE_SIZE,
|
| 139 |
step=32,
|
| 140 |
+
value=640,
|
| 141 |
)
|
| 142 |
|
| 143 |
height = gr.Slider(
|
|
|
|
| 145 |
minimum=256,
|
| 146 |
maximum=MAX_IMAGE_SIZE,
|
| 147 |
step=32,
|
| 148 |
+
value=1024,
|
| 149 |
)
|
| 150 |
|
| 151 |
with gr.Row():
|
|
|
|
| 156 |
minimum=1,
|
| 157 |
maximum=50,
|
| 158 |
step=1,
|
| 159 |
+
value=5,
|
| 160 |
)
|
| 161 |
|
| 162 |
gr.Examples(
|
| 163 |
examples = examples,
|
| 164 |
fn = infer,
|
| 165 |
inputs = [prompt],
|
| 166 |
+
outputs = [output_image, seed],
|
| 167 |
cache_examples="lazy"
|
| 168 |
)
|
| 169 |
|
|
|
|
| 171 |
triggers=[run_button.click, prompt.submit],
|
| 172 |
fn = infer,
|
| 173 |
inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 174 |
+
outputs = [output_image, seed]
|
| 175 |
)
|
| 176 |
|
| 177 |
+
demo.launch(debug=True)
|