ngoctuanai commited on
Commit
11b5377
1 Parent(s): fe42c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -110
app.py CHANGED
@@ -6,19 +6,20 @@ 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
- if current_model == "SD-1.5":
 
22
  API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
23
  elif current_model == "SDXL-1.0":
24
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
@@ -34,140 +35,159 @@ if current_model == "SD-1.5":
34
  API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
35
  elif current_model == "Midjourney-V4-XL":
36
  API_URL = "https://api-inference.huggingface.co/models/openskyml/midjourney-v4-xl"
37
- "inputs": prompt + ", 8k",
38
- "is_negative": is_negative,
39
- "steps": steps,
40
- "cfg_scale": cfg_scale,
41
- "seed": seed if seed is not None else random.randint(-1, 2147483647)
42
- }
43
- elif image_style == "Cinematic":
44
- payload = {
45
- "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
46
- "is_negative": is_negative + ", abstract, cartoon, stylized",
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 == "Digital Art":
52
- payload = {
53
- "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
54
- "is_negative": is_negative + ", sharp , modern , bright",
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 == "Portrait":
60
- payload = {
61
- "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)",
62
- "is_negative": is_negative,
63
- "steps": steps,
64
- "cfg_scale": cfg_scale,
65
- "seed": seed if seed is not None else random.randint(-1, 2147483647)
66
- }
67
-
68
- image_bytes = requests.post(API_URL, headers=headers, json=payload).content
69
- image = Image.open(io.BytesIO(image_bytes))
70
- return image
 
 
 
 
 
 
 
71
 
72
  css = """
 
73
  .gradio-container {
74
- max-width: 800px !important;
75
- margin: auto;
76
- padding-top: 1.5rem;
 
77
  }
78
 
 
79
  .gr-button {
80
- color: white;
81
- border-color: black;
82
- background: black;
83
- white-space: nowrap;
84
  }
85
 
86
  .gr-button:focus {
87
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
88
- outline: none;
89
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
90
- --tw-border-opacity: 1;
91
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
92
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
93
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
94
- --tw-ring-opacity: .5;
95
  }
96
 
 
97
  .footer, .dark .footer {
98
- margin-bottom: 45px;
99
- margin-top: 35px;
100
- text-align: center;
101
- border-bottom: 1px solid #e5e5e5;
102
  }
103
 
104
  .footer > p, .dark .footer > p {
105
- font-size: .8rem;
106
- display: inline-block;
107
- padding: 0 10px;
108
- transform: translateY(10px);
109
- background: white;
110
- }
111
-
112
- .dark .footer {
113
- border-color: #303030;
114
- }
115
-
116
- .dark .footer > p {
117
- background: #0b0f19;
118
  }
119
 
 
120
  #share-btn-container {
121
- padding: 0 0.5rem !important;
122
- background-color: #000000;
123
- justify-content: center;
124
- align-items: center;
125
- border-radius: 9999px !important;
126
- max-width: 13rem;
127
- margin-left: auto;
128
  }
129
 
130
  #share-btn-container:hover {
131
- background-color: #060606;
132
  }
133
 
134
  #share-btn {
135
- all: initial;
136
- color: #ffffff;
137
- font-weight: 600;
138
- cursor: pointer;
139
- font-family: 'IBM Plex Sans', sans-serif;
140
- margin-left: 0.5rem !important;
141
- padding: 0.5rem !important;
142
- right: 0;
143
  }
144
 
 
145
  .animate-spin {
146
- animation: spin 1s linear infinite;
147
  }
148
 
149
  @keyframes spin {
150
- from { transform: rotate(0deg); }
151
- to { transform: rotate(360deg); }
152
  }
153
 
 
154
  #gallery {
155
- min-height: 22rem;
156
- margin-bottom: 15px;
157
- margin-left: auto;
158
- margin-right: auto;
159
- border-bottom-right-radius: .5rem !important;
160
- border-bottom-left-radius: .5rem !important;
161
  }
162
  """
163
 
164
- gr.Interface(generate_txt2img,
165
- title="AI Diffusion",
166
- description="Generate images from text prompts using different AI models and styles",
167
- examples=[
168
- ["a cute dog"],
169
- ["a beautiful sunset"],
170
- ["a fantasy world"]
171
- ],
172
- layout="vertical",
173
- css=css).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
 
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
  css = """
81
+ /* General Container Styles */
82
  .gradio-container {
83
+ font-family: 'IBM Plex Sans', sans-serif;
84
+ max-width: 730px !important;
85
+ margin: auto;
86
+ padding-top: 1.5rem;
87
  }
88
 
89
+ /* Button Styles */
90
  .gr-button {
91
+ color: white;
92
+ border-color: black;
93
+ background: black;
94
+ white-space: nowrap;
95
  }
96
 
97
  .gr-button:focus {
98
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
99
+ outline: none;
100
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
101
+ --tw-border-opacity: 1;
102
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
103
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
104
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
105
+ --tw-ring-opacity: .5;
106
  }
107
 
108
+ /* Footer Styles */
109
  .footer, .dark .footer {
110
+ margin-bottom: 45px;
111
+ margin-top: 35px;
112
+ text-align: center;
 
113
  }
114
 
115
  .footer > p, .dark .footer > p {
116
+ font-size: .8rem;
117
+ display: inline-block;
118
+ padding: 0 10px;
119
+ transform: translateY(10px);
120
+ background: white;
 
 
 
 
 
 
 
 
121
  }
122
 
123
+ /* Share Button Styles */
124
  #share-btn-container {
125
+ padding: 0 0.5rem !important;
126
+ background-color: #000000;
127
+ justify-content: center;
128
+ align-items: center;
129
+ border-radius: 9999px !important;
130
+ max-width: 13rem;
131
+ margin-left: auto;
132
  }
133
 
134
  #share-btn-container:hover {
135
+ background-color: #060606;
136
  }
137
 
138
  #share-btn {
139
+ all: initial;
140
+ color: #ffffff;
141
+ font-weight: 600;
142
+ cursor: pointer;
143
+ font-family: 'IBM Plex Sans', sans-serif;
144
+ margin-left: 0.5rem !important;
145
+ padding: 0.5rem !important;
146
+ right: 0;
147
  }
148
 
149
+ /* Animation Styles */
150
  .animate-spin {
151
+ animation: spin 1s linear infinite;
152
  }
153
 
154
  @keyframes spin {
155
+ from { transform: rotate(0deg); }
156
+ to { transform: rotate(360deg); }
157
  }
158
 
159
+ /* Other Styles */
160
  #gallery {
161
+ min-height: 22rem;
162
+ margin-bottom: 15px;
163
+ margin-left: auto;
164
+ margin-right: auto;
165
+ border-bottom-right-radius: .5rem !important;
166
+ border-bottom-left-radius: .5rem !important;
167
  }
168
  """
169
 
170
+ with gr.Blocks(css=css) as demo:
171
+ favicon = '<img src="" width="48px" style="display: inline">'
172
+ gr.Markdown(
173
+ f"""<h1><center>{favicon} AI Diffusion</center></h1>
174
+ """
175
+ )
176
+
177
+ with gr.Row(elem_id="prompt-container"):
178
+ current_model = gr.Dropdown(label="Current Model", choices=list_models, value=list_models[1])
179
+
180
+ with gr.Row(elem_id="prompt-container"):
181
+ text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt", lines=1, elem_id="prompt-text-input")
182
+ text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
183
+
184
+ with gr.Row():
185
+ image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
186
+
187
+ with gr.Accordion("Advanced settings", open=False):
188
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")
189
+ image_style = gr.Dropdown(label="Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style", allow_custom_value=False)
190
+
191
+ text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
192
+
193
+ demo.launch(show_api=False)