Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,66 +4,60 @@ import torch
|
|
| 4 |
from diffusers import FluxPipeline, FluxTransformer2DModel, FlowMatchEulerDiscreteScheduler
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
import random
|
| 9 |
import os
|
| 10 |
-
|
| 11 |
hf_token = os.environ.get('HF_TOKEN')
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Constants
|
| 14 |
model = "black-forest-labs/FLUX.1-dev"
|
|
|
|
|
|
|
|
|
|
| 15 |
MAX_SEED = np.iinfo(np.int32).max
|
| 16 |
MAX_IMAGE_SIZE = 2048
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
transformer = FluxTransformer2DModel.from_single_file(
|
| 25 |
-
"https://huggingface.co/lodestones/Chroma/resolve/main/chroma-unlocked-v27.safetensors",
|
| 26 |
-
torch_dtype=torch.bfloat16,
|
| 27 |
-
token=hf_token
|
| 28 |
-
)
|
| 29 |
-
except KeyError as e:
|
| 30 |
-
print(f"Error loading chroma-unlocked-v27.safetensors: {e}. Falling back to pretrained model.")
|
| 31 |
-
transformer = FluxTransformer2DModel.from_pretrained(
|
| 32 |
-
"lodestones/Chroma",
|
| 33 |
-
subfolder="transformer",
|
| 34 |
-
torch_dtype=torch.bfloat16,
|
| 35 |
-
token=hf_token
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
pipe = FluxPipeline.from_pretrained(
|
| 39 |
-
model,
|
| 40 |
transformer=transformer,
|
| 41 |
-
torch_dtype=torch.bfloat16,
|
| 42 |
-
token=hf_token
|
| 43 |
-
)
|
| 44 |
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
|
| 45 |
pipe.scheduler.config, use_beta_sigmas=True
|
| 46 |
)
|
| 47 |
-
pipe.to(
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
if randomize_seed:
|
| 51 |
seed = random.randint(0, MAX_SEED)
|
| 52 |
-
generator = torch.Generator(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
generator=generator
|
| 62 |
).images
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
#col-container {
|
| 68 |
margin: 0 auto;
|
| 69 |
max-width: 1024px;
|
|
@@ -71,10 +65,12 @@ css = """
|
|
| 71 |
"""
|
| 72 |
|
| 73 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 74 |
with gr.Column(elem_id="col-container"):
|
| 75 |
-
gr.HTML("<h1><center>Model Testing</center></h1><p><center>
|
| 76 |
|
| 77 |
with gr.Row():
|
|
|
|
| 78 |
prompt = gr.Text(
|
| 79 |
label="Prompt",
|
| 80 |
show_label=False,
|
|
@@ -82,12 +78,15 @@ with gr.Blocks(css=css) as demo:
|
|
| 82 |
placeholder="Enter your prompt",
|
| 83 |
container=False,
|
| 84 |
)
|
|
|
|
| 85 |
run_button = gr.Button("Run", scale=0)
|
| 86 |
|
| 87 |
-
result = gr.Gallery(label="Gallery", format="png", columns=1, preview=True, height=400)
|
| 88 |
|
| 89 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 90 |
with gr.Row():
|
|
|
|
| 91 |
width = gr.Slider(
|
| 92 |
label="Width",
|
| 93 |
minimum=256,
|
|
@@ -95,6 +94,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 95 |
step=32,
|
| 96 |
value=1024,
|
| 97 |
)
|
|
|
|
| 98 |
height = gr.Slider(
|
| 99 |
label="Height",
|
| 100 |
minimum=256,
|
|
@@ -104,6 +104,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 104 |
)
|
| 105 |
|
| 106 |
with gr.Row():
|
|
|
|
| 107 |
num_inference_steps = gr.Slider(
|
| 108 |
label="Number of inference steps",
|
| 109 |
minimum=1,
|
|
@@ -111,6 +112,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 111 |
step=1,
|
| 112 |
value=30,
|
| 113 |
)
|
|
|
|
| 114 |
guidance_scale = gr.Slider(
|
| 115 |
label="Guidance Scale",
|
| 116 |
minimum=0,
|
|
@@ -119,7 +121,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 119 |
value=3.5,
|
| 120 |
)
|
| 121 |
|
|
|
|
| 122 |
with gr.Row():
|
|
|
|
| 123 |
nums = gr.Slider(
|
| 124 |
label="Number of Images",
|
| 125 |
minimum=1,
|
|
@@ -128,6 +132,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 128 |
value=1,
|
| 129 |
scale=1,
|
| 130 |
)
|
|
|
|
| 131 |
seed = gr.Slider(
|
| 132 |
label="Seed",
|
| 133 |
minimum=0,
|
|
@@ -135,13 +140,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 135 |
step=1,
|
| 136 |
value=-1,
|
| 137 |
)
|
|
|
|
| 138 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 139 |
|
| 140 |
gr.on(
|
| 141 |
triggers=[run_button.click, prompt.submit],
|
| 142 |
-
fn=infer,
|
| 143 |
-
inputs=[prompt, width, height, num_inference_steps, guidance_scale, nums, seed, randomize_seed],
|
| 144 |
-
outputs=[result, seed]
|
| 145 |
)
|
| 146 |
-
|
| 147 |
demo.launch()
|
|
|
|
| 4 |
from diffusers import FluxPipeline, FluxTransformer2DModel, FlowMatchEulerDiscreteScheduler
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from PIL import Image
|
| 7 |
+
import requests
|
| 8 |
+
from translatepy import Translator
|
| 9 |
import numpy as np
|
| 10 |
import random
|
| 11 |
import os
|
|
|
|
| 12 |
hf_token = os.environ.get('HF_TOKEN')
|
| 13 |
+
from io import BytesIO
|
| 14 |
+
|
| 15 |
+
translator = Translator()
|
| 16 |
|
| 17 |
# Constants
|
| 18 |
model = "black-forest-labs/FLUX.1-dev"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
MAX_SEED = np.iinfo(np.int32).max
|
| 23 |
MAX_IMAGE_SIZE = 2048
|
| 24 |
|
| 25 |
+
# Ensure model and scheduler are initialized in GPU-enabled function
|
| 26 |
+
if torch.cuda.is_available():
|
| 27 |
+
transformer = FluxTransformer2DModel.from_single_file(
|
| 28 |
+
"https://huggingface.co/ekt1701/Test_case/blob/main/rayflux_v10.safetensors",
|
| 29 |
+
torch_dtype=torch.bfloat16
|
| 30 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
pipe = FluxPipeline.from_pretrained(
|
| 32 |
+
model,
|
| 33 |
transformer=transformer,
|
| 34 |
+
torch_dtype=torch.bfloat16, token=hf_token)
|
|
|
|
|
|
|
| 35 |
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
|
| 36 |
pipe.scheduler.config, use_beta_sigmas=True
|
| 37 |
)
|
| 38 |
+
pipe.to("cuda")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@spaces.GPU()
|
| 42 |
+
def infer(prompt, width, height, num_inference_steps, guidance_scale, nums, seed=42, randomize_seed=True, progress=gr.Progress(track_tqdm=True)):
|
| 43 |
if randomize_seed:
|
| 44 |
seed = random.randint(0, MAX_SEED)
|
| 45 |
+
generator = torch.Generator().manual_seed(seed)
|
| 46 |
+
image = pipe(
|
| 47 |
+
prompt = prompt,
|
| 48 |
+
width = width,
|
| 49 |
+
height = height,
|
| 50 |
+
num_inference_steps = num_inference_steps,
|
| 51 |
+
guidance_scale=guidance_scale,
|
| 52 |
+
num_images_per_prompt=nums,
|
| 53 |
+
generator = generator
|
|
|
|
| 54 |
).images
|
| 55 |
|
| 56 |
+
|
| 57 |
+
return image, seed
|
| 58 |
|
| 59 |
+
|
| 60 |
+
css="""
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
max-width: 1024px;
|
|
|
|
| 65 |
"""
|
| 66 |
|
| 67 |
with gr.Blocks(css=css) as demo:
|
| 68 |
+
|
| 69 |
with gr.Column(elem_id="col-container"):
|
| 70 |
+
gr.HTML("<h1><center>Image Model Testing</center></h1><p><center>RayFlux V1 Model.</center></p>")
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
+
|
| 74 |
prompt = gr.Text(
|
| 75 |
label="Prompt",
|
| 76 |
show_label=False,
|
|
|
|
| 78 |
placeholder="Enter your prompt",
|
| 79 |
container=False,
|
| 80 |
)
|
| 81 |
+
|
| 82 |
run_button = gr.Button("Run", scale=0)
|
| 83 |
|
| 84 |
+
result = gr.Gallery(label="Gallery", format="png", columns = 1, preview=True, height=400)
|
| 85 |
|
| 86 |
with gr.Accordion("Advanced Settings", open=False):
|
| 87 |
+
|
| 88 |
with gr.Row():
|
| 89 |
+
|
| 90 |
width = gr.Slider(
|
| 91 |
label="Width",
|
| 92 |
minimum=256,
|
|
|
|
| 94 |
step=32,
|
| 95 |
value=1024,
|
| 96 |
)
|
| 97 |
+
|
| 98 |
height = gr.Slider(
|
| 99 |
label="Height",
|
| 100 |
minimum=256,
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
+
|
| 108 |
num_inference_steps = gr.Slider(
|
| 109 |
label="Number of inference steps",
|
| 110 |
minimum=1,
|
|
|
|
| 112 |
step=1,
|
| 113 |
value=30,
|
| 114 |
)
|
| 115 |
+
|
| 116 |
guidance_scale = gr.Slider(
|
| 117 |
label="Guidance Scale",
|
| 118 |
minimum=0,
|
|
|
|
| 121 |
value=3.5,
|
| 122 |
)
|
| 123 |
|
| 124 |
+
|
| 125 |
with gr.Row():
|
| 126 |
+
|
| 127 |
nums = gr.Slider(
|
| 128 |
label="Number of Images",
|
| 129 |
minimum=1,
|
|
|
|
| 132 |
value=1,
|
| 133 |
scale=1,
|
| 134 |
)
|
| 135 |
+
|
| 136 |
seed = gr.Slider(
|
| 137 |
label="Seed",
|
| 138 |
minimum=0,
|
|
|
|
| 140 |
step=1,
|
| 141 |
value=-1,
|
| 142 |
)
|
| 143 |
+
|
| 144 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 145 |
|
| 146 |
gr.on(
|
| 147 |
triggers=[run_button.click, prompt.submit],
|
| 148 |
+
fn = infer,
|
| 149 |
+
inputs = [prompt, width, height, num_inference_steps, guidance_scale, nums, seed, randomize_seed],
|
| 150 |
+
outputs = [result, seed]
|
| 151 |
)
|
| 152 |
+
|
| 153 |
demo.launch()
|