Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,24 @@ from PIL import Image
|
|
8 |
from fastapi import FastAPI, Request
|
9 |
from translatepy import Translator
|
10 |
|
11 |
-
css_code =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Initialize translator
|
14 |
translator = Translator()
|
@@ -71,70 +88,99 @@ async def api_generate(request: Request):
|
|
71 |
# Gradio Interface
|
72 |
def make_me():
|
73 |
with gr.Row():
|
74 |
-
|
|
|
75 |
txt_input = gr.Textbox(
|
76 |
label='Your prompt:',
|
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('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
|
90 |
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
# Translate prompt to English
|
115 |
-
translated_prompt = str(translator.translate(prompt, 'English'))
|
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 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# Create Gradio app
|
137 |
-
with gr.Blocks(css=css_code) as demo:
|
|
|
138 |
make_me()
|
139 |
|
140 |
# Enable queue before mounting
|
|
|
8 |
from fastapi import FastAPI, Request
|
9 |
from translatepy import Translator
|
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 = Translator()
|
|
|
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 = str(translator.translate(prompt, 'English'))
|
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
|