Singularity666 commited on
Commit
2c282ba
·
verified ·
1 Parent(s): 7f5929e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
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
- if os.path.exists(instance_data_dir):
15
- shutil.rmtree(instance_data_dir)
16
- os.makedirs(instance_data_dir, exist_ok=True)
17
-
18
- image1.save(os.path.join(instance_data_dir, "instance_0.png"))
19
- if image2 is not None:
20
- image2.save(os.path.join(instance_data_dir, "instance_1.png"))
21
-
22
- fine_tune_model(instance_data_dir, instance_prompt, MODEL_NAME, OUTPUT_DIR)
23
- return "Model fine-tuning complete."
 
 
 
 
24
 
25
  def generate_images(prompt, num_samples, height, width, num_inference_steps, guidance_scale):
26
- pipe = StableDiffusionPipeline.from_pretrained(OUTPUT_DIR, safety_checker=None, torch_dtype=torch.float16).to("cuda")
27
- pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
28
- g_cuda = torch.Generator(device='cuda').manual_seed(1337)
29
-
30
- with torch.autocast("cuda"), torch.inference_mode():
31
- images = pipe(
32
- prompt, height=height, width=width, num_images_per_prompt=num_samples,
33
- num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, generator=g_cuda
34
- ).images
35
-
36
- return images
 
 
 
 
 
 
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()