chokiproai commited on
Commit
b9e7f35
1 Parent(s): 9408072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +199 -39
app.py CHANGED
@@ -1,45 +1,205 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- # Load the RunwayML model using Gradio
4
- model = gr.load("models/runwayml/stable-diffusion-v1-5")
5
-
6
- # Custom HTML and CSS for styling
7
- custom_html = """
8
- <!DOCTYPE html>
9
- <html>
10
- <head>
11
- <style>
12
- body {
13
- font-family: Arial, sans-serif;
14
- background-color: #f4f4f4;
15
- text-align: center;
16
- margin: 0;
17
- padding: 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
- .container {
20
- max-width: 600px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  margin: auto;
22
- padding: 20px;
23
- background-color: #fff;
24
- border-radius: 10px;
25
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
26
- margin-top: 50px;
27
- }
28
- h1 {
29
- color: #333;
30
- }
31
- </style>
32
- </head>
33
- <body>
34
- <div class="container">
35
- <h1>Stable Diffusion Model</h1>
36
- {input}
37
- {output}
38
- </div>
39
- </body>
40
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  """
42
 
43
- # Launch the interface with the loaded model and custom styling
44
- iface = gr.Interface(fn=model, inputs=model.interface.inputs, outputs=model.interface.outputs)
45
- iface.share(html=custom_html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import io
4
+ 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
+ if current_model == "SD-1.5":
16
+ API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
17
+
18
+
19
+
20
+
21
+ if image_style == "None style":
22
+ payload = {
23
+ "inputs": prompt + ", 8k",
24
+ "is_negative": is_negative,
25
+ "steps": steps,
26
+ "cfg_scale": cfg_scale,
27
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
28
+ }
29
+ elif image_style == "Cinematic":
30
+ payload = {
31
+ "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
32
+ "is_negative": is_negative + ", abstract, cartoon, stylized",
33
+ "steps": steps,
34
+ "cfg_scale": cfg_scale,
35
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
36
+ }
37
+ elif image_style == "Digital Art":
38
+ payload = {
39
+ "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
40
+ "is_negative": is_negative + ", sharp , modern , bright",
41
+ "steps": steps,
42
+ "cfg_scale": cfg_scale,
43
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
44
  }
45
+ elif image_style == "Portrait":
46
+ payload = {
47
+ "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)",
48
+ "is_negative": is_negative,
49
+ "steps": steps,
50
+ "cfg_scale": cfg_scale,
51
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
52
+ }
53
+
54
+ image_bytes = requests.post(API_URL, headers=headers, json=payload).content
55
+ image = Image.open(io.BytesIO(image_bytes))
56
+ return image
57
+
58
+
59
+ css = """
60
+ .gradio-container {
61
+ font-family: 'IBM Plex Sans', sans-serif;
62
+ }
63
+ .gr-button {
64
+ color: white;
65
+ border-color: black;
66
+ background: black;
67
+ }
68
+ input[type='range'] {
69
+ accent-color: black;
70
+ }
71
+ .dark input[type='range'] {
72
+ accent-color: #dfdfdf;
73
+ }
74
+ .gradio-container {
75
+ max-width: 730px !important;
76
  margin: auto;
77
+ padding-top: 1.5rem;
78
+ }
79
+ #gallery {
80
+ min-height: 22rem;
81
+ margin-bottom: 15px;
82
+ margin-left: auto;
83
+ margin-right: auto;
84
+ border-bottom-right-radius: .5rem !important;
85
+ border-bottom-left-radius: .5rem !important;
86
+ }
87
+ #gallery>div>.h-full {
88
+ min-height: 20rem;
89
+ }
90
+ .details:hover {
91
+ text-decoration: underline;
92
+ }
93
+ .gr-button {
94
+ white-space: nowrap;
95
+ }
96
+ .gr-button:focus {
97
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
98
+ outline: none;
99
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
100
+ --tw-border-opacity: 1;
101
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
102
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
103
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
104
+ --tw-ring-opacity: .5;
105
+ }
106
+ #advanced-btn {
107
+ font-size: .7rem !important;
108
+ line-height: 19px;
109
+ margin-top: 12px;
110
+ margin-bottom: 12px;
111
+ padding: 2px 8px;
112
+ border-radius: 14px !important;
113
+ }
114
+ #advanced-options {
115
+ display: none;
116
+ margin-bottom: 20px;
117
+ }
118
+ .footer {
119
+ margin-bottom: 45px;
120
+ margin-top: 35px;
121
+ text-align: center;
122
+ border-bottom: 1px solid #e5e5e5;
123
+ }
124
+ .footer>p {
125
+ font-size: .8rem;
126
+ display: inline-block;
127
+ padding: 0 10px;
128
+ transform: translateY(10px);
129
+ background: white;
130
+ }
131
+ .dark .footer {
132
+ border-color: #303030;
133
+ }
134
+ .dark .footer>p {
135
+ background: #0b0f19;
136
+ }
137
+ .acknowledgments h4{
138
+ margin: 1.25em 0 .25em 0;
139
+ font-weight: bold;
140
+ font-size: 115%;
141
+ }
142
+ .animate-spin {
143
+ animation: spin 1s linear infinite;
144
+ }
145
+ @keyframes spin {
146
+ from {
147
+ transform: rotate(0deg);
148
+ }
149
+ to {
150
+ transform: rotate(360deg);
151
+ }
152
+ }
153
+ #share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
154
+ div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
155
+ #share-btn-container:hover {background-color: #060606}
156
+ #share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
157
+ #share-btn * {all: unset}
158
+ #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
159
+ #share-btn-container .wrap {display: none !important}
160
+ #share-btn-container.hidden {display: none!important}
161
+ .gr-form{
162
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
163
+ }
164
+ #prompt-container{
165
+ gap: 0;
166
+ }
167
+ #prompt-container .form{
168
+ border-top-right-radius: 0;
169
+ border-bottom-right-radius: 0;
170
+ }
171
+ #gen-button{
172
+ border-top-left-radius:0;
173
+ border-bottom-left-radius:0;
174
+ }
175
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
176
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
177
+ .image_duplication{position: absolute; width: 100px; left: 50px}
178
+ .tabitem{border: 0 !important}
179
  """
180
 
181
+ with gr.Blocks(css=css) as demo:
182
+
183
+ favicon = '<img src="" width="48px" style="display: inline">'
184
+ gr.Markdown(
185
+ f"""<h1><center>{favicon} StableDiffusionv1.5</center></h1>
186
+ """
187
+ )
188
+
189
+ with gr.Row(elem_id="prompt-container"):
190
+ current_model = gr.Dropdown(label="Current Model", choices=list_models, value=list_models[1])
191
+
192
+ with gr.Row(elem_id="prompt-container"):
193
+ text_prompt = gr.Textbox(label="Prompt", placeholder="a cute cat", lines=1, elem_id="prompt-text-input")
194
+ text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
195
+
196
+ with gr.Row():
197
+ image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
198
+
199
+ with gr.Accordion("Advanced settings", open=False):
200
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")
201
+ image_style = gr.Dropdown(label="Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style", allow_custom_value=False)
202
+
203
+ text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
204
+
205
+ demo.launch(show_api=False)