Rojban commited on
Commit
548ceda
Β·
1 Parent(s): 0e7f518

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,40 +1,47 @@
1
  import gradio as gr
2
  import os
 
3
  from diffusers import DiffusionPipeline, StableDiffusionXLImg2ImgPipeline
4
  import torch
 
 
5
 
6
- # Load your custom Stable Diffusion model
7
  token = os.getenv("token")
8
- model = gr.load("models/Rojban/AutoTrain_Dreambooth3", hf_token=token)
 
 
 
 
 
 
 
9
 
10
- # Define the image generation function
11
- def generate_image(prompt, seed=42):
12
- model_name = "stabilityai/stable-diffusion-xl-base-1.0"
13
- pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16)
14
- pipe.to("cuda")
15
 
16
- refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(
17
- "stabilityai/stable-diffusion-xl-refiner-1.0",
18
- torch_dtype=torch.float16,
19
- )
20
- refiner.to("cuda")
21
 
 
 
 
22
  generator = torch.Generator("cuda").manual_seed(seed)
23
  image = pipe(prompt=prompt, generator=generator).images[0]
24
  image = refiner(prompt=prompt, generator=generator, image=image).images[0]
 
 
 
 
25
 
26
- # Save and return the image
27
- image_path = "generated_image.png"
28
- image.save(image_path)
29
- return image_path
30
 
31
  # Create the Gradio interface
32
  interface = gr.Interface(
33
  fn=generate_image,
34
  inputs=[gr.Textbox(label="Prompt"), gr.Number(label="Seed")],
35
- outputs=gr.Image(type="file"),
36
  title="Custom Stable Diffusion Model",
37
- description="Generate images using a custom Stable Diffusion model."
38
  )
39
 
40
  # Launch the app
 
1
  import gradio as gr
2
  import os
3
+
4
  from diffusers import DiffusionPipeline, StableDiffusionXLImg2ImgPipeline
5
  import torch
6
+ import random
7
+ import uuid
8
 
 
9
  token = os.getenv("token")
10
+ model = gr.load("models/Rojban/dreambooth4", hf_token=token)
11
+
12
+ pipe = DiffusionPipeline.from_pretrained(
13
+ model,
14
+ torch_dtype=torch.float16,
15
+ )
16
+ pipe.to("cuda")
17
+ pipe.load_lora_weights("Rojban/dreambooth4", weight_name="pytorch_lora_weights.safetensors")
18
 
19
+ refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(
20
+ "stabilityai/stable-diffusion-xl-refiner-1.0",
21
+ torch_dtype=torch.float16,
22
+ )
23
+ refiner.to("cuda")
24
 
 
 
 
 
 
25
 
26
+ def generate_image(prompt, seed=None):
27
+ if seed is None:
28
+ seed = 253
29
  generator = torch.Generator("cuda").manual_seed(seed)
30
  image = pipe(prompt=prompt, generator=generator).images[0]
31
  image = refiner(prompt=prompt, generator=generator, image=image).images[0]
32
+ name = f"{seed}_{str(uuid.uuid4())}.png"
33
+ save_path = f"images/{name}"
34
+ image.save(save_path)
35
+ return save_path
36
 
 
 
 
 
37
 
38
  # Create the Gradio interface
39
  interface = gr.Interface(
40
  fn=generate_image,
41
  inputs=[gr.Textbox(label="Prompt"), gr.Number(label="Seed")],
42
+ outputs=gr.Image(type="filepath"),
43
  title="Custom Stable Diffusion Model",
44
+ description="Generate images using a custom Stable Diffusion model.",
45
  )
46
 
47
  # Launch the app