veasnakao commited on
Commit
f667891
·
verified ·
1 Parent(s): 2e6b874

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -22
app.py CHANGED
@@ -1,27 +1,122 @@
1
  import gradio as gr
2
- from PIL import Image, ImageDraw
3
- import io
4
-
5
- # Function to generate an image from a prompt
6
- def generate_image(prompt):
7
- try:
8
- # Create a simple image with a blue background and text
9
- img = Image.new('RGB', (256, 256), color='blue')
10
- draw = ImageDraw.Draw(img)
11
- draw.text((10, 10), prompt, fill='white')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Convert the PIL image to a format Gradio can use
14
- buffered = io.BytesIO()
15
- img.save(buffered, format="PNG")
16
- img_bytes = buffered.getvalue()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Return the image as a PIL Image object for Gradio
19
- return Image.open(io.BytesIO(img_bytes))
20
- except Exception as e:
21
- return f"Error: {e}"
 
 
 
22
 
23
- # Create and launch the Gradio interface
24
- iface = gr.Interface(fn=generate_image, inputs="text", outputs="image")
 
 
 
 
25
 
26
- # Launch the Gradio interface
27
- iface.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
+
8
+ dtype = torch.bfloat16
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+
11
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
12
+
13
+ MAX_SEED = np.iinfo(np.int32).max
14
+ MAX_IMAGE_SIZE = 2048
15
+
16
+ @spaces.GPU()
17
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
18
+ if randomize_seed:
19
+ seed = random.randint(0, MAX_SEED)
20
+ generator = torch.Generator().manual_seed(seed)
21
+ image = pipe(
22
+ prompt = prompt,
23
+ width = width,
24
+ height = height,
25
+ num_inference_steps = num_inference_steps,
26
+ generator = generator,
27
+ guidance_scale=0.0
28
+ ).images[0]
29
+ return image, seed
30
+
31
+ examples = [
32
+ "a tiny astronaut hatching from an egg on the moon",
33
+ "a cat holding a sign that says hello world",
34
+ "an anime illustration of a wiener schnitzel",
35
+ ]
36
+
37
+ css="""
38
+ #col-container {
39
+ margin: 0 auto;
40
+ max-width: 520px;
41
+ }
42
+ """
43
+
44
+ with gr.Blocks(css=css) as demo:
45
+
46
+ with gr.Column(elem_id="col-container"):
47
+ gr.Markdown(f"""# FLUX.1 [schnell]
48
+ 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
49
+ [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
50
+ """)
51
+
52
+ with gr.Row():
53
+
54
+ prompt = gr.Text(
55
+ label="Prompt",
56
+ show_label=False,
57
+ max_lines=1,
58
+ placeholder="Enter your prompt",
59
+ container=False,
60
+ )
61
+
62
+ run_button = gr.Button("Run", scale=0)
63
+
64
+ result = gr.Image(label="Result", show_label=False)
65
 
66
+ with gr.Accordion("Advanced Settings", open=False):
67
+
68
+ seed = gr.Slider(
69
+ label="Seed",
70
+ minimum=0,
71
+ maximum=MAX_SEED,
72
+ step=1,
73
+ value=0,
74
+ )
75
+
76
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
77
+
78
+ with gr.Row():
79
+
80
+ width = gr.Slider(
81
+ label="Width",
82
+ minimum=256,
83
+ maximum=MAX_IMAGE_SIZE,
84
+ step=32,
85
+ value=1024,
86
+ )
87
+
88
+ height = gr.Slider(
89
+ label="Height",
90
+ minimum=256,
91
+ maximum=MAX_IMAGE_SIZE,
92
+ step=32,
93
+ value=1024,
94
+ )
95
+
96
+ with gr.Row():
97
+
98
+
99
+ num_inference_steps = gr.Slider(
100
+ label="Number of inference steps",
101
+ minimum=1,
102
+ maximum=50,
103
+ step=1,
104
+ value=4,
105
+ )
106
 
107
+ gr.Examples(
108
+ examples = examples,
109
+ fn = infer,
110
+ inputs = [prompt],
111
+ outputs = [result, seed],
112
+ cache_examples="lazy"
113
+ )
114
 
115
+ gr.on(
116
+ triggers=[run_button.click, prompt.submit],
117
+ fn = infer,
118
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
119
+ outputs = [result, seed]
120
+ )
121
 
122
+ demo.launch()