Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# 1. Define the base model and the checkpoint model
|
| 8 |
+
BASE_MODEL = "runwayml/stable-diffusion-v1-5"
|
| 9 |
+
CHECKPOINT_MODEL = "uhralk/Indigo_Furry_mix"
|
| 10 |
+
CHECKPOINT_FILENAME = "indigo_Furrymix_v120_hybrid_fin_fp16.safetensors" # Using the recommended filename
|
| 11 |
+
|
| 12 |
+
# 2. Load the model
|
| 13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
BASE_MODEL,
|
| 19 |
+
torch_dtype=dtype,
|
| 20 |
+
use_safetensors=True
|
| 21 |
+
)
|
| 22 |
+
pipe.load_lora_weights(CHECKPOINT_MODEL, weight_name=CHECKPOINT_FILENAME, subfolder="")
|
| 23 |
+
pipe.to(device)
|
| 24 |
+
print(f"Model loaded successfully on {device}")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error loading model on {device}: {e}")
|
| 27 |
+
# Fallback to CPU if GPU loading fails or if no GPU
|
| 28 |
+
try:
|
| 29 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 30 |
+
BASE_MODEL,
|
| 31 |
+
torch_dtype=torch.float32,
|
| 32 |
+
use_safetensors=True
|
| 33 |
+
)
|
| 34 |
+
pipe.load_lora_weights(CHECKPOINT_MODEL, weight_name=CHECKPOINT_FILENAME, subfolder="")
|
| 35 |
+
pipe.to("cpu")
|
| 36 |
+
print("Model forced loaded on CPU due to previous error or no GPU.")
|
| 37 |
+
except Exception as e_cpu:
|
| 38 |
+
print(f"Critical error: Failed to load model even on CPU: {e_cpu}")
|
| 39 |
+
exit()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# 3. Define the Gradio inference function
|
| 43 |
+
def generate_image(prompt, negative_prompt, num_inference_steps, guidance_scale, seed):
|
| 44 |
+
if not prompt:
|
| 45 |
+
return None, "Please enter a prompt."
|
| 46 |
+
|
| 47 |
+
generator = torch.Generator(device=device).manual_seed(seed) if seed != -1 else None
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
image = pipe(
|
| 51 |
+
prompt=prompt,
|
| 52 |
+
negative_prompt=negative_prompt,
|
| 53 |
+
num_inference_steps=int(num_inference_steps),
|
| 54 |
+
guidance_scale=float(guidance_scale),
|
| 55 |
+
generator=generator
|
| 56 |
+
).images[0]
|
| 57 |
+
return image, None
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return None, f"Error during image generation: {e}"
|
| 60 |
+
|
| 61 |
+
# 4. Create the Gradio Interface
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=generate_image,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Textbox(label="Prompt", placeholder="A furry creature with indigo fur, in a magical forest, whimsical art, highly detailed"),
|
| 66 |
+
gr.Textbox(label="Negative Prompt (optional)", placeholder="blurry, ugly, deformed, low quality, bad anatomy"),
|
| 67 |
+
gr.Slider(minimum=10, maximum=100, value=25, step=1, label="Inference Steps"),
|
| 68 |
+
gr.Slider(minimum=1.0, maximum=20.0, value=7.5, step=0.5, label="Guidance Scale"),
|
| 69 |
+
gr.Number(label="Seed (-1 for random)", value=-1, precision=0)
|
| 70 |
+
],
|
| 71 |
+
outputs=[
|
| 72 |
+
gr.Image(type="pil", label="Generated Image"),
|
| 73 |
+
gr.Textbox(label="Status/Error")
|
| 74 |
+
],
|
| 75 |
+
title="Indigo Furry Mix - Text-to-Image Generator",
|
| 76 |
+
description="Generate images of furry characters with the Indigo Furry Mix model.",
|
| 77 |
+
examples=[
|
| 78 |
+
["An indigo furry warrior, wielding a glowing sword, intricate armor, epic fantasy art", "blurry, low quality"],
|
| 79 |
+
["A cute indigo furry wizard casting a spell, magical effects, cartoon style", "disfigured, ugly"],
|
| 80 |
+
]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
iface.launch()
|