eder0782 commited on
Commit
f611d13
verified
1 Parent(s): a8f57f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -41
app.py CHANGED
@@ -1,13 +1,12 @@
1
-
2
- # from huggingface_hub import login
3
- # # Login com token via Secrets
4
- # login(os.environ["HF_TOKEN"])
5
  import gradio as gr
6
  import numpy as np
7
  import random
8
  import spaces
9
  import torch
10
  from diffusers import DiffusionPipeline
 
 
 
11
 
12
  dtype = torch.bfloat16
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -17,28 +16,46 @@ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", tor
17
  MAX_SEED = np.iinfo(np.int32).max
18
  MAX_IMAGE_SIZE = 2048
19
 
 
 
 
 
20
  @spaces.GPU()
21
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
22
  if randomize_seed:
23
  seed = random.randint(0, MAX_SEED)
24
  generator = torch.Generator().manual_seed(seed)
 
 
25
  image = pipe(
26
- prompt = prompt,
27
- width = width,
28
- height = height,
29
- num_inference_steps = num_inference_steps,
30
- generator = generator,
31
- guidance_scale=0.0
32
- ).images[0]
33
- return image, seed
34
-
 
 
 
 
 
 
 
 
 
 
 
 
35
  examples = [
36
  "a tiny astronaut hatching from an egg on the moon",
37
  "a cat holding a sign that says hello world",
38
  "an anime illustration of a wiener schnitzel",
39
  ]
40
 
41
- css="""
42
  #col-container {
43
  margin: 0 auto;
44
  max-width: 520px;
@@ -46,7 +63,6 @@ css="""
46
  """
47
 
48
  with gr.Blocks(css=css) as demo:
49
-
50
  with gr.Column(elem_id="col-container"):
51
  gr.Markdown(f"""# FLUX.1 [schnell]
52
  12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
@@ -54,7 +70,6 @@ with gr.Blocks(css=css) as demo:
54
  """)
55
 
56
  with gr.Row():
57
-
58
  prompt = gr.Text(
59
  label="Prompt",
60
  show_label=False,
@@ -62,13 +77,11 @@ with gr.Blocks(css=css) as demo:
62
  placeholder="Enter your prompt",
63
  container=False,
64
  )
65
-
66
  run_button = gr.Button("Run", scale=0)
67
 
68
  result = gr.Image(label="Result", show_label=False)
69
 
70
  with gr.Accordion("Advanced Settings", open=False):
71
-
72
  seed = gr.Slider(
73
  label="Seed",
74
  minimum=0,
@@ -76,11 +89,8 @@ with gr.Blocks(css=css) as demo:
76
  step=1,
77
  value=0,
78
  )
79
-
80
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
81
-
82
  with gr.Row():
83
-
84
  width = gr.Slider(
85
  label="Width",
86
  minimum=256,
@@ -88,7 +98,6 @@ with gr.Blocks(css=css) as demo:
88
  step=32,
89
  value=1024,
90
  )
91
-
92
  height = gr.Slider(
93
  label="Height",
94
  minimum=256,
@@ -96,31 +105,32 @@ with gr.Blocks(css=css) as demo:
96
  step=32,
97
  value=1024,
98
  )
99
-
100
- with gr.Row():
101
-
102
-
103
- num_inference_steps = gr.Slider(
104
- label="Number of inference steps",
105
- minimum=1,
106
- maximum=50,
107
- step=1,
108
- value=4,
109
- )
110
 
111
  gr.Examples(
112
- examples = examples,
113
- fn = infer,
114
- inputs = [prompt],
115
- outputs = [result, seed],
116
  cache_examples="lazy"
117
  )
118
 
 
 
 
 
119
  gr.on(
120
  triggers=[run_button.click, prompt.submit],
121
- fn = infer,
122
- inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
123
- outputs = [result, seed]
 
124
  )
125
 
126
- demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
7
+ import os
8
+ import uuid
9
+ from datetime import datetime
10
 
11
  dtype = torch.bfloat16
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
16
  MAX_SEED = np.iinfo(np.int32).max
17
  MAX_IMAGE_SIZE = 2048
18
 
19
+ # Diret贸rio para salvar imagens (use /tmp para Hugging Face Spaces)
20
+ OUTPUT_DIR = "/tmp"
21
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
22
+
23
  @spaces.GPU()
24
  def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
  generator = torch.Generator().manual_seed(seed)
28
+
29
+ # Gerar a imagem
30
  image = pipe(
31
+ prompt=prompt,
32
+ width=width,
33
+ height=height,
34
+ num_inference_steps=num_inference_steps,
35
+ generator=generator,
36
+ guidance_scale=0.0
37
+ ).images[0]
38
+
39
+ # Gerar nome de arquivo 煤nico
40
+ filename = f"image_{uuid.uuid4().hex}.png"
41
+ filepath = os.path.join(OUTPUT_DIR, filename)
42
+
43
+ # Salvar a imagem
44
+ image.save(filepath)
45
+
46
+ # Construir o URL p煤blico (ajuste conforme a URL do seu Space)
47
+ space_url = "https://eder0782-flux-image-generator.hf.space"
48
+ image_url = f"{space_url}/file/{filename}"
49
+
50
+ return {"image_url": image_url, "seed": seed}
51
+
52
  examples = [
53
  "a tiny astronaut hatching from an egg on the moon",
54
  "a cat holding a sign that says hello world",
55
  "an anime illustration of a wiener schnitzel",
56
  ]
57
 
58
+ css = """
59
  #col-container {
60
  margin: 0 auto;
61
  max-width: 520px;
 
63
  """
64
 
65
  with gr.Blocks(css=css) as demo:
 
66
  with gr.Column(elem_id="col-container"):
67
  gr.Markdown(f"""# FLUX.1 [schnell]
68
  12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
 
70
  """)
71
 
72
  with gr.Row():
 
73
  prompt = gr.Text(
74
  label="Prompt",
75
  show_label=False,
 
77
  placeholder="Enter your prompt",
78
  container=False,
79
  )
 
80
  run_button = gr.Button("Run", scale=0)
81
 
82
  result = gr.Image(label="Result", show_label=False)
83
 
84
  with gr.Accordion("Advanced Settings", open=False):
 
85
  seed = gr.Slider(
86
  label="Seed",
87
  minimum=0,
 
89
  step=1,
90
  value=0,
91
  )
 
92
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
93
  with gr.Row():
 
94
  width = gr.Slider(
95
  label="Width",
96
  minimum=256,
 
98
  step=32,
99
  value=1024,
100
  )
 
101
  height = gr.Slider(
102
  label="Height",
103
  minimum=256,
 
105
  step=32,
106
  value=1024,
107
  )
108
+ num_inference_steps = gr.Slider(
109
+ label="Number of inference steps",
110
+ minimum=1,
111
+ maximum=50,
112
+ step=1,
113
+ value=4,
114
+ )
 
 
 
 
115
 
116
  gr.Examples(
117
+ examples=examples,
118
+ fn=infer,
119
+ inputs=[prompt],
120
+ outputs=[result, seed],
121
  cache_examples="lazy"
122
  )
123
 
124
+ # Ajustar a sa铆da para a interface
125
+ def format_output(output):
126
+ return output["image_url"], output["seed"]
127
+
128
  gr.on(
129
  triggers=[run_button.click, prompt.submit],
130
+ fn=infer,
131
+ inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
132
+ outputs=[result, seed],
133
+ _js=format_output
134
  )
135
 
136
+ demo.launch()