TheMaisk commited on
Commit
bbe0796
·
1 Parent(s): ec8a27c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "SDXL-1.0",
10
+ "SD-1.5",
11
+ "OpenJourney-V4",
12
+ "Anything-V4",
13
+ "Disney-Pixar-Cartoon",
14
+ "Pixel-Art-XL",
15
+ "Dalle-3-XL",
16
+ "Midjourney-V4-XL",
17
+ ]
18
+
19
+ def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7,
20
+ seed=None):
21
+
22
+ if current_model == "SD-1.5":
23
+ API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
24
+ elif current_model == "SDXL-1.0":
25
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
26
+ elif current_model == "OpenJourney-V4":
27
+ API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
28
+ elif current_model == "Anything-V4":
29
+ API_URL = "https://api-inference.huggingface.co/models/xyn-ai/anything-v4.0"
30
+ elif current_model == "Disney-Pixar-Cartoon":
31
+ API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/disney-pixar-cartoon"
32
+ elif current_model == "Pixel-Art-XL":
33
+ API_URL = "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl"
34
+ elif current_model == "Dalle-3-XL":
35
+ API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
36
+ elif current_model == "Midjourney-V4-XL":
37
+ API_URL = "https://api-inference.huggingface.co/models/openskyml/midjourney-v4-xl"
38
+
39
+ API_TOKEN = os.environ.get("HF_READ_TOKEN")
40
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
41
+
42
+
43
+ if image_style == "None style":
44
+ payload = {
45
+ "inputs": prompt + ", 8k",
46
+ "is_negative": is_negative,
47
+ "steps": steps,
48
+ "cfg_scale": cfg_scale,
49
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
50
+ }
51
+ elif image_style == "Cinematic":
52
+ payload = {
53
+ "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
54
+ "is_negative": is_negative + ", abstract, cartoon, stylized",
55
+ "steps": steps,
56
+ "cfg_scale": cfg_scale,
57
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
58
+ }
59
+ elif image_style == "Digital Art":
60
+ payload = {
61
+ "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
62
+ "is_negative": is_negative + ", sharp , modern , bright",
63
+ "steps": steps,
64
+ "cfg_scale": cfg_scale,
65
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
66
+ }
67
+ elif image_style == "Portrait":
68
+ payload = {
69
+ "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)",
70
+ "is_negative": is_negative,
71
+ "steps": steps,
72
+ "cfg_scale": cfg_scale,
73
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
74
+ }
75
+
76
+ image_bytes = requests.post(API_URL, headers=headers, json=payload).content
77
+ image = Image.open(io.BytesIO(image_bytes))
78
+ return image
79
+
80
+
81
+ css = """
82
+ /* General Container Styles */
83
+ .gradio-container {
84
+ font-family: 'IBM Plex Sans', sans-serif;
85
+ max-width: 730px !important;
86
+ margin: auto;
87
+ padding-top: 1.5rem;
88
+ }
89
+ /* Button Styles */
90
+ .gr-button {
91
+ color: white;
92
+ border-color: black;
93
+ background: black;
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
+ /* Footer Styles */
107
+ .footer, .dark .footer {
108
+ margin-bottom: 45px;
109
+ margin-top: 35px;
110
+ text-align: center;
111
+ border-bottom: 1px solid #e5e5e5;
112
+ }
113
+ .footer > p, .dark .footer > p {
114
+ font-size: .8rem;
115
+ display: inline-block;
116
+ padding: 0 10px;
117
+ transform: translateY(10px);
118
+ background: white;
119
+ }
120
+ .dark .footer {
121
+ border-color: #303030;
122
+ }
123
+ .dark .footer > p {
124
+ background: #0b0f19;
125
+ }
126
+ /* Share Button Styles */
127
+ #share-btn-container {
128
+ padding: 0 0.5rem !important;
129
+ background-color: #000000;
130
+ justify-content: center;
131
+ align-items: center;
132
+ border-radius: 9999px !important;
133
+ max-width: 13rem;
134
+ margin-left: auto;
135
+ }
136
+ #share-btn-container:hover {
137
+ background-color: #060606;
138
+ }
139
+ #share-btn {
140
+ all: initial;
141
+ color: #ffffff;
142
+ font-weight: 600;
143
+ cursor: pointer;
144
+ font-family: 'IBM Plex Sans', sans-serif;
145
+ margin-left: 0.5rem !important;
146
+ padding: 0.5rem !important;
147
+ right: 0;
148
+ }
149
+ /* Animation Styles */
150
+ .animate-spin {
151
+ animation: spin 1s linear infinite;
152
+ }
153
+ @keyframes spin {
154
+ from { transform: rotate(0deg); }
155
+ to { transform: rotate(360deg); }
156
+ }
157
+ /* Other Styles */
158
+ #gallery {
159
+ min-height: 22rem;
160
+ margin-bottom: 15px;
161
+ margin-left: auto;
162
+ margin-right: auto;
163
+ border-bottom-right-radius: .5rem !important;
164
+ border-bottom-left-radius: .5rem !important;
165
+ }
166
+ """
167
+
168
+ with gr.Blocks(css=css) as demo:
169
+
170
+ favicon = '<img src="" width="48px" style="display: inline">'
171
+ gr.Markdown(
172
+ f"""<h1><center>{favicon} AI Diffusion</center></h1>
173
+ """
174
+ )
175
+
176
+ with gr.Row(elem_id="prompt-container"):
177
+ current_model = gr.Dropdown(label="Current Model", choices=list_models, value=list_models[1])
178
+
179
+ with gr.Row(elem_id="prompt-container"):
180
+ text_prompt = gr.Textbox(label="Prompt", placeholder="a cute dog", lines=1, elem_id="prompt-text-input")
181
+ text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
182
+
183
+ with gr.Row():
184
+ image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
185
+
186
+ with gr.Accordion("Advanced settings", open=False):
187
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")
188
+ image_style = gr.Dropdown(label="Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style", allow_custom_value=False)
189
+
190
+ text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
191
+
192
+ demo.launch(show_api=False)