Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,143 +8,22 @@ from PIL import Image
|
|
| 8 |
from fastapi import FastAPI, Request
|
| 9 |
from deep_translator import GoogleTranslator
|
| 10 |
|
| 11 |
-
# CSS yang lebih lengkap dengan berbagai elemen styling
|
| 12 |
css_code = """
|
| 13 |
-
/* General Styling */
|
| 14 |
-
.gradio-container {
|
| 15 |
-
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 16 |
-
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 17 |
-
min-height: 100vh;
|
| 18 |
-
padding: 20px;
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
/* Textbox Styling */
|
| 22 |
#custom_textbox {
|
|
|
|
| 23 |
min-height: 150px;
|
| 24 |
-
padding: 15px;
|
| 25 |
-
border-radius: 12px;
|
| 26 |
-
border: 2px solid #e0e0e0;
|
| 27 |
-
font-size: 16px;
|
| 28 |
-
background: rgba(255, 255, 255, 0.9);
|
| 29 |
-
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
|
| 30 |
-
transition: all 0.3s ease;
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
#custom_textbox:focus {
|
| 34 |
-
border-color: #4facfe;
|
| 35 |
-
box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.2);
|
| 36 |
-
outline: none;
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
/* Button Styling */
|
| 40 |
#custom_gen_button {
|
| 41 |
-
background:
|
| 42 |
-
color: white;
|
| 43 |
-
border: none;
|
| 44 |
-
border-radius: 12px;
|
| 45 |
-
padding: 12px 24px;
|
| 46 |
-
font-weight: 600;
|
| 47 |
-
font-size: 16px;
|
| 48 |
-
cursor: pointer;
|
| 49 |
-
transition: all 0.3s ease;
|
| 50 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 51 |
-
width: 100%;
|
| 52 |
-
margin-bottom: 10px;
|
| 53 |
}
|
| 54 |
-
|
| 55 |
-
#custom_gen_button:hover {
|
| 56 |
-
transform: translateY(-2px);
|
| 57 |
-
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
#custom_stop_button {
|
| 61 |
-
background:
|
| 62 |
-
color: white;
|
| 63 |
-
border: none;
|
| 64 |
-
border-radius: 12px;
|
| 65 |
-
padding: 12px 24px;
|
| 66 |
-
font-weight: 600;
|
| 67 |
-
font-size: 16px;
|
| 68 |
-
cursor: pointer;
|
| 69 |
-
transition: all 0.3s ease;
|
| 70 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 71 |
-
width: 100%;
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
#custom_stop_button:hover {
|
| 75 |
-
transform: translateY(-2px);
|
| 76 |
-
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
|
| 77 |
}
|
| 78 |
-
|
| 79 |
-
/* Image Styling */
|
| 80 |
#custom_image {
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
transition: all 0.3s ease;
|
| 84 |
-
background: white;
|
| 85 |
-
padding: 10px;
|
| 86 |
-
max-width: 100%;
|
| 87 |
-
height: auto;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
#custom_image:hover {
|
| 91 |
-
transform: scale(1.01);
|
| 92 |
-
box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
/* Dropdown Styling */
|
| 96 |
-
.dark .dropdown-option {
|
| 97 |
-
background: #2d3748 !important;
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
.dark .dropdown-option:hover {
|
| 101 |
-
background: #4a5568 !important;
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
/* Panel Styling */
|
| 105 |
-
.gr-box {
|
| 106 |
-
border-radius: 16px !important;
|
| 107 |
-
background: rgba(255, 255, 255, 0.8) !important;
|
| 108 |
-
backdrop-filter: blur(10px);
|
| 109 |
-
border: 1px solid rgba(255, 255, 255, 0.2) !important;
|
| 110 |
-
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important;
|
| 111 |
-
padding: 20px !important;
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
/* Label Styling */
|
| 115 |
-
.gr-label {
|
| 116 |
-
font-weight: 600 !important;
|
| 117 |
-
color: #2d3748 !important;
|
| 118 |
-
margin-bottom: 8px !important;
|
| 119 |
-
font-size: 16px !important;
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
/* Responsive Adjustments */
|
| 123 |
-
@media (max-width: 768px) {
|
| 124 |
-
.gradio-container {
|
| 125 |
-
padding: 10px;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
#custom_textbox {
|
| 129 |
-
min-height: 120px;
|
| 130 |
-
font-size: 14px;
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
#custom_gen_button, #custom_stop_button {
|
| 134 |
-
padding: 10px 15px;
|
| 135 |
-
font-size: 14px;
|
| 136 |
-
}
|
| 137 |
-
}
|
| 138 |
-
|
| 139 |
-
/* Animation for loading state */
|
| 140 |
-
@keyframes pulse {
|
| 141 |
-
0% { opacity: 0.6; }
|
| 142 |
-
50% { opacity: 1; }
|
| 143 |
-
100% { opacity: 0.6; }
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
.loading {
|
| 147 |
-
animation: pulse 1.5s infinite;
|
| 148 |
}
|
| 149 |
"""
|
| 150 |
|
|
@@ -209,75 +88,99 @@ async def api_generate(request: Request):
|
|
| 209 |
# Gradio Interface
|
| 210 |
def make_me():
|
| 211 |
with gr.Row():
|
| 212 |
-
|
|
|
|
| 213 |
txt_input = gr.Textbox(
|
| 214 |
label='Your prompt:',
|
| 215 |
lines=4,
|
| 216 |
container=False,
|
| 217 |
elem_id="custom_textbox",
|
| 218 |
-
placeholder="
|
| 219 |
-
interactive=True
|
| 220 |
)
|
| 221 |
-
|
| 222 |
-
with gr.Column(scale=1):
|
| 223 |
-
gen_button = gr.Button('Generate image', elem_id="custom_gen_button", variant="primary")
|
| 224 |
-
stop_button = gr.Button('Stop', variant='secondary', interactive=False,
|
| 225 |
-
elem_id="custom_stop_button")
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
output_image = gr.Image(
|
| 243 |
label="Generated Image",
|
| 244 |
-
width=512,
|
| 245 |
-
height=768,
|
| 246 |
elem_id="custom_image",
|
| 247 |
show_label=True,
|
| 248 |
interactive=False
|
| 249 |
)
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
# Translate prompt to English
|
| 256 |
-
translated_prompt = translator.translate(prompt)
|
| 257 |
-
image = gen_image(model_str, translated_prompt)
|
| 258 |
-
if image is None:
|
| 259 |
-
return None, {"error": "Generation failed"}
|
| 260 |
-
|
| 261 |
-
base64_str = image_to_base64(image)
|
| 262 |
-
response = {
|
| 263 |
-
"status": "success",
|
| 264 |
-
"model": model_str,
|
| 265 |
-
"original_prompt": prompt,
|
| 266 |
-
"translated_prompt": translated_prompt,
|
| 267 |
-
"image_base64": base64_str,
|
| 268 |
-
"image_format": "jpeg"
|
| 269 |
-
}
|
| 270 |
-
return image, response
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
# Create Gradio app
|
| 278 |
-
with gr.Blocks(css=css_code,
|
| 279 |
-
gr.Markdown("#
|
| 280 |
-
gr.Markdown("Generate stunning images from text prompts using advanced AI models", elem_id="subtitle")
|
| 281 |
make_me()
|
| 282 |
|
| 283 |
# Enable queue before mounting
|
|
|
|
| 8 |
from fastapi import FastAPI, Request
|
| 9 |
from deep_translator import GoogleTranslator
|
| 10 |
|
|
|
|
| 11 |
css_code = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
#custom_textbox {
|
| 13 |
+
width: 100%;
|
| 14 |
min-height: 150px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
#custom_gen_button {
|
| 17 |
+
background: #4CAF50 !important;
|
| 18 |
+
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
#custom_stop_button {
|
| 21 |
+
background: #F44336 !important;
|
| 22 |
+
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
|
|
|
|
|
|
| 24 |
#custom_image {
|
| 25 |
+
width: 100%;
|
| 26 |
+
max-height: 768px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
"""
|
| 29 |
|
|
|
|
| 88 |
# Gradio Interface
|
| 89 |
def make_me():
|
| 90 |
with gr.Row():
|
| 91 |
+
# Left Column (50% width)
|
| 92 |
+
with gr.Column(scale=1, min_width=400):
|
| 93 |
txt_input = gr.Textbox(
|
| 94 |
label='Your prompt:',
|
| 95 |
lines=4,
|
| 96 |
container=False,
|
| 97 |
elem_id="custom_textbox",
|
| 98 |
+
placeholder="Enter your prompt here..."
|
|
|
|
| 99 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
model_dropdown = gr.Dropdown(
|
| 102 |
+
models,
|
| 103 |
+
label="Select LoRA Model",
|
| 104 |
+
value=models[0] if models else None,
|
| 105 |
+
container=False
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
with gr.Row():
|
| 109 |
+
gen_button = gr.Button(
|
| 110 |
+
'Generate Image',
|
| 111 |
+
elem_id="custom_gen_button",
|
| 112 |
+
variant='primary'
|
| 113 |
+
)
|
| 114 |
+
stop_button = gr.Button(
|
| 115 |
+
'Stop',
|
| 116 |
+
variant='stop',
|
| 117 |
+
elem_id="custom_stop_button",
|
| 118 |
+
interactive=False
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Right Column (50% width)
|
| 122 |
+
with gr.Column(scale=1, min_width=400):
|
| 123 |
output_image = gr.Image(
|
| 124 |
label="Generated Image",
|
|
|
|
|
|
|
| 125 |
elem_id="custom_image",
|
| 126 |
show_label=True,
|
| 127 |
interactive=False
|
| 128 |
)
|
| 129 |
|
| 130 |
+
json_output = gr.JSON(
|
| 131 |
+
label="API Response",
|
| 132 |
+
container=False
|
| 133 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
# Functionality remains the same
|
| 136 |
+
def generate_wrapper(model_str, prompt):
|
| 137 |
+
# Translate prompt to English
|
| 138 |
+
translated_prompt = translator.translate(prompt)
|
| 139 |
+
image = gen_image(model_str, translated_prompt)
|
| 140 |
+
if image is None:
|
| 141 |
+
return None, {"error": "Generation failed"}
|
| 142 |
+
|
| 143 |
+
base64_str = image_to_base64(image)
|
| 144 |
+
response = {
|
| 145 |
+
"status": "success",
|
| 146 |
+
"model": model_str,
|
| 147 |
+
"original_prompt": prompt,
|
| 148 |
+
"translated_prompt": translated_prompt,
|
| 149 |
+
"image_base64": base64_str,
|
| 150 |
+
"image_format": "jpeg"
|
| 151 |
+
}
|
| 152 |
+
return image, response
|
| 153 |
+
|
| 154 |
+
def on_generate_click():
|
| 155 |
+
return gr.Button(interactive=False), gr.Button(interactive=True)
|
| 156 |
+
|
| 157 |
+
def on_stop_click():
|
| 158 |
+
return gr.Button(interactive=True), gr.Button(interactive=False)
|
| 159 |
+
|
| 160 |
+
gen_event = gen_button.click(
|
| 161 |
+
on_generate_click,
|
| 162 |
+
inputs=None,
|
| 163 |
+
outputs=[gen_button, stop_button]
|
| 164 |
+
).then(
|
| 165 |
+
generate_wrapper,
|
| 166 |
+
[model_dropdown, txt_input],
|
| 167 |
+
[output_image, json_output]
|
| 168 |
+
).then(
|
| 169 |
+
on_stop_click,
|
| 170 |
+
inputs=None,
|
| 171 |
+
outputs=[gen_button, stop_button]
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
stop_button.click(
|
| 175 |
+
on_stop_click,
|
| 176 |
+
inputs=None,
|
| 177 |
+
outputs=[gen_button, stop_button],
|
| 178 |
+
cancels=[gen_event]
|
| 179 |
+
)
|
| 180 |
|
| 181 |
# Create Gradio app
|
| 182 |
+
with gr.Blocks(css=css_code, title="Image Generation App") as demo:
|
| 183 |
+
gr.Markdown("# Image Generation Tool")
|
|
|
|
| 184 |
make_me()
|
| 185 |
|
| 186 |
# Enable queue before mounting
|