Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,27 +8,10 @@ from PIL import Image
|
|
| 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 |
|
| 30 |
# Initialize translator
|
| 31 |
-
translator =
|
| 32 |
|
| 33 |
# Load models
|
| 34 |
models_load = {}
|
|
@@ -88,99 +71,70 @@ async def api_generate(request: Request):
|
|
| 88 |
# Gradio Interface
|
| 89 |
def make_me():
|
| 90 |
with gr.Row():
|
| 91 |
-
|
| 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="
|
| 99 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
label="Select LoRA Model",
|
| 104 |
-
value=models[0] if models else None,
|
| 105 |
-
container=False
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 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 |
-
|
| 131 |
-
|
| 132 |
-
container=False
|
| 133 |
-
)
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 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
|
| 183 |
-
gr.Markdown("# Image Generation Tool")
|
| 184 |
make_me()
|
| 185 |
|
| 186 |
# Enable queue before mounting
|
|
|
|
| 8 |
from fastapi import FastAPI, Request
|
| 9 |
from deep_translator import GoogleTranslator
|
| 10 |
|
| 11 |
+
css_code = os.getenv("DazDinGo_CSS")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Initialize translator
|
| 14 |
+
translator = DeepTranslator(source="auto", target="en")
|
| 15 |
|
| 16 |
# Load models
|
| 17 |
models_load = {}
|
|
|
|
| 71 |
# Gradio Interface
|
| 72 |
def make_me():
|
| 73 |
with gr.Row():
|
| 74 |
+
with gr.Column(scale=4):
|
|
|
|
| 75 |
txt_input = gr.Textbox(
|
| 76 |
label='Your prompt:',
|
| 77 |
lines=4,
|
| 78 |
container=False,
|
| 79 |
elem_id="custom_textbox",
|
| 80 |
+
placeholder="Prompt"
|
| 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('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_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])
|
| 96 |
+
|
| 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,
|
| 104 |
+
height=768,
|
| 105 |
elem_id="custom_image",
|
| 106 |
show_label=True,
|
| 107 |
interactive=False
|
| 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
|
| 115 |
+
translated_prompt = translator.translate(prompt)
|
| 116 |
+
image = gen_image(model_str, translated_prompt)
|
| 117 |
+
if image is None:
|
| 118 |
+
return None, {"error": "Generation failed"}
|
| 119 |
+
|
| 120 |
+
base64_str = image_to_base64(image)
|
| 121 |
+
response = {
|
| 122 |
+
"status": "success",
|
| 123 |
+
"model": model_str,
|
| 124 |
+
"original_prompt": prompt,
|
| 125 |
+
"translated_prompt": translated_prompt,
|
| 126 |
+
"image_base64": base64_str,
|
| 127 |
+
"image_format": "jpeg"
|
| 128 |
+
}
|
| 129 |
+
return image, response
|
| 130 |
+
|
| 131 |
+
gen_event = gen_button.click(generate_wrapper, [model_dropdown, txt_input],
|
| 132 |
+
[output_image, json_output])
|
| 133 |
+
stop_button.click(on_stop_click, inputs=None,
|
| 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
|