Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
-
from pathlib import Path
|
| 5 |
from main import fine_tune_model
|
| 6 |
from diffusers import StableDiffusionPipeline, DDIMScheduler
|
| 7 |
import torch
|
|
@@ -9,31 +8,41 @@ import torch
|
|
| 9 |
MODEL_NAME = "runwayml/stable-diffusion-v1-5"
|
| 10 |
OUTPUT_DIR = "/home/user/app/stable_diffusion_weights/custom_model"
|
| 11 |
|
| 12 |
-
def fine_tune(instance_prompt, image1, image2):
|
| 13 |
instance_data_dir = "/home/user/app/instance_images"
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def generate_images(prompt, num_samples, height, width, num_inference_steps, guidance_scale):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
).
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def gradio_app():
|
| 39 |
with gr.Blocks() as demo:
|
|
@@ -64,4 +73,4 @@ def gradio_app():
|
|
| 64 |
demo.launch()
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
gradio_app()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import shutil
|
|
|
|
| 4 |
from main import fine_tune_model
|
| 5 |
from diffusers import StableDiffusionPipeline, DDIMScheduler
|
| 6 |
import torch
|
|
|
|
| 8 |
MODEL_NAME = "runwayml/stable-diffusion-v1-5"
|
| 9 |
OUTPUT_DIR = "/home/user/app/stable_diffusion_weights/custom_model"
|
| 10 |
|
| 11 |
+
def fine_tune(instance_prompt, image1, image2=None):
|
| 12 |
instance_data_dir = "/home/user/app/instance_images"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
if os.path.exists(instance_data_dir):
|
| 16 |
+
shutil.rmtree(instance_data_dir)
|
| 17 |
+
os.makedirs(instance_data_dir, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
image1.save(os.path.join(instance_data_dir, "instance_0.png"))
|
| 20 |
+
if image2 is not None:
|
| 21 |
+
image2.save(os.path.join(instance_data_dir, "instance_1.png"))
|
| 22 |
+
|
| 23 |
+
fine_tune_model(instance_data_dir, instance_prompt, MODEL_NAME, OUTPUT_DIR)
|
| 24 |
+
return "Model fine-tuning complete."
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return str(e)
|
| 27 |
|
| 28 |
def generate_images(prompt, num_samples, height, width, num_inference_steps, guidance_scale):
|
| 29 |
+
try:
|
| 30 |
+
if not os.path.exists(OUTPUT_DIR):
|
| 31 |
+
return "The model path does not exist."
|
| 32 |
+
|
| 33 |
+
pipe = StableDiffusionPipeline.from_pretrained(OUTPUT_DIR, safety_checker=None, torch_dtype=torch.float16).to("cuda")
|
| 34 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
| 35 |
+
g_cuda = torch.Generator(device='cuda').manual_seed(1337)
|
| 36 |
+
|
| 37 |
+
with torch.autocast("cuda"), torch.inference_mode():
|
| 38 |
+
images = pipe(
|
| 39 |
+
prompt, height=height, width=width, num_images_per_prompt=num_samples,
|
| 40 |
+
num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, generator=g_cuda
|
| 41 |
+
).images
|
| 42 |
+
|
| 43 |
+
return images
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return str(e)
|
| 46 |
|
| 47 |
def gradio_app():
|
| 48 |
with gr.Blocks() as demo:
|
|
|
|
| 73 |
demo.launch()
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
gradio_app()
|