hotbiz commited on
Commit
7fdc288
1 Parent(s): aa81fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py CHANGED
@@ -2,3 +2,127 @@ import gradio as gr
2
 
3
  gr.load("models/black-forest-labs/FLUX.1-schnell").launch()
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  gr.load("models/black-forest-labs/FLUX.1-schnell").launch()
4
 
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"
14
+
15
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
16
+
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
+ "Gambarkan suasana perayaan Hari Ulang Tahun Republik Indonesia yang ke-79 di sebuah alun-alun kota. Tampak bendera merah putih berkibar di tengah lapangan, di sekelilingnya ribuan orang mengenakan pakaian tradisional dari berbagai daerah Indonesia, semua bersatu dalam semangat kebangsaan. Di latar belakang, terdapat panggung besar yang dihiasi dengan dekorasi merah dan putih, di mana seorang pemimpin upacara sedang memberikan pidato. Langit cerah biru dengan beberapa awan putih lembut, dan di udara, terlihat pesawat tempur TNI AU yang terbang membentuk formasi melintas di atas area perayaan. Di sekeliling lapangan, terlihat stand-stand kuliner khas Indonesia yang dipenuhi oleh warga yang sedang menikmati makanan bersama."
37
+ "Depict a scene of a beautiful 20-year-old girl named Dewi, with a sweet and gentle face, shopping at the bustling Gombong morning market. She is dressed in stylish, revealing attire that accentuates her allure. Dewi moves gracefully among the stalls, attracting the admiring gazes of all the men around her. The market is filled with vibrant activity, with vendors selling fresh produce, local delicacies, and various goods. The atmosphere is lively, with bright sunlight casting soft shadows, and Dewi stands out as the captivating focal point of the scene."
38
+ "a tiny astronaut hatching from an egg on the moon",
39
+ "a cat holding a sign that says hello world",
40
+ "an anime illustration of a wiener schnitzel",
41
+ ]
42
+
43
+ css="""
44
+ #col-container {
45
+ margin: 0 auto;
46
+ max-width: 520px;
47
+ }
48
+ """
49
+
50
+ with gr.Blocks(css=css) as demo:
51
+
52
+ with gr.Column(elem_id="col-container"):
53
+ gr.Markdown(f"""# FLUX.1 [schnell]
54
+ 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
55
+ [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
56
+ """)
57
+
58
+ with gr.Row():
59
+
60
+ prompt = gr.Text(
61
+ label="Prompt",
62
+ show_label=False,
63
+ max_lines=1,
64
+ placeholder="Enter your prompt",
65
+ container=False,
66
+ )
67
+
68
+ run_button = gr.Button("Run", scale=0)
69
+
70
+ result = gr.Image(label="Result", show_label=False)
71
+
72
+ with gr.Accordion("Advanced Settings", open=False):
73
+
74
+ seed = gr.Slider(
75
+ label="Seed",
76
+ minimum=0,
77
+ maximum=MAX_SEED,
78
+ step=1,
79
+ value=0,
80
+ )
81
+
82
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
83
+
84
+ with gr.Row():
85
+
86
+ width = gr.Slider(
87
+ label="Width",
88
+ minimum=256,
89
+ maximum=MAX_IMAGE_SIZE,
90
+ step=32,
91
+ value=1024,
92
+ )
93
+
94
+ height = gr.Slider(
95
+ label="Height",
96
+ minimum=256,
97
+ maximum=MAX_IMAGE_SIZE,
98
+ step=32,
99
+ value=1024,
100
+ )
101
+
102
+ with gr.Row():
103
+
104
+
105
+ num_inference_steps = gr.Slider(
106
+ label="Number of inference steps",
107
+ minimum=1,
108
+ maximum=50,
109
+ step=1,
110
+ value=4,
111
+ )
112
+
113
+ gr.Examples(
114
+ examples = examples,
115
+ fn = infer,
116
+ inputs = [prompt],
117
+ outputs = [result, seed],
118
+ cache_examples="lazy"
119
+ )
120
+
121
+ gr.on(
122
+ triggers=[run_button.click, prompt.submit],
123
+ fn = infer,
124
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
125
+ outputs = [result, seed]
126
+ )
127
+
128
+ demo.launch()