ngoctuanai commited on
Commit
44aa685
·
1 Parent(s): 6e30aa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -161
app.py CHANGED
@@ -16,179 +16,68 @@ list_models = [
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
-
90
- /* Button Styles */
91
- .gr-button {
92
- color: white;
93
- border-color: black;
94
- background: black;
95
- white-space: nowrap;
96
- }
97
-
98
- .gr-button:focus {
99
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
100
- outline: none;
101
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
102
- --tw-border-opacity: 1;
103
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
104
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
105
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
106
- --tw-ring-opacity: .5;
107
- }
108
-
109
- /* Footer Styles */
110
- .footer, .dark .footer {
111
- margin-bottom: 45px;
112
- margin-top: 35px;
113
- text-align: center;
114
- border-bottom: 1px solid #e5e5e5;
115
- }
116
-
117
- .footer > p, .dark .footer > p {
118
- font-size: .8rem;
119
- display: inline-block;
120
- padding: 0 10px;
121
- transform: translateY(10px);
122
- background: white;
123
- }
124
-
125
- .dark .footer {
126
- border-color: #303030;
127
  }
128
-
129
- .dark .footer > p {
130
- background: #0b0f19;
131
  }
132
-
133
- /* Share Button Styles */
134
- #share-btn-container {
135
- padding: 0 0.5rem !important;
136
- background-color: #000000;
137
- justify-content: center;
138
- align-items: center;
139
- border-radius: 9999px !important;
140
- max-width: 13rem;
141
- margin-left: auto;
142
  }
143
-
144
- #share-btn-container:hover {
145
- background-color: #060606;
146
- }
147
-
148
- #share-btn {
149
- all: initial;
150
- color: #ffffff;
151
- font-weight: 600;
152
- cursor: pointer;
153
- font-family: 'IBM Plex Sans', sans-serif;
154
- margin-left: 0.5rem !important;
155
- padding: 0.5rem !important;
156
- right: 0;
157
- }
158
-
159
- /* Animation Styles */
160
- .animate-spin {
161
- animation: spin 1s linear infinite;
162
- }
163
-
164
- @keyframes spin {
165
- from { transform: rotate(0deg); }
166
- to { transform: rotate(360deg); }
167
  }
168
-
169
- /* Other Styles */
170
- #gallery {
171
- min-height: 22rem;
172
- margin-bottom: 15px;
173
- margin-left: auto;
174
- margin-right: auto;
175
- border-bottom-right-radius: .5rem !important;
176
- border-bottom-left-radius: .5rem !important;
177
  }
178
  """
179
 
 
180
  with gr.Blocks(css=css) as demo:
181
- gr.Markdown("# AI Diffusion")
 
182
  with gr.Row():
183
- with gr.Column():
184
- current_model = gr.Dropdown(label="Current Model", choices=list_models, value=list_models[1])
185
- text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a description of the image", lines=2)
186
- negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter undesired elements", lines=2)
187
- image_style = gr.Dropdown(label="Image Style", choices=["None", "Cinematic", "Digital Art", "Portrait"], value="None")
188
- text_button = gr.Button("Generate", variant='primary')
189
- with gr.Column():
190
- image_output = gr.Image(type="pil", label="Output Image")
191
 
192
- text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
 
 
 
 
 
193
 
194
- demo.launch(show_api=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()