ngoctuanai commited on
Commit
1f62c1a
1 Parent(s): 44aa685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -57
app.py CHANGED
@@ -5,79 +5,209 @@ 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
- # Your generate_txt2img function
20
- def generate_txt2img(current_model, prompt, is_negative, image_style, steps, cfg_scale, seed):
21
- # ... (Your function implementation)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Enhanced CSS for better styling
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  css = """
25
- body {
26
- font-family: 'Helvetica Neue', sans-serif;
27
- background-color: #f7f8fa;
28
- }
29
  .gradio-container {
30
- max-width: 800px;
31
- margin: auto;
32
- padding-top: 1rem;
33
- padding-bottom: 1rem;
34
- border-radius: 10px;
35
- background-color: white;
36
- box-shadow: 0 0 20px rgba(0,0,0,0.1);
37
  }
38
- .gradio-row {
39
- margin-bottom: 1rem;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  }
41
- .gradio-button, .gradio-dropdown, .gradio-textbox {
42
- border-radius: 5px;
 
 
 
 
 
43
  }
44
- .gradio-button {
45
- background-color: #4CAF50; /* Green */
46
- color: white;
 
 
 
 
47
  }
48
- .gradio-label {
49
- display: block;
50
- margin-bottom: 5px;
51
- font-weight: bold;
52
  }
53
- """
54
 
55
- # Gradio interface setup
56
- with gr.Blocks(css=css) as demo:
57
- gr.Markdown("# AI Image Generation")
58
 
59
- with gr.Row():
60
- current_model = gr.Dropdown(label="Select Model", choices=list_models, value=list_models[0])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- with gr.Row():
63
- text_prompt = gr.Textbox(label="Enter Prompt", placeholder="Describe the image you want to generate", lines=2)
64
- text_button = gr.Button("Generate Image")
 
 
 
 
 
 
 
 
 
 
65
 
66
- with gr.Row():
67
- image_output = gr.Image(type="pil", label="Generated Image")
 
 
 
68
 
69
- with gr.Accordion("Advanced settings"):
70
- with gr.Column():
71
- negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter elements to avoid in the image", lines=2)
72
- image_style = gr.Dropdown(label="Select Style", choices=["None", "Cinematic", "Digital Art", "Portrait"], value="None")
73
- steps = gr.Slider(minimum=1, maximum=100, value=50, label="Steps")
74
- cfg_scale = gr.Slider(minimum=1, maximum=20, value=7, label="CFG Scale")
75
- seed = gr.Number(label="Seed", value=random.randint(0, 2**32 - 1))
76
 
77
- text_button.click(
78
- func=generate_txt2img,
79
- inputs=[current_model, text_prompt, negative_prompt, image_style, steps, cfg_scale, seed],
80
- outputs=image_output
81
- )
82
 
 
83
  demo.launch()
 
5
  import os
6
  from PIL import Image
7
 
8
+ List of available models
9
  list_models = [
10
+ "SDXL-1.0",
11
+ "SD-1.5",
12
+ "OpenJourney-V4",
13
+ "Anything-V4",
14
+ "Disney-Pixar-Cartoon",
15
+ "Pixel-Art-XL",
16
+ "Dalle-3-XL",
17
+ "Midjourney-V4-XL",
18
  ]
19
 
20
+ Function to generate image from text prompt using selected model and style
21
+ def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7,
22
+ seed=None):
23
+ # API URLs for different models
24
+ if current_model == "SD-1.5":
25
+ API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
26
+ elif current_model == "SDXL-1.0":
27
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
28
+ elif current_model == "OpenJourney-V4":
29
+ API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
30
+ elif current_model == "Anything-V4":
31
+ API_URL = "https://api-inference.huggingface.co/models/xyn-ai/anything-v4.0"
32
+ elif current_model == "Disney-Pixar-Cartoon":
33
+ API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/disney-pixar-cartoon"
34
+ elif current_model == "Pixel-Art-XL":
35
+ API_URL = "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl"
36
+ elif current_model == "Dalle-3-XL":
37
+ API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
38
+ elif current_model == "Midjourney-V4-XL":
39
+ API_URL = "https://api-inference.huggingface.co/models/openskyml/midjourney-v4-xl"
40
 
41
+ API_TOKEN = os.environ.get("HF_READ_TOKEN")
42
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
43
+
44
+ # Construct payload based on selected style and options
45
+ if image_style == "None style":
46
+ payload = {
47
+ "inputs": prompt + ", 8k",
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
+ elif image_style == "Cinematic":
54
+ payload = {
55
+ "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
56
+ "is_negative": is_negative + ", abstract, cartoon, stylized",
57
+ "steps": steps,
58
+ "cfg_scale": cfg_scale,
59
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
60
+ }
61
+ elif image_style == "Digital Art":
62
+ payload = {
63
+ "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
64
+ "is_negative": is_negative + ", sharp , modern , bright",
65
+ "steps": steps,
66
+ "cfg_scale": cfg_scale,
67
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
68
+ }
69
+ elif image_style == "Portrait":
70
+ payload = {
71
+ "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)",
72
+ "is_negative": is_negative,
73
+ "steps": steps,
74
+ "cfg_scale": cfg_scale,
75
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
76
+ }
77
+
78
+ # Send request to API and retrieve image
79
+ image_bytes = requests.post(API_URL, headers=headers, json=payload).content
80
+ image = Image.open(io.BytesIO(image_bytes))
81
+ return image
82
+ CSS styles for the app
83
  css = """
84
+ /* General Container Styles */
 
 
 
85
  .gradio-container {
86
+ max-width: 800px !important;
87
+ margin: auto;
88
+ padding-top: 1.5rem;
 
 
 
 
89
  }
90
+
91
+ /* Button Styles */
92
+ .gr-button {
93
+ color: white;
94
+ border-color: black;
95
+ background: black;
96
+ white-space: nowrap;
97
+ }
98
+
99
+ .gr-button:focus {
100
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
101
+ outline: none;
102
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
103
+ --tw-border-opacity: 1;
104
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
105
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
106
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
107
+ --tw-ring-opacity: .5;
108
  }
109
+
110
+ /* Footer Styles */
111
+ .footer, .dark .footer {
112
+ margin-bottom: 45px;
113
+ margin-top: 35px;
114
+ text-align: center;
115
+ border-bottom: 1px solid #e5e5e5;
116
  }
117
+
118
+ .footer > p, .dark .footer > p {
119
+ font-size: .8rem;
120
+ display: inline-block;
121
+ padding: 0 10px;
122
+ transform: translateY(10px);
123
+ background: white;
124
  }
125
+
126
+ .dark .footer {
127
+ border-color: #303030;
 
128
  }
 
129
 
130
+ .dark .footer > p {
131
+ background: #0b0f19;
132
+ }
133
 
134
+ /* Share Button Styles */
135
+ #share-btn-container {
136
+ padding: 0 0.5rem !important;
137
+ background-color: #000000;
138
+ justify-content: center;
139
+ align-items: center;
140
+ border-radius: 9999px !important;
141
+ max-width: 13rem;
142
+ margin-left: auto;
143
+ }
144
+
145
+ #share-btn-container:hover {
146
+ background-color: #060606;
147
+ }
148
+
149
+ #share-btn {
150
+ all: initial;
151
+ color: #ffffff;
152
+ font-weight: 600;
153
+ cursor: pointer;
154
+ font-family: 'IBM Plex Sans', sans-serif;
155
+ margin-left: 0.5rem !important;
156
+ padding: 0.5rem !important;
157
+ right: 0;
158
+ }
159
+
160
+ /* Animation Styles */
161
+ .animate-spin {
162
+ animation: spin 1s linear infinite;
163
+ }
164
+
165
+ @keyframes spin {
166
+ from { transform: rotate(0deg); }
167
+ to { transform: rotate(360deg); }
168
+ }
169
+
170
+ /* Other Styles */
171
+ #gallery {
172
+ min-height: 22rem;
173
+ margin-bottom: 15px;
174
+ margin-left: auto;
175
+ margin-right: auto;
176
+ border-bottom-right-radius: .5rem !important;
177
+ border-bottom-left-radius: .5rem !important;
178
+ }
179
+ """
180
 
181
+ Create the app interface
182
+ with gr.Interface(generate_txt2img,
183
+ title="AI Diffusion",
184
+ description="Generate images from text prompts using different AI models and styles",
185
+ examples=[
186
+ ["a cute dog"],
187
+ ["a beautiful sunset"],
188
+ ["a fantasy world"]
189
+ ],
190
+ layout="vertical",
191
+ css=css) as demo:
192
+ # Add model selection dropdown
193
+ demo.inputs[0].label = "Current Model"
194
 
195
+ # Add prompt text input and generate button
196
+ demo.inputs[1].label = "Prompt"
197
+ demo.inputs[1].placeholder = "Enter your text prompt here"
198
+ demo.inputs[1].lines = 1
199
+ demo.inputs[2].visibility = "hidden" # Hide negative prompt textbox initially
200
 
201
+ @demo.inputs[3].callback
202
+ def style_selected(value):
203
+ # Show or hide negative prompt textbox based on the selected style
204
+ if value == "None style":
205
+ demo.inputs[2].visibility = "hidden"
206
+ else:
207
+ demo.inputs[2].visibility = "visible"
208
 
209
+ # Add image output
210
+ demo.outputs[0].label = "Generated Image"
 
 
 
211
 
212
+ # Run the app
213
  demo.launch()