| import gradio as gr |
| from diffusers import StableDiffusionPipeline |
| import torch |
| import os |
| from safetensors import safe_open |
|
|
| |
| device = "cuda" if torch.cuda.is_available() else "cpu" |
| dtype = torch.float16 if device == "cuda" else torch.float32 |
|
|
| print(f"Using device: {device}") |
|
|
| |
| pipe = StableDiffusionPipeline.from_pretrained( |
| "runwayml/stable-diffusion-v1-5", |
| torch_dtype=dtype |
| ) |
|
|
| |
| lora_loaded = False |
| lora_path = "./lora" |
|
|
| print("π Looking for LoRA files...") |
|
|
| if os.path.exists(lora_path): |
| files = os.listdir(lora_path) |
| print(f"π Files in lora folder: {files}") |
| |
| |
| possible_names = [ |
| "adapter_model.safetensors", |
| "pytorch_lora_weights.safetensors", |
| "lora_weights.safetensors" |
| ] |
| |
| lora_file_found = None |
| for filename in possible_names: |
| if filename in files: |
| lora_file_found = filename |
| print(f"β
Found LoRA file: {filename}") |
| break |
| |
| if lora_file_found: |
| model_path = os.path.join(lora_path, lora_file_found) |
| file_size = os.path.getsize(model_path) |
| print(f"π File size: {file_size:,} bytes ({file_size/1024/1024:.2f} MB)") |
| |
| |
| try: |
| print("π§ Testing file integrity...") |
| with safe_open(model_path, framework="pt") as f: |
| keys = list(f.keys()) |
| print(f"β
File is valid! Found {len(keys)} tensors") |
| |
| |
| print("π― Loading with diffusers...") |
| |
| if lora_file_found == "pytorch_lora_weights.safetensors": |
| |
| pipe.load_lora_weights(lora_path, weight_name=lora_file_found) |
| else: |
| |
| pipe.load_lora_weights(lora_path) |
| |
| lora_loaded = True |
| print("β
LoRA weights loaded successfully!") |
| |
| except Exception as e: |
| print(f"β Error with {lora_file_found}: {e}") |
| print("π‘ File appears to be corrupted or incompatible") |
| else: |
| print("β No LoRA files found with expected names") |
| |
| else: |
| print("β LoRA folder not found") |
|
|
| |
| pipe = pipe.to(device) |
|
|
| |
| if device == "cpu": |
| try: |
| pipe.enable_attention_slicing() |
| print("β
Basic CPU optimizations enabled") |
| except Exception as e: |
| print(f"β οΈ Could not enable CPU optimizations: {e}") |
|
|
| def generate(prompt, quality): |
| if quality == "Fast": |
| steps = 20 |
| guidance_scale = 7.0 |
| else: |
| steps = 40 |
| guidance_scale = 8.5 |
| |
| with torch.no_grad(): |
| image = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance_scale).images[0] |
| return image |
|
|
| |
| title = "Fine-tuned Stable Diffusion" |
| if lora_loaded: |
| title += " with LoRA β¨" |
| description = "β
LoRA weights loaded! Your custom trained model is active." |
| else: |
| title += " (Base Model)" |
| description = "β οΈ Running with base model only. Check logs for details." |
|
|
| description += "\nChoose 'Fast' for quicker generation or 'High Quality' for better details." |
|
|
| demo = gr.Interface( |
| fn=generate, |
| inputs=[ |
| gr.Textbox(label="Enter your prompt", placeholder="A beautiful artwork..."), |
| gr.Dropdown(["Fast", "High Quality"], value="Fast", label="Generation Mode") |
| ], |
| outputs=gr.Image(label="Generated Image"), |
| title=title, |
| description=description |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |