Update appCODE.py
Browse files- appCODE.py +60 -59
appCODE.py
CHANGED
|
@@ -7,75 +7,76 @@ from diffusers import StableDiffusion3Pipeline
|
|
| 7 |
from diffusers.loaders import SD3LoraLoaderMixin
|
| 8 |
from safetensors.torch import load_file, save_file
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Load Hugging Face token securely
|
| 15 |
-
token = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
-
# Model ID for SD 3.5 Large
|
| 18 |
-
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
| 19 |
|
| 20 |
-
# Convert .pt to .safetensors if needed
|
| 21 |
-
lora_pt_path = "lora_trained_model.pt"
|
| 22 |
-
lora_safetensors_path = "lora_trained_model.safetensors"
|
| 23 |
|
| 24 |
-
if os.path.exists(lora_pt_path) and not os.path.exists(lora_safetensors_path):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
# Load Stable Diffusion pipeline
|
| 31 |
-
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
).to(device)
|
| 36 |
|
| 37 |
-
# Load and fuse LoRA
|
| 38 |
-
if os.path.exists(lora_safetensors_path):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
else:
|
| 46 |
-
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
for name,
|
| 50 |
-
|
| 51 |
-
print(f"β
LoRA applied to: {name}, requires_grad={param.requires_grad}")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
@spaces.GPU(duration=65)
|
| 55 |
-
def generate_image(prompt: str, seed: int = None):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# Create a generator with the seed
|
| 61 |
-
generator = torch.manual_seed(seed)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
seed_input = gr.Number(label="Seed (optional)", value=None)
|
| 74 |
-
|
| 75 |
-
generate_btn = gr.Button("Generate Image")
|
| 76 |
-
output_image = gr.Image(label="Generated Image")
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
# Launch
|
| 81 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from diffusers.loaders import SD3LoraLoaderMixin
|
| 8 |
from safetensors.torch import load_file, save_file
|
| 9 |
|
| 10 |
+
# Ensure GPU allocation for image generation (moved here)
|
| 11 |
+
def main():
|
| 12 |
+
# Device selection
|
| 13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 15 |
|
| 16 |
+
# Load Hugging Face token securely
|
| 17 |
+
token = os.getenv("HF_TOKEN")
|
| 18 |
|
| 19 |
+
# Model ID for SD 3.5 Large
|
| 20 |
+
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
| 21 |
|
| 22 |
+
# Convert .pt to .safetensors if needed
|
| 23 |
+
lora_pt_path = "lora_trained_model.pt"
|
| 24 |
+
lora_safetensors_path = "lora_trained_model.safetensors"
|
| 25 |
|
| 26 |
+
if os.path.exists(lora_pt_path) and not os.path.exists(lora_safetensors_path):
|
| 27 |
+
print("π Converting LoRA .pt to .safetensors...")
|
| 28 |
+
lora_weights = torch.load(lora_pt_path, map_location="cpu")
|
| 29 |
+
save_file(lora_weights, lora_safetensors_path)
|
| 30 |
+
print(f"β
LoRA saved as {lora_safetensors_path}")
|
| 31 |
|
| 32 |
+
# Load Stable Diffusion 3.5 pipeline with optimized settings
|
| 33 |
+
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
| 34 |
+
model_repo_id,
|
| 35 |
+
torch_dtype=torch_dtype,
|
| 36 |
+
use_safetensors=True,
|
| 37 |
+
).to(device)
|
| 38 |
|
| 39 |
+
# Load and fuse LoRA weights (optimized method)
|
| 40 |
+
if os.path.exists(lora_safetensors_path):
|
| 41 |
+
try:
|
| 42 |
+
SD3LoraLoaderMixin.load_lora_weights(pipeline, lora_safetensors_path)
|
| 43 |
+
pipeline.fuse_lora()
|
| 44 |
+
print("β
LoRA weights loaded and fused successfully!")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"β Error loading LoRA: {e}")
|
| 47 |
+
else:
|
| 48 |
+
print("β οΈ LoRA file not found! Running base model.")
|
| 49 |
|
| 50 |
+
# Ensure LoRA is applied correctly
|
| 51 |
+
applied_lora = any("lora" in name.lower() for name, _ in pipeline.text_encoder.named_parameters())
|
| 52 |
+
print(f"β
LoRA Applied: {applied_lora}")
|
|
|
|
| 53 |
|
| 54 |
+
# Image generation function with GPU decorator
|
| 55 |
+
@spaces.GPU(duration=65)
|
| 56 |
+
def generate_image(prompt: str, seed: int = None):
|
| 57 |
+
"""Generates an image using Stable Diffusion 3.5 with LoRA fine-tuning."""
|
| 58 |
+
seed = seed or random.randint(0, 100000)
|
| 59 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 60 |
+
return pipeline(prompt, generator=generator).images[0]
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Gradio Interface
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# πΌοΈ LoRA Fine-Tuned SD 3.5 Image Generator")
|
| 65 |
|
| 66 |
+
with gr.Row():
|
| 67 |
+
prompt_input = gr.Textbox(
|
| 68 |
+
label="Enter Prompt",
|
| 69 |
+
value="A woman in her 20s with expressive black eyes, graceful face, elegant body, standing on the beach at sunset. Photorealistic, highly detailed."
|
| 70 |
+
)
|
| 71 |
+
seed_input = gr.Number(label="Seed (optional)", value=None)
|
| 72 |
|
| 73 |
+
generate_btn = gr.Button("Generate Image")
|
| 74 |
+
output_image = gr.Image(label="Generated Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
generate_btn.click(generate_image, inputs=[prompt_input, seed_input], outputs=output_image)
|
| 77 |
|
| 78 |
+
# Launch Gradio app
|
| 79 |
+
demo.launch()
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
main()
|