Whoruw commited on
Commit
ce759a0
·
verified ·
1 Parent(s): 438bbb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -205
app.py CHANGED
@@ -1,230 +1,151 @@
1
- impor gradio sebagai gr
2
- impor numpy sebagai np
3
- impor acak
4
- impor obor
5
- dari diffuser impor StableDiffusion3Pipeline
6
-
7
- # Inisialisasi model Gradio
8
- perangkat = "cuda" jika torch.cuda.is_available() jika tidak "cpu"
9
- tipe data = obor.float16
10
-
11
- repo = "stabilitasai/difusi-stabil-3-medium"
12
- jalur pipa = StableDiffusionPipeline.dari_pra-latihan(repo, tipe_obor=torch.float16).ke(perangkat)
13
-
14
- MAX_SEED = np.iinfo(np.int32).maks
15
- UKURAN_GAMBAR_MAKS = 1344
16
-
17
- @gr.GPU
18
- def infer(prompt, negative_prompt, seed, randomize_seed, lebar, tinggi, skala_panduan, jumlah_langkah_inferensi, kemajuan=gr.Kemajuan(track_tqdm=Benar)):
19
-
20
- jika acak_seed:
21
- benih = acak.randint(0, MAX_SEED)
22
-
23
- generator = obor.Generator(perangkat=perangkat).manual_seed(benih)
24
-
25
- gambar = pipa(
26
- prompt=perintah,
27
- prompt_negatif=prompt_negatif,
28
- skala_panduan=skala_panduan,
29
- num_inference_steps=jumlah_langkah_inferensi,
30
- lebar=lebar,
31
- tinggi=tinggi,
32
- pembangkit=pembangkit
33
- ).gambar[0]
34
-
35
- kembalikan gambar, benih
36
-
37
- contoh = [
38
- ["Astronot di hutan, palet warna keren, warna tenang, detail, 8k"],
39
- ["Seorang astronot menunggangi kuda hijau"],
40
- ["Sepotong kue keju ceviche yang lezat"],
41
- [Bahasa Indonesia]
42
-
43
  css = """
44
- tubuh {
45
  font-family: 'Arial', sans-serif;
46
- warna latar belakang: #f2f2f2;
47
- warna: #333;
48
- }
49
-
50
- #col-wadah {
51
- lebar maks: 800 piksel;
52
- margin: 20px otomatis;
53
- bantalan: 20px;
54
- warna latar belakang: #ffffff;
55
- batas: 1px solid #ccc;
56
- radius batas: 8px;
57
- bayangan kotak: 0 4px 8px rgba(0, 0, 0, 0.1);
58
  }
59
 
60
- #judul {
61
- ukuran font: 24px;
62
- bobot font: tebal;
63
- margin-bawah: 20px;
64
- teks-ratakan: tengah;
 
 
 
65
  }
66
 
67
- .kotak masukan {
68
- margin-bawah: 20px;
 
 
 
69
  }
70
 
71
- label kotak masukan {
72
- ukuran font: 18px;
73
- bobot font: tebal;
74
- tampilan: blok;
75
- margin-bawah: 10px;
76
  }
77
 
78
- .kotak masukan input[jenis="teks"],
79
- .kotak masukan input[jenis="angka"],
80
- .kotak masukan pilih {
81
- lebar: kalk(100% - 20px);
82
- bantalan: 10px;
83
- ukuran font: 16px;
84
- batas: 1px solid #ccc;
85
- radius batas: 4px;
86
  }
87
 
88
- .kotak masukan input[jenis="teks"]:fokus,
89
- .input-box input[jenis="angka"]:fokus,
90
- .kotak input pilih:fokus {
91
- garis besar: tidak ada;
92
- warna batas: #4CAF50;
 
 
 
 
93
  }
94
 
95
- .kotak masukan .label penggeser {
96
- ukuran font: 16px;
97
- margin-atas: 10px;
98
- margin-bawah: 5px;
 
99
  }
100
 
101
- .tombol {
102
- warna latar belakang: #4CAF50;
103
- warna putih;
104
- bantalan: 10px 20px;
105
- perbatasan: tidak ada;
106
- radius batas: 4px;
107
- kursor: penunjuk;
108
- ukuran font: 16px;
109
- tampilan: blok;
110
- lebar: 100%;
111
- teks-ratakan: tengah;
112
  }
113
 
114
- .tombol:arahkan kursor {
115
- warna latar belakang: #45a049;
116
  }
117
 
118
- .hasil {
119
- margin-atas: 20px;
120
- teks-ratakan: tengah;
121
  }
122
 
123
- .hasil img {
124
- lebar maks: 100%;
125
- radius batas: 8px;
126
- bayangan kotak: 0 4px 8px rgba(0, 0, 0, 0.1);
127
  }
128
  """
129
 
130
- dengan gr.Blocks(css=css) sebagai demo:
131
-
132
- gr.Penurunan harga("""
133
- # Difusi Stabil 3 Demo Sedang
134
- Pelajari lebih lanjut tentang [seri Stable Diffusion 3](https://stability.ai/news/stable-diffusion-3). Cobalah di [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), [Stable Assistant](https://stability.ai/stable-assistant), atau di Discord melalui [Stable Artisan](https://stability.ai/stable-artisan). Jalankan secara lokal dengan [ComfyUI](https://github.com/comfyanonymous/ComfyUI) atau [diffusers](https://github.com/huggingface/diffusers)
135
- """)
136
-
137
- dengan gr.Column(elem_id="col-container"):
138
- gr.Div(elem_id="judul", text="Buat Gambar")
139
-
140
- dengan gr.Row():
141
- prompt = gr.Kotakteks(
142
- label="Permintaan",
143
- garis=3,
144
- placeholder="Masukkan permintaan Anda di sini...",
145
- elem_classes=["kotak-input"],
146
- )
147
-
148
- tombol_jalankan = gr.Tombol(
149
- "Hasilkan Gambar",
150
- kelas_elem=["tombol"]
151
- )
152
-
153
- hasil_gambar = gr.Gambar(
154
- label="Hasil",
155
- elem_classes=["hasil"],
156
- )
157
-
158
- dengan gr.Accordion("Pengaturan Lanjutan", open=False):
159
- prompt_negatif = gr.Kotakteks(
160
- label="Perintah Negatif",
161
- placeholder="Masukkan prompt negatif (opsional)",
162
- elem_classes=["kotak-input"],
163
- )
164
-
165
- benih = gr.Nomor(
166
- label="Benih",
167
- minimum=0,
168
- maks=MAX_SEED,
169
- langkah=1,
170
- nilai=0,
171
- elem_classes=["kotak-input"],
172
- )
173
-
174
- acak_benih = gr.Kotak centang(
175
- label="Acak Benih",
176
- nilai=Benar,
177
- elem_classes=["kotak-input"],
178
- )
179
-
180
- dengan gr.Row():
181
- lebar = gr.Slider(
182
- label="Lebar",
183
- menit=256,
184
- maks=UKURAN_GAMBAR_MAKS,
185
- langkah=64,
186
- nilai=1024,
187
- elem_classes=["kotak-input"],
188
- )
189
-
190
- tinggi = gr.Slider(
191
- label="Tinggi",
192
- menit=256,
193
- maks=UKURAN_GAMBAR_MAKS,
194
- langkah=64,
195
- nilai=1024,
196
- elem_classes=["kotak-input"],
197
- )
198
-
199
- dengan gr.Row():
200
- skala_panduan = gr.Slider(
201
- label="Skala Panduan",
202
- minimum=0,0,
203
- maks=10.0,
204
- langkah=0,1,
205
- nilai=5.0,
206
- elem_classes=["kotak-input"],
207
- )
208
-
209
- num_inference_steps = gr.Nomor(
210
- label="Jumlah Langkah Inferensi",
211
- menit=1,
212
- maks=50,
213
- langkah=1,
214
- nilai=28,
215
- elem_classes=["kotak-input"],
216
- )
217
-
218
- gr.Contoh(
219
- contoh=contoh,
220
- masukan=[perintah],
221
- elem_classes=["kotak masukan"]
222
- )
223
-
224
- jalankan_tombol.klik(
225
- fn=menyimpulkan,
226
- input=[prompt, prompt_negatif, benih, acak_benih, lebar, tinggi, skala_panduan, jumlah_langkah_inferensi],
227
- keluaran=[gambar_hasil],
228
- )
229
-
230
- demo.peluncuran()
 
1
+ import torch
2
+ from diffusers import StableDiffusionPipeline
3
+ import gradio as gr
4
+ import numpy as np
5
+ import random
6
+
7
+ # Inisialisasi model
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ dtype = torch.float16
10
+
11
+ repo = "stabilityai/stable-diffusion-3-medium-diffusers"
12
+ pipeline = StableDiffusionPipeline.from_pretrained(repo, torch_dtype=dtype).to(device)
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 1344
16
+
17
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
18
+ if randomize_seed:
19
+ seed = random.randint(0, MAX_SEED)
20
+
21
+ generator = torch.Generator(device=device).manual_seed(seed)
22
+ image = pipeline(
23
+ prompt=prompt,
24
+ negative_prompt=negative_prompt,
25
+ width=width,
26
+ height=height,
27
+ guidance_scale=guidance_scale,
28
+ num_inference_steps=num_inference_steps,
29
+ generator=generator
30
+ ).images[0]
31
+
32
+ return image, seed
33
+
34
+ examples = [
35
+ ["Astronaut in the forest, cool color palette, calm colors, detailed, 8k"],
36
+ ["An astronaut riding a green horse"],
37
+ ["A delicious piece of ceviche cheesecake"]
38
+ ]
39
+
40
+ # CSS untuk antarmuka yang lebih menarik
 
 
41
  css = """
42
+ body {
43
  font-family: 'Arial', sans-serif;
44
+ background-color: #f0f0f0;
45
+ color: #333;
46
+ margin: 0;
47
+ padding: 0;
48
+ display: flex;
49
+ flex-direction: column;
50
+ align-items: center;
51
+ justify-content: center;
52
+ height: 100vh;
 
 
 
53
  }
54
 
55
+ #col-container {
56
+ max-width: 800px;
57
+ width: 100%;
58
+ margin: 20px;
59
+ padding: 20px;
60
+ background-color: #fff;
61
+ border-radius: 10px;
62
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
63
  }
64
 
65
+ #title {
66
+ font-size: 28px;
67
+ font-weight: bold;
68
+ text-align: center;
69
+ margin-bottom: 20px;
70
  }
71
 
72
+ .input-box {
73
+ margin-bottom: 20px;
 
 
 
74
  }
75
 
76
+ .input-box label {
77
+ font-size: 18px;
78
+ font-weight: bold;
79
+ display: block;
80
+ margin-bottom: 5px;
 
 
 
81
  }
82
 
83
+ .input-box input,
84
+ .input-box select,
85
+ .input-box textarea {
86
+ width: calc(100% - 20px);
87
+ padding: 10px;
88
+ font-size: 16px;
89
+ border: 1px solid #ccc;
90
+ border-radius: 4px;
91
+ margin-bottom: 10px;
92
  }
93
 
94
+ .input-box input:focus,
95
+ .input-box select:focus,
96
+ .input-box textarea:focus {
97
+ outline: none;
98
+ border-color: #007BFF;
99
  }
100
 
101
+ .button {
102
+ background-color: #007BFF;
103
+ color: white;
104
+ padding: 10px 20px;
105
+ border: none;
106
+ border-radius: 4px;
107
+ cursor: pointer;
108
+ font-size: 16px;
109
+ width: 100%;
 
 
110
  }
111
 
112
+ .button:hover {
113
+ background-color: #0056b3;
114
  }
115
 
116
+ .result {
117
+ margin-top: 20px;
118
+ text-align: center;
119
  }
120
 
121
+ .result img {
122
+ max-width: 100%;
123
+ border-radius: 8px;
124
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
125
  }
126
  """
127
 
128
+ with gr.Blocks(css=css) as demo:
129
+ gr.Markdown("""
130
+ # Stable Diffusion 3 Medium Demo
131
+ Generate images using Stable Diffusion 3. Try different prompts and settings to see the results.
132
+ """, elem_id="title")
133
+
134
+ with gr.Column(elem_id="col-container"):
135
+ prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here...", elem_classes="input-box")
136
+ negative_prompt = gr.Textbox(label="Negative Prompt", lines=1, placeholder="Enter negative prompt (optional)", elem_classes="input-box")
137
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, elem_classes="input-box")
138
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True, elem_classes="input-box")
139
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=64, value=512, elem_classes="input-box")
140
+ height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=64, value=512, elem_classes="input-box")
141
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=20.0, step=0.5, value=7.5, elem_classes="input-box")
142
+ num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=1, maximum=100, step=1, value=28, elem_classes="input-box")
143
+
144
+ generate_button = gr.Button("Generate Image", elem_classes="button")
145
+ result_image = gr.Image(elem_classes="result")
146
+
147
+ generate_button.click(infer, inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], outputs=[result_image, seed])
148
+
149
+ gr.Examples(examples=examples, inputs=[prompt])
150
+
151
+ demo.launch()