Spaces:
Sleeping
Sleeping
Update app.py
Browse files🧪 Test Shilin-LU/VINE-B-Enc
app.py
CHANGED
|
@@ -1,33 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
|
| 9 |
model_repo_id = "stabilityai/sdxl-turbo" # Base model
|
| 10 |
-
lora_adapter_id = "
|
| 11 |
-
lora_adapter_name = "
|
| 12 |
|
|
|
|
| 13 |
if torch.cuda.is_available():
|
| 14 |
torch_dtype = torch.float16
|
| 15 |
-
device = "cuda"
|
| 16 |
else:
|
| 17 |
torch_dtype = torch.float32
|
| 18 |
-
device = "cpu"
|
| 19 |
|
| 20 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype, variant="fp16" if torch_dtype==torch.float16 else None)
|
| 21 |
pipe = pipe.to(device)
|
| 22 |
# Charger l'adapter LoRA
|
| 23 |
-
|
| 24 |
-
pipe.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
MAX_SEED = np.iinfo(np.int32).max
|
| 27 |
MAX_IMAGE_SIZE = 1024
|
| 28 |
|
| 29 |
-
|
| 30 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 31 |
def infer(
|
| 32 |
prompt,
|
| 33 |
negative_prompt,
|
|
@@ -41,9 +40,7 @@ def infer(
|
|
| 41 |
):
|
| 42 |
if randomize_seed:
|
| 43 |
seed = random.randint(0, MAX_SEED)
|
| 44 |
-
|
| 45 |
generator = torch.Generator().manual_seed(seed)
|
| 46 |
-
|
| 47 |
image = pipe(
|
| 48 |
prompt=prompt,
|
| 49 |
negative_prompt=negative_prompt,
|
|
@@ -53,10 +50,8 @@ def infer(
|
|
| 53 |
height=height,
|
| 54 |
generator=generator,
|
| 55 |
).images[0]
|
| 56 |
-
|
| 57 |
return image, seed
|
| 58 |
|
| 59 |
-
|
| 60 |
examples = [
|
| 61 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 62 |
"An astronaut riding a green horse",
|
|
@@ -72,8 +67,7 @@ css = """
|
|
| 72 |
|
| 73 |
with gr.Blocks(css=css) as demo:
|
| 74 |
with gr.Column(elem_id="col-container"):
|
| 75 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 76 |
-
|
| 77 |
with gr.Row():
|
| 78 |
prompt = gr.Text(
|
| 79 |
label="Prompt",
|
|
@@ -82,11 +76,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 82 |
placeholder="Enter your prompt",
|
| 83 |
container=False,
|
| 84 |
)
|
| 85 |
-
|
| 86 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 87 |
-
|
| 88 |
result = gr.Image(label="Result", show_label=False)
|
| 89 |
-
|
| 90 |
with gr.Accordion("Advanced Settings", open=False):
|
| 91 |
negative_prompt = gr.Text(
|
| 92 |
label="Negative prompt",
|
|
@@ -94,7 +85,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 94 |
placeholder="Enter a negative prompt",
|
| 95 |
visible=False,
|
| 96 |
)
|
| 97 |
-
|
| 98 |
seed = gr.Slider(
|
| 99 |
label="Seed",
|
| 100 |
minimum=0,
|
|
@@ -102,43 +92,37 @@ with gr.Blocks(css=css) as demo:
|
|
| 102 |
step=1,
|
| 103 |
value=0,
|
| 104 |
)
|
| 105 |
-
|
| 106 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 107 |
-
|
| 108 |
with gr.Row():
|
| 109 |
width = gr.Slider(
|
| 110 |
label="Width",
|
| 111 |
minimum=256,
|
| 112 |
maximum=MAX_IMAGE_SIZE,
|
| 113 |
step=32,
|
| 114 |
-
value=1024,
|
| 115 |
)
|
| 116 |
-
|
| 117 |
height = gr.Slider(
|
| 118 |
label="Height",
|
| 119 |
minimum=256,
|
| 120 |
maximum=MAX_IMAGE_SIZE,
|
| 121 |
step=32,
|
| 122 |
-
value=1024,
|
| 123 |
)
|
| 124 |
-
|
| 125 |
with gr.Row():
|
| 126 |
guidance_scale = gr.Slider(
|
| 127 |
label="Guidance scale",
|
| 128 |
minimum=0.0,
|
| 129 |
maximum=10.0,
|
| 130 |
step=0.1,
|
| 131 |
-
value=0.0,
|
| 132 |
)
|
| 133 |
-
|
| 134 |
num_inference_steps = gr.Slider(
|
| 135 |
label="Number of inference steps",
|
| 136 |
minimum=1,
|
| 137 |
maximum=50,
|
| 138 |
step=1,
|
| 139 |
-
value=2,
|
| 140 |
)
|
| 141 |
-
|
| 142 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 143 |
gr.on(
|
| 144 |
triggers=[run_button.click, prompt.submit],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
model_repo_id = "stabilityai/sdxl-turbo" # Base model
|
| 8 |
+
lora_adapter_id = "Shilin-LU/VINE-B-Enc" # LoRA adapter
|
| 9 |
+
lora_adapter_name = "VINE-B-Enc"
|
| 10 |
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
if torch.cuda.is_available():
|
| 13 |
torch_dtype = torch.float16
|
|
|
|
| 14 |
else:
|
| 15 |
torch_dtype = torch.float32
|
|
|
|
| 16 |
|
| 17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype, variant="fp16" if torch_dtype==torch.float16 else None)
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
# Charger l'adapter LoRA
|
| 20 |
+
try:
|
| 21 |
+
pipe.load_lora_weights(lora_adapter_id, adapter_name=lora_adapter_name)
|
| 22 |
+
pipe.set_adapters([lora_adapter_name], adapter_weights=[1.0])
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print("[INFO] If you see 'PEFT backend is required for this method.', please install PEFT with: pip install peft\nError details:", e)
|
| 25 |
+
print(f"Erreur lors du chargement de l'adapter LoRA: {e}")
|
| 26 |
|
| 27 |
MAX_SEED = np.iinfo(np.int32).max
|
| 28 |
MAX_IMAGE_SIZE = 1024
|
| 29 |
|
|
|
|
|
|
|
| 30 |
def infer(
|
| 31 |
prompt,
|
| 32 |
negative_prompt,
|
|
|
|
| 40 |
):
|
| 41 |
if randomize_seed:
|
| 42 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 43 |
generator = torch.Generator().manual_seed(seed)
|
|
|
|
| 44 |
image = pipe(
|
| 45 |
prompt=prompt,
|
| 46 |
negative_prompt=negative_prompt,
|
|
|
|
| 50 |
height=height,
|
| 51 |
generator=generator,
|
| 52 |
).images[0]
|
|
|
|
| 53 |
return image, seed
|
| 54 |
|
|
|
|
| 55 |
examples = [
|
| 56 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 57 |
"An astronaut riding a green horse",
|
|
|
|
| 67 |
|
| 68 |
with gr.Blocks(css=css) as demo:
|
| 69 |
with gr.Column(elem_id="col-container"):
|
| 70 |
+
gr.Markdown(" # Text-to-Image Gradio Template (Shilin-LU/VINE-B-Enc adapter)")
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
| 73 |
label="Prompt",
|
|
|
|
| 76 |
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
|
|
|
| 79 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
|
|
|
| 80 |
result = gr.Image(label="Result", show_label=False)
|
|
|
|
| 81 |
with gr.Accordion("Advanced Settings", open=False):
|
| 82 |
negative_prompt = gr.Text(
|
| 83 |
label="Negative prompt",
|
|
|
|
| 85 |
placeholder="Enter a negative prompt",
|
| 86 |
visible=False,
|
| 87 |
)
|
|
|
|
| 88 |
seed = gr.Slider(
|
| 89 |
label="Seed",
|
| 90 |
minimum=0,
|
|
|
|
| 92 |
step=1,
|
| 93 |
value=0,
|
| 94 |
)
|
|
|
|
| 95 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
width = gr.Slider(
|
| 98 |
label="Width",
|
| 99 |
minimum=256,
|
| 100 |
maximum=MAX_IMAGE_SIZE,
|
| 101 |
step=32,
|
| 102 |
+
value=1024,
|
| 103 |
)
|
|
|
|
| 104 |
height = gr.Slider(
|
| 105 |
label="Height",
|
| 106 |
minimum=256,
|
| 107 |
maximum=MAX_IMAGE_SIZE,
|
| 108 |
step=32,
|
| 109 |
+
value=1024,
|
| 110 |
)
|
|
|
|
| 111 |
with gr.Row():
|
| 112 |
guidance_scale = gr.Slider(
|
| 113 |
label="Guidance scale",
|
| 114 |
minimum=0.0,
|
| 115 |
maximum=10.0,
|
| 116 |
step=0.1,
|
| 117 |
+
value=0.0,
|
| 118 |
)
|
|
|
|
| 119 |
num_inference_steps = gr.Slider(
|
| 120 |
label="Number of inference steps",
|
| 121 |
minimum=1,
|
| 122 |
maximum=50,
|
| 123 |
step=1,
|
| 124 |
+
value=2,
|
| 125 |
)
|
|
|
|
| 126 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 127 |
gr.on(
|
| 128 |
triggers=[run_button.click, prompt.submit],
|