onnew commited on
Commit
6227457
·
verified ·
1 Parent(s): a59227d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -1,27 +1,21 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
-
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
  import torch
 
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
 
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
 
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
 
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
  def infer(
26
  prompt,
27
  negative_prompt,
@@ -33,11 +27,13 @@ def infer(
33
  num_inference_steps,
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
 
36
  if randomize_seed:
37
  seed = random.randint(0, MAX_SEED)
38
 
39
- generator = torch.Generator().manual_seed(seed)
40
 
 
41
  image = pipe(
42
  prompt=prompt,
43
  negative_prompt=negative_prompt,
@@ -50,7 +46,6 @@ def infer(
50
 
51
  return image, seed
52
 
53
-
54
  examples = [
55
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
  "An astronaut riding a green horse",
@@ -105,7 +100,7 @@ with gr.Blocks(css=css) as demo:
105
  minimum=256,
106
  maximum=MAX_IMAGE_SIZE,
107
  step=32,
108
- value=1024, # Replace with defaults that work for your model
109
  )
110
 
111
  height = gr.Slider(
@@ -113,7 +108,7 @@ with gr.Blocks(css=css) as demo:
113
  minimum=256,
114
  maximum=MAX_IMAGE_SIZE,
115
  step=32,
116
- value=1024, # Replace with defaults that work for your model
117
  )
118
 
119
  with gr.Row():
@@ -122,18 +117,19 @@ with gr.Blocks(css=css) as demo:
122
  minimum=0.0,
123
  maximum=10.0,
124
  step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
 
128
  num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
  minimum=1,
131
  maximum=50,
132
  step=1,
133
- value=2, # Replace with defaults that work for your model
134
  )
135
 
136
  gr.Examples(examples=examples, inputs=[prompt])
 
137
  gr.on(
138
  triggers=[run_button.click, prompt.submit],
139
  fn=infer,
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
 
 
4
  import torch
5
+ from diffusers import DiffusionPipeline
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ model_repo_id = "stabilityai/sdxl-turbo" # Modelo de exemplo (ajuste conforme necessário)
9
 
10
+ # Usando torch.float16 para melhorar a performance com GPUs (se disponível)
11
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
 
12
 
13
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)
 
14
 
15
+ # Definindo parâmetros máximos
16
  MAX_SEED = np.iinfo(np.int32).max
17
  MAX_IMAGE_SIZE = 1024
18
 
 
 
19
  def infer(
20
  prompt,
21
  negative_prompt,
 
27
  num_inference_steps,
28
  progress=gr.Progress(track_tqdm=True),
29
  ):
30
+ # Geração de seed aleatória (caso solicitado)
31
  if randomize_seed:
32
  seed = random.randint(0, MAX_SEED)
33
 
34
+ generator = torch.Generator(device).manual_seed(seed)
35
 
36
+ # Definindo uma resolução menor para acelerar a execução (ajuste conforme necessário)
37
  image = pipe(
38
  prompt=prompt,
39
  negative_prompt=negative_prompt,
 
46
 
47
  return image, seed
48
 
 
49
  examples = [
50
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
51
  "An astronaut riding a green horse",
 
100
  minimum=256,
101
  maximum=MAX_IMAGE_SIZE,
102
  step=32,
103
+ value=1024, # Valor de default
104
  )
105
 
106
  height = gr.Slider(
 
108
  minimum=256,
109
  maximum=MAX_IMAGE_SIZE,
110
  step=32,
111
+ value=1024, # Valor de default
112
  )
113
 
114
  with gr.Row():
 
117
  minimum=0.0,
118
  maximum=10.0,
119
  step=0.1,
120
+ value=7.5, # Valor de default mais alto para mais controle
121
  )
122
 
123
  num_inference_steps = gr.Slider(
124
+ label="Inference steps",
125
  minimum=1,
126
  maximum=50,
127
  step=1,
128
+ value=20, # Aumenta para melhorar a qualidade se necessário
129
  )
130
 
131
  gr.Examples(examples=examples, inputs=[prompt])
132
+
133
  gr.on(
134
  triggers=[run_button.click, prompt.submit],
135
  fn=infer,