Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,8 +43,15 @@ app.layout = dbc.Container([
|
|
| 43 |
], className="mb-4"),
|
| 44 |
dbc.Card([
|
| 45 |
dbc.CardBody([
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
])
|
| 49 |
])
|
| 50 |
], fluid=True)
|
|
@@ -120,18 +127,25 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
|
| 120 |
logging.error(f"Request failed: {str(e)}")
|
| 121 |
raise Exception(f"Request failed: {str(e)}")
|
| 122 |
|
| 123 |
-
def process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt):
|
| 124 |
try:
|
|
|
|
| 125 |
enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
|
|
|
|
|
|
|
| 126 |
image_bytes = generate_image(stability_api_key, enhanced_prompt, style, negative_prompt)
|
|
|
|
|
|
|
| 127 |
return image_bytes, enhanced_prompt
|
| 128 |
except Exception as e:
|
| 129 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
|
|
|
| 130 |
return None, str(e)
|
| 131 |
|
| 132 |
@app.callback(
|
| 133 |
[Output("image-output", "src"),
|
| 134 |
-
Output("enhanced-prompt-output", "children")
|
|
|
|
| 135 |
[Input("submit-btn", "n_clicks")],
|
| 136 |
[State("google-api-key", "value"),
|
| 137 |
State("stability-api-key", "value"),
|
|
@@ -144,13 +158,18 @@ def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, ne
|
|
| 144 |
if n_clicks is None:
|
| 145 |
raise PreventUpdate
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
def run_process():
|
| 148 |
-
image_bytes, enhanced_prompt = process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt)
|
| 149 |
if image_bytes:
|
| 150 |
encoded_image = base64.b64encode(image_bytes).decode('ascii')
|
| 151 |
-
return f"data:image/jpeg;base64,{encoded_image}", f"Enhanced Prompt: {enhanced_prompt}"
|
| 152 |
else:
|
| 153 |
-
return "", f"Error: {enhanced_prompt}"
|
| 154 |
|
| 155 |
# Run the process in a separate thread
|
| 156 |
thread = threading.Thread(target=run_process)
|
|
|
|
| 43 |
], className="mb-4"),
|
| 44 |
dbc.Card([
|
| 45 |
dbc.CardBody([
|
| 46 |
+
dcc.Loading(
|
| 47 |
+
id="loading",
|
| 48 |
+
type="circle",
|
| 49 |
+
children=[
|
| 50 |
+
html.Div(id="status-message", className="mb-3"),
|
| 51 |
+
html.Img(id="image-output", className="img-fluid"),
|
| 52 |
+
html.Div(id="enhanced-prompt-output", className="mt-3"),
|
| 53 |
+
]
|
| 54 |
+
),
|
| 55 |
])
|
| 56 |
])
|
| 57 |
], fluid=True)
|
|
|
|
| 127 |
logging.error(f"Request failed: {str(e)}")
|
| 128 |
raise Exception(f"Request failed: {str(e)}")
|
| 129 |
|
| 130 |
+
def process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt, set_status):
|
| 131 |
try:
|
| 132 |
+
set_status("Enhancing prompt...")
|
| 133 |
enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
|
| 134 |
+
|
| 135 |
+
set_status("Generating image...")
|
| 136 |
image_bytes = generate_image(stability_api_key, enhanced_prompt, style, negative_prompt)
|
| 137 |
+
|
| 138 |
+
set_status("Image generated successfully!")
|
| 139 |
return image_bytes, enhanced_prompt
|
| 140 |
except Exception as e:
|
| 141 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
| 142 |
+
set_status(f"Error: {str(e)}")
|
| 143 |
return None, str(e)
|
| 144 |
|
| 145 |
@app.callback(
|
| 146 |
[Output("image-output", "src"),
|
| 147 |
+
Output("enhanced-prompt-output", "children"),
|
| 148 |
+
Output("status-message", "children")],
|
| 149 |
[Input("submit-btn", "n_clicks")],
|
| 150 |
[State("google-api-key", "value"),
|
| 151 |
State("stability-api-key", "value"),
|
|
|
|
| 158 |
if n_clicks is None:
|
| 159 |
raise PreventUpdate
|
| 160 |
|
| 161 |
+
status = {"message": "Starting process..."}
|
| 162 |
+
|
| 163 |
+
def set_status(message):
|
| 164 |
+
status["message"] = message
|
| 165 |
+
|
| 166 |
def run_process():
|
| 167 |
+
image_bytes, enhanced_prompt = process_and_generate(google_api_key, stability_api_key, prompt, style, negative_prompt, set_status)
|
| 168 |
if image_bytes:
|
| 169 |
encoded_image = base64.b64encode(image_bytes).decode('ascii')
|
| 170 |
+
return f"data:image/jpeg;base64,{encoded_image}", f"Enhanced Prompt: {enhanced_prompt}", status["message"]
|
| 171 |
else:
|
| 172 |
+
return "", f"Error: {enhanced_prompt}", status["message"]
|
| 173 |
|
| 174 |
# Run the process in a separate thread
|
| 175 |
thread = threading.Thread(target=run_process)
|