Spaces:
Runtime error
Runtime error
ngoctuanai
commited on
Commit
·
686fe7a
1
Parent(s):
1f62c1a
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import random
|
|
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",
|
@@ -17,10 +16,8 @@ list_models = [
|
|
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":
|
@@ -41,7 +38,6 @@ elif current_model == "Midjourney-V4-XL":
|
|
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",
|
@@ -75,20 +71,17 @@ elif image_style == "Portrait":
|
|
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 |
-
|
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;
|
@@ -107,7 +100,6 @@ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow
|
|
107 |
--tw-ring-opacity: .5;
|
108 |
}
|
109 |
|
110 |
-
/* Footer Styles */
|
111 |
.footer, .dark .footer {
|
112 |
margin-bottom: 45px;
|
113 |
margin-top: 35px;
|
@@ -131,7 +123,6 @@ border-color: #303030;
|
|
131 |
background: #0b0f19;
|
132 |
}
|
133 |
|
134 |
-
/* Share Button Styles */
|
135 |
#share-btn-container {
|
136 |
padding: 0 0.5rem !important;
|
137 |
background-color: #000000;
|
@@ -157,7 +148,6 @@ padding: 0.5rem !important;
|
|
157 |
right: 0;
|
158 |
}
|
159 |
|
160 |
-
/* Animation Styles */
|
161 |
.animate-spin {
|
162 |
animation: spin 1s linear infinite;
|
163 |
}
|
@@ -167,7 +157,6 @@ from { transform: rotate(0deg); }
|
|
167 |
to { transform: rotate(360deg); }
|
168 |
}
|
169 |
|
170 |
-
/* Other Styles */
|
171 |
#gallery {
|
172 |
min-height: 22rem;
|
173 |
margin-bottom: 15px;
|
@@ -178,8 +167,7 @@ border-bottom-left-radius: .5rem !important;
|
|
178 |
}
|
179 |
"""
|
180 |
|
181 |
-
|
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=[
|
@@ -188,26 +176,4 @@ examples=[
|
|
188 |
["a fantasy world"]
|
189 |
],
|
190 |
layout="vertical",
|
191 |
-
css=css)
|
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()
|
|
|
5 |
import os
|
6 |
from PIL import Image
|
7 |
|
|
|
8 |
list_models = [
|
9 |
"SDXL-1.0",
|
10 |
"SD-1.5",
|
|
|
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":
|
|
|
38 |
API_TOKEN = os.environ.get("HF_READ_TOKEN")
|
39 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
40 |
|
|
|
41 |
if image_style == "None style":
|
42 |
payload = {
|
43 |
"inputs": prompt + ", 8k",
|
|
|
71 |
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
72 |
}
|
73 |
|
|
|
74 |
image_bytes = requests.post(API_URL, headers=headers, json=payload).content
|
75 |
image = Image.open(io.BytesIO(image_bytes))
|
76 |
return image
|
77 |
+
|
78 |
css = """
|
|
|
79 |
.gradio-container {
|
80 |
max-width: 800px !important;
|
81 |
margin: auto;
|
82 |
padding-top: 1.5rem;
|
83 |
}
|
84 |
|
|
|
85 |
.gr-button {
|
86 |
color: white;
|
87 |
border-color: black;
|
|
|
100 |
--tw-ring-opacity: .5;
|
101 |
}
|
102 |
|
|
|
103 |
.footer, .dark .footer {
|
104 |
margin-bottom: 45px;
|
105 |
margin-top: 35px;
|
|
|
123 |
background: #0b0f19;
|
124 |
}
|
125 |
|
|
|
126 |
#share-btn-container {
|
127 |
padding: 0 0.5rem !important;
|
128 |
background-color: #000000;
|
|
|
148 |
right: 0;
|
149 |
}
|
150 |
|
|
|
151 |
.animate-spin {
|
152 |
animation: spin 1s linear infinite;
|
153 |
}
|
|
|
157 |
to { transform: rotate(360deg); }
|
158 |
}
|
159 |
|
|
|
160 |
#gallery {
|
161 |
min-height: 22rem;
|
162 |
margin-bottom: 15px;
|
|
|
167 |
}
|
168 |
"""
|
169 |
|
170 |
+
gr.Interface(generate_txt2img,
|
|
|
171 |
title="AI Diffusion",
|
172 |
description="Generate images from text prompts using different AI models and styles",
|
173 |
examples=[
|
|
|
176 |
["a fantasy world"]
|
177 |
],
|
178 |
layout="vertical",
|
179 |
+
css=css).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|