Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,145 @@ from PIL import Image
|
|
| 8 |
from fastapi import FastAPI, Request
|
| 9 |
from deep_translator import GoogleTranslator
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Initialize translator
|
| 14 |
translator = GoogleTranslator(source='auto', target='en')
|
|
@@ -77,19 +215,20 @@ def make_me():
|
|
| 77 |
lines=4,
|
| 78 |
container=False,
|
| 79 |
elem_id="custom_textbox",
|
| 80 |
-
placeholder="
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
with gr.Column(scale=1):
|
| 84 |
-
gen_button = gr.Button('Generate image', elem_id="custom_gen_button")
|
| 85 |
stop_button = gr.Button('Stop', variant='secondary', interactive=False,
|
| 86 |
elem_id="custom_stop_button")
|
| 87 |
|
| 88 |
def on_generate_click():
|
| 89 |
-
return gr.Button('
|
| 90 |
|
| 91 |
def on_stop_click():
|
| 92 |
-
return gr.Button('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
| 93 |
|
| 94 |
gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
|
| 95 |
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
|
|
@@ -97,7 +236,9 @@ def make_me():
|
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
| 99 |
model_dropdown = gr.Dropdown(models, label="Select Model",
|
| 100 |
-
value=models[0] if models else None
|
|
|
|
|
|
|
| 101 |
output_image = gr.Image(
|
| 102 |
label="Generated Image",
|
| 103 |
width=512,
|
|
@@ -108,7 +249,7 @@ def make_me():
|
|
| 108 |
)
|
| 109 |
|
| 110 |
# JSON output
|
| 111 |
-
json_output = gr.JSON(label="API Response")
|
| 112 |
|
| 113 |
def generate_wrapper(model_str, prompt):
|
| 114 |
# Translate prompt to English
|
|
@@ -134,12 +275,13 @@ def make_me():
|
|
| 134 |
outputs=[gen_button, stop_button], cancels=[gen_event])
|
| 135 |
|
| 136 |
# Create Gradio app
|
| 137 |
-
with gr.Blocks(css=css_code) as demo:
|
|
|
|
|
|
|
| 138 |
make_me()
|
| 139 |
|
| 140 |
# Enable queue before mounting
|
| 141 |
-
|
| 142 |
-
demo.queue()
|
| 143 |
|
| 144 |
# Mount Gradio app to FastAPI
|
| 145 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 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: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
|
| 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: linear-gradient(to right, #ff4d4d 0%, #f97878 100%);
|
| 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 |
+
border-radius: 16px;
|
| 82 |
+
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
|
| 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 |
|
| 151 |
# Initialize translator
|
| 152 |
translator = GoogleTranslator(source='auto', target='en')
|
|
|
|
| 215 |
lines=4,
|
| 216 |
container=False,
|
| 217 |
elem_id="custom_textbox",
|
| 218 |
+
placeholder="Describe the image you want to generate...",
|
| 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 |
def on_generate_click():
|
| 228 |
+
return gr.Button('Generating...', interactive=False, elem_id="custom_gen_button", variant="stop"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
|
| 229 |
|
| 230 |
def on_stop_click():
|
| 231 |
+
return gr.Button('Generate image', elem_id="custom_gen_button", variant="primary"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
| 232 |
|
| 233 |
gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
|
| 234 |
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
|
|
|
|
| 236 |
with gr.Row():
|
| 237 |
with gr.Column():
|
| 238 |
model_dropdown = gr.Dropdown(models, label="Select Model",
|
| 239 |
+
value=models[0] if models else None,
|
| 240 |
+
interactive=True,
|
| 241 |
+
elem_classes=["model-selector"])
|
| 242 |
output_image = gr.Image(
|
| 243 |
label="Generated Image",
|
| 244 |
width=512,
|
|
|
|
| 249 |
)
|
| 250 |
|
| 251 |
# JSON output
|
| 252 |
+
json_output = gr.JSON(label="API Response", elem_id="api-response")
|
| 253 |
|
| 254 |
def generate_wrapper(model_str, prompt):
|
| 255 |
# Translate prompt to English
|
|
|
|
| 275 |
outputs=[gen_button, stop_button], cancels=[gen_event])
|
| 276 |
|
| 277 |
# Create Gradio app
|
| 278 |
+
with gr.Blocks(css=css_code, theme=gr.themes.Soft()) as demo:
|
| 279 |
+
gr.Markdown("# AI Image Generator", elem_id="title")
|
| 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
|
| 284 |
+
demo.queue(concurrency_count=5)
|
|
|
|
| 285 |
|
| 286 |
# Mount Gradio app to FastAPI
|
| 287 |
app = gr.mount_gradio_app(app, demo, path="/")
|