kadirnar commited on
Commit
cb757a5
1 Parent(s): 9c6a12c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  import random
4
  import uuid
@@ -8,18 +7,28 @@ import numpy as np
8
  from PIL import Image
9
  import spaces
10
  import torch
11
- from diffusers import StableDiffusion3Pipeline, DPMSolverSinglestepScheduler, AutoencoderKL
12
  from huggingface_hub import snapshot_download
13
 
 
14
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
15
 
16
- snapshot_download(
 
 
 
 
17
  repo_id="stabilityai/stable-diffusion-3-medium",
18
  repo_type="model",
19
  ignore_patterns=["*.md", "*..gitattributes"],
20
  local_dir="stable-diffusion-3-medium",
21
- token=huggingface_token, # yeni bir token-id yazın.
22
- )
 
 
 
 
 
23
 
24
  DESCRIPTION = """# Stable Diffusion 3"""
25
  if not torch.cuda.is_available():
@@ -32,23 +41,24 @@ USE_TORCH_COMPILE = False
32
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
33
 
34
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
35
- vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
36
 
37
- pipe = StableDiffusion3Pipeline.from_pretrained("stable-diffusion-3-medium", torch_dtype=torch.float16)
38
-
 
 
 
 
39
 
40
  def save_image(img):
41
  unique_name = str(uuid.uuid4()) + ".png"
42
  img.save(unique_name)
43
  return unique_name
44
 
45
-
46
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
47
  if randomize_seed:
48
  seed = random.randint(0, MAX_SEED)
49
  return seed
50
 
51
-
52
  @spaces.GPU(enable_queue=True)
53
  def generate(
54
  prompt: str,
@@ -64,12 +74,9 @@ def generate(
64
  use_resolution_binning: bool = True,
65
  progress=gr.Progress(track_tqdm=True),
66
  ):
67
- pipe.to(device)
68
  seed = int(randomize_seed_fn(seed, randomize_seed))
69
  generator = torch.Generator().manual_seed(seed)
70
- #pipe.scheduler = DPMSolverSinglestepScheduler(use_karras_sigmas=True).from_config(pipe.scheduler.config)
71
- #pipe.scheduler = DPMSolverMultistepScheduler(algorithm_type="sde-dpmsolver++").from_config(pipe.scheduler.config)
72
-
73
  if not use_negative_prompt:
74
  negative_prompt = None # type: ignore
75
 
@@ -87,7 +94,6 @@ def generate(
87
 
88
  return output
89
 
90
-
91
  examples = [
92
  "neon holography crystal cat",
93
  "a cat eating a piece of cheese",
@@ -101,6 +107,7 @@ css = '''
101
  .gradio-container{max-width: 1000px !important}
102
  h1{text-align:center}
103
  '''
 
104
  with gr.Blocks(css=css) as demo:
105
  with gr.Row():
106
  with gr.Column():
@@ -136,7 +143,7 @@ with gr.Blocks(css=css) as demo:
136
  negative_prompt = gr.Text(
137
  label="Negative prompt",
138
  max_lines=1,
139
- value = "deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
140
  visible=True,
141
  )
142
  seed = gr.Slider(
@@ -225,4 +232,4 @@ with gr.Blocks(css=css) as demo:
225
  )
226
 
227
  if __name__ == "__main__":
228
- demo.queue().launch()
 
 
1
  import os
2
  import random
3
  import uuid
 
7
  from PIL import Image
8
  import spaces
9
  import torch
10
+ from diffusers import StableDiffusionPipeline, DPMSolverSinglestepScheduler, AutoencoderKL
11
  from huggingface_hub import snapshot_download
12
 
13
+ # Çevre değişkeninden token'ı al
14
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
15
 
16
+ if huggingface_token is None:
17
+ raise ValueError("HUGGINGFACE_TOKEN environment variable is not set.")
18
+
19
+ # Model ve VAE dosyalarını indir
20
+ model_path = snapshot_download(
21
  repo_id="stabilityai/stable-diffusion-3-medium",
22
  repo_type="model",
23
  ignore_patterns=["*.md", "*..gitattributes"],
24
  local_dir="stable-diffusion-3-medium",
25
+ token=huggingface_token,
26
+ )
27
+
28
+ vae_path = snapshot_download(
29
+ repo_id="madebyollin/sdxl-vae-fp16-fix",
30
+ token=huggingface_token
31
+ )
32
 
33
  DESCRIPTION = """# Stable Diffusion 3"""
34
  if not torch.cuda.is_available():
 
41
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
42
 
43
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 
44
 
45
+ # VAE modelini yükle
46
+ vae = AutoencoderKL.from_pretrained(vae_path, torch_dtype=torch.float16)
47
+
48
+ # Stable Diffusion pipeline'ı yükle
49
+ pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
50
+ pipe.to(device)
51
 
52
  def save_image(img):
53
  unique_name = str(uuid.uuid4()) + ".png"
54
  img.save(unique_name)
55
  return unique_name
56
 
 
57
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
58
  if randomize_seed:
59
  seed = random.randint(0, MAX_SEED)
60
  return seed
61
 
 
62
  @spaces.GPU(enable_queue=True)
63
  def generate(
64
  prompt: str,
 
74
  use_resolution_binning: bool = True,
75
  progress=gr.Progress(track_tqdm=True),
76
  ):
 
77
  seed = int(randomize_seed_fn(seed, randomize_seed))
78
  generator = torch.Generator().manual_seed(seed)
79
+
 
 
80
  if not use_negative_prompt:
81
  negative_prompt = None # type: ignore
82
 
 
94
 
95
  return output
96
 
 
97
  examples = [
98
  "neon holography crystal cat",
99
  "a cat eating a piece of cheese",
 
107
  .gradio-container{max-width: 1000px !important}
108
  h1{text-align:center}
109
  '''
110
+
111
  with gr.Blocks(css=css) as demo:
112
  with gr.Row():
113
  with gr.Column():
 
143
  negative_prompt = gr.Text(
144
  label="Negative prompt",
145
  max_lines=1,
146
+ value="deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
147
  visible=True,
148
  )
149
  seed = gr.Slider(
 
232
  )
233
 
234
  if __name__ == "__main__":
235
+ demo.queue().launch()