ngoctuanai commited on
Commit
6130d44
·
1 Parent(s): 68f5e75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -63
app.py CHANGED
@@ -5,52 +5,17 @@ import random
5
  import os
6
  from PIL import Image
7
 
8
- list_models = [
9
- "SD-1.5",
10
- ]
11
 
12
- def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7,
13
- seed=None):
14
-
15
- elif current_model == "SD-1.5":
16
- API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
17
-
18
-
19
- API_TOKEN = os.environ.get("HF_READ_TOKEN")
20
- headers = {"Authorization": f"Bearer {API_TOKEN}"}
21
-
22
-
23
- if image_style == "None style":
24
- payload = {
25
- "inputs": prompt + ", 8k",
26
- "is_negative": is_negative,
27
- "steps": steps,
28
- "cfg_scale": cfg_scale,
29
- "seed": seed if seed is not None else random.randint(-1, 2147483647)
30
- }
31
- elif image_style == "Cinematic":
32
- payload = {
33
- "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
34
- "is_negative": is_negative + ", abstract, cartoon, stylized",
35
- "steps": steps,
36
- "cfg_scale": cfg_scale,
37
- "seed": seed if seed is not None else random.randint(-1, 2147483647)
38
- }
39
- elif image_style == "Digital Art":
40
- payload = {
41
- "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
42
- "is_negative": is_negative + ", sharp , modern , bright",
43
- "steps": steps,
44
- "cfg_scale": cfg_scale,
45
- "seed": seed if seed is not None else random.randint(-1, 2147483647)
46
- }
47
- elif image_style == "Portrait":
48
- payload = {
49
- "inputs": prompt + ", soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)",
50
- "is_negative": is_negative,
51
- "steps": steps,
52
- "cfg_scale": cfg_scale,
53
- "seed": seed if seed is not None else random.randint(-1, 2147483647)
54
  }
55
 
56
  image_bytes = requests.post(API_URL, headers=headers, json=payload).content
@@ -180,28 +145,37 @@ css = """
180
  .tabitem{border: 0 !important}
181
  """
182
 
183
- with gr.Blocks(css=css) as demo:
184
-
185
- favicon = '<img src="" width="48px" style="display: inline">'
186
- gr.Markdown(
187
- f"""<h1><center>{favicon} Stable Diffusion v15</center></h1>
188
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  )
190
-
191
- with gr.Row(elem_id="prompt-container"):
192
- current_model = gr.Dropdown(label="Current Model", choices=list_models, value=list_models[1])
193
-
194
- with gr.Row(elem_id="prompt-container"):
195
- text_prompt = gr.Textbox(label="Prompt", placeholder="a cute cat", lines=1, elem_id="prompt-text-input")
196
- text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
197
-
198
  with gr.Row():
199
  image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
200
-
 
 
 
201
  with gr.Accordion("Advanced settings", open=False):
202
  negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")
203
- image_style = gr.Dropdown(label="Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style", allow_custom_value=False)
204
 
205
- text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
206
 
207
- demo.launch(show_api=False)
 
5
  import os
6
  from PIL import Image
7
 
8
+ API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
9
+ API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
10
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
11
 
12
+ def query(prompt, is_negative=False, steps=1, cfg_scale=6, seed=None):
13
+ payload = {
14
+ "inputs": prompt,
15
+ "is_negative": is_negative,
16
+ "steps": steps,
17
+ "cfg_scale": cfg_scale,
18
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  }
20
 
21
  image_bytes = requests.post(API_URL, headers=headers, json=payload).content
 
145
  .tabitem{border: 0 !important}
146
  """
147
 
148
+ with gr.Blocks(css=css, theme="pseudolab/huggingface-korea-theme") as dalle:
149
+ gr.HTML(
150
+ """
151
+ <div style="text-align: center; margin: 0 auto;">
152
+ <div
153
+ style="
154
+ display: inline-flex;
155
+ align-items: center;
156
+ gap: 0.8rem;
157
+ font-size: 1.75rem;
158
+ "
159
+ >
160
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
161
+ Stable Diffusion v1.5 XL
162
+ </h1>
163
+ </div>
164
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
165
+ </p>
166
+ </div>
167
+ """
168
  )
169
+
 
 
 
 
 
 
 
170
  with gr.Row():
171
  image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
172
+ with gr.Column(elem_id="prompt-container"):
173
+ text_prompt = gr.Textbox(label="Prompt", placeholder="a cute cat", lines=1, elem_id="prompt-text-input")
174
+ text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
175
+
176
  with gr.Accordion("Advanced settings", open=False):
177
  negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")
 
178
 
179
+ text_button.click(query, inputs=[text_prompt, negative_prompt], outputs=image_output)
180
 
181
+ dalle.launch(show_api=False)