Leofreddare commited on
Commit
333e175
·
verified ·
1 Parent(s): 7e9a7be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -21
app.py CHANGED
@@ -1,35 +1,98 @@
1
  import gradio as gr
2
- import torch
 
3
  from diffusers import StableDiffusionXLPipeline
 
4
  from huggingface_hub import hf_hub_download
5
 
6
- # Set device: use "cuda" if available; otherwise "cpu"
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- # Load the SDXL base pipeline; adjust torch_dtype for CPU if necessary.
 
 
 
 
 
 
 
 
10
  pipe = StableDiffusionXLPipeline.from_pretrained(
11
- "stabilityai/stable-diffusion-xl-base-1.0",
12
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
13
  use_safetensors=True
14
  )
15
- pipe.to(device)
16
 
17
- # Download your LoRA weights from your Hugging Face repository.
18
  lora_path = hf_hub_download(repo_id="Leofreddare/DreamCartoonLora", filename="DreamCartoonLora.safetensors")
19
- print("LoRA weights downloaded to:", lora_path)
20
-
21
- # Load the LoRA weights into the pipeline.
22
  pipe.load_lora_weights(lora_path)
23
 
24
- def generate(prompt):
25
- with torch.no_grad():
26
- image = pipe(prompt=prompt).images[0]
27
- return image
28
-
29
- gr.Interface(
30
- fn=generate,
31
- inputs=gr.Textbox(label="Enter your prompt"),
32
- outputs="image",
33
- title="DreamCartoonLora Inference"
34
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
  from diffusers import StableDiffusionXLPipeline
5
+ import torch
6
  from huggingface_hub import hf_hub_download
7
 
8
+ # Set device: use "cuda" if available, otherwise "cpu"
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
+ # Change the model to SDXL 1.0 base
12
+ model_repo_id = "stabilityai/stable-diffusion-xl-base-1.0"
13
+
14
+ if torch.cuda.is_available():
15
+ torch_dtype = torch.float16
16
+ else:
17
+ torch_dtype = torch.float32
18
+
19
+ # Load the SDXL 1.0 base pipeline with safetensors support.
20
  pipe = StableDiffusionXLPipeline.from_pretrained(
21
+ model_repo_id,
22
+ torch_dtype=torch_dtype,
23
  use_safetensors=True
24
  )
25
+ pipe = pipe.to(device)
26
 
27
+ # Download your DreamCartoonLora weights from Hugging Face and load them into the pipeline.
28
  lora_path = hf_hub_download(repo_id="Leofreddare/DreamCartoonLora", filename="DreamCartoonLora.safetensors")
29
+ print("Loaded DreamCartoonLora from:", lora_path)
 
 
30
  pipe.load_lora_weights(lora_path)
31
 
32
+ MAX_SEED = np.iinfo(np.int32).max
33
+ MAX_IMAGE_SIZE = 1024
34
+
35
+ def infer(
36
+ prompt,
37
+ negative_prompt,
38
+ seed,
39
+ randomize_seed,
40
+ width,
41
+ height,
42
+ guidance_scale,
43
+ num_inference_steps,
44
+ progress=gr.Progress(track_tqdm=True),
45
+ ):
46
+ if randomize_seed:
47
+ seed = random.randint(0, MAX_SEED)
48
+ generator = torch.Generator(device=device).manual_seed(seed)
49
+ image = pipe(
50
+ prompt=prompt,
51
+ negative_prompt=negative_prompt,
52
+ guidance_scale=guidance_scale,
53
+ num_inference_steps=num_inference_steps,
54
+ width=width,
55
+ height=height,
56
+ generator=generator,
57
+ ).images[0]
58
+ return image, seed
59
+
60
+ examples = [
61
+ "A dreamy cartoon landscape with vivid colors",
62
+ "A futuristic city rendered in a cartoon style",
63
+ "A magical forest with a cartoon twist",
64
+ ]
65
+
66
+ css = """
67
+ #col-container {
68
+ margin: 0 auto;
69
+ max-width: 640px;
70
+ }
71
+ """
72
+
73
+ with gr.Blocks(css=css) as demo:
74
+ with gr.Column(elem_id="col-container"):
75
+ gr.Markdown("# Text-to-Image with DreamCartoonLora on SDXL 1.0")
76
+ with gr.Row():
77
+ prompt = gr.Text(label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False)
78
+ run_button = gr.Button("Run", scale=0, variant="primary")
79
+ result = gr.Image(label="Result", show_label=False)
80
+ with gr.Accordion("Advanced Settings", open=False):
81
+ negative_prompt = gr.Text(label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", visible=False)
82
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
83
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
84
+ with gr.Row():
85
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
86
+ height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
87
+ with gr.Row():
88
+ guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
89
+ num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=20)
90
+ gr.Examples(examples=examples, inputs=[prompt])
91
+ gr.on(triggers=[run_button.click, prompt.submit],
92
+ fn=infer,
93
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
94
+ outputs=[result, seed])
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()
98