kadirnar commited on
Commit
2928f1f
1 Parent(s): f04e854

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import random
3
  import uuid
@@ -7,23 +9,19 @@ import numpy as np
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
- # Model ve VAE dosyalarını indir
17
- model_path = snapshot_download(
18
  repo_id="stabilityai/stable-diffusion-3-medium",
19
  repo_type="model",
20
  ignore_patterns=["*.md", "*..gitattributes"],
21
  local_dir="stable-diffusion-3-medium",
22
- token=huggingface_token,
23
- )
24
- print(model_path)
25
 
26
- """
27
  DESCRIPTION = """# Stable Diffusion 3"""
28
  if not torch.cuda.is_available():
29
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
@@ -35,24 +33,23 @@ USE_TORCH_COMPILE = False
35
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
36
 
37
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 
38
 
39
- pipe = StableDiffusionPipeline.from_single_file(
40
- "https://huggingface.co/stabilityai/stable-diffusion-3-medium/blob/main/tachyon.safetensors",
41
- torch_dtype=torch.float16,
42
- use_auth_token=huggingface_token
43
- )
44
- pipe.to(device)
45
 
46
  def save_image(img):
47
  unique_name = str(uuid.uuid4()) + ".png"
48
  img.save(unique_name)
49
  return unique_name
50
 
 
51
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
52
  if randomize_seed:
53
  seed = random.randint(0, MAX_SEED)
54
  return seed
55
 
 
56
  @spaces.GPU(enable_queue=True)
57
  def generate(
58
  prompt: str,
@@ -68,9 +65,12 @@ def generate(
68
  use_resolution_binning: bool = True,
69
  progress=gr.Progress(track_tqdm=True),
70
  ):
 
71
  seed = int(randomize_seed_fn(seed, randomize_seed))
72
  generator = torch.Generator().manual_seed(seed)
73
-
 
 
74
  if not use_negative_prompt:
75
  negative_prompt = None # type: ignore
76
 
@@ -88,6 +88,7 @@ def generate(
88
 
89
  return output
90
 
 
91
  examples = [
92
  "neon holography crystal cat",
93
  "a cat eating a piece of cheese",
@@ -101,7 +102,6 @@ css = '''
101
  .gradio-container{max-width: 1000px !important}
102
  h1{text-align:center}
103
  '''
104
-
105
  with gr.Blocks(css=css) as demo:
106
  with gr.Row():
107
  with gr.Column():
@@ -137,7 +137,7 @@ with gr.Blocks(css=css) as demo:
137
  negative_prompt = gr.Text(
138
  label="Negative prompt",
139
  max_lines=1,
140
- 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",
141
  visible=True,
142
  )
143
  seed = gr.Slider(
@@ -226,4 +226,4 @@ with gr.Blocks(css=css) as demo:
226
  )
227
 
228
  if __name__ == "__main__":
229
- demo.queue().launch()
 
1
+
2
+
3
  import os
4
  import random
5
  import uuid
 
9
  from PIL import Image
10
  import spaces
11
  import torch
12
+ from diffusers import StableDiffusion3Pipeline, DPMSolverSinglestepScheduler, AutoencoderKL
13
  from huggingface_hub import snapshot_download
14
 
 
15
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
16
+
17
+ snapshot_download(
 
18
  repo_id="stabilityai/stable-diffusion-3-medium",
19
  repo_type="model",
20
  ignore_patterns=["*.md", "*..gitattributes"],
21
  local_dir="stable-diffusion-3-medium",
22
+ token=huggingface_token, # yeni bir token-id yazın.
23
+ )
 
24
 
 
25
  DESCRIPTION = """# Stable Diffusion 3"""
26
  if not torch.cuda.is_available():
27
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
 
33
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
34
 
35
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
36
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
37
 
38
+ pipe = StableDiffusion3Pipeline.from_pretrained("stable-diffusion-3-medium", torch_dtype=torch.float16)
39
+
 
 
 
 
40
 
41
  def save_image(img):
42
  unique_name = str(uuid.uuid4()) + ".png"
43
  img.save(unique_name)
44
  return unique_name
45
 
46
+
47
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
48
  if randomize_seed:
49
  seed = random.randint(0, MAX_SEED)
50
  return seed
51
 
52
+
53
  @spaces.GPU(enable_queue=True)
54
  def generate(
55
  prompt: str,
 
65
  use_resolution_binning: bool = True,
66
  progress=gr.Progress(track_tqdm=True),
67
  ):
68
+ pipe.to(device)
69
  seed = int(randomize_seed_fn(seed, randomize_seed))
70
  generator = torch.Generator().manual_seed(seed)
71
+ #pipe.scheduler = DPMSolverSinglestepScheduler(use_karras_sigmas=True).from_config(pipe.scheduler.config)
72
+ #pipe.scheduler = DPMSolverMultistepScheduler(algorithm_type="sde-dpmsolver++").from_config(pipe.scheduler.config)
73
+
74
  if not use_negative_prompt:
75
  negative_prompt = None # type: ignore
76
 
 
88
 
89
  return output
90
 
91
+
92
  examples = [
93
  "neon holography crystal cat",
94
  "a cat eating a piece of cheese",
 
102
  .gradio-container{max-width: 1000px !important}
103
  h1{text-align:center}
104
  '''
 
105
  with gr.Blocks(css=css) as demo:
106
  with gr.Row():
107
  with gr.Column():
 
137
  negative_prompt = gr.Text(
138
  label="Negative prompt",
139
  max_lines=1,
140
+ 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",
141
  visible=True,
142
  )
143
  seed = gr.Slider(
 
226
  )
227
 
228
  if __name__ == "__main__":
229
+ demo.queue().launch()