Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,13 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import requests
|
| 4 |
import logging
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Set up logging
|
| 7 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -21,6 +27,28 @@ extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, si
|
|
| 21 |
cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face
|
| 22 |
"""
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def enhance_prompt(google_api_key, prompt, style):
|
| 25 |
genai.configure(api_key=google_api_key)
|
| 26 |
model = genai.GenerativeModel("gemini-2.0-flash-lite")
|
|
@@ -96,37 +124,42 @@ def process_and_generate(google_api_key, stability_api_key, prompt, style, negat
|
|
| 96 |
try:
|
| 97 |
enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
|
| 98 |
image_bytes = generate_image(stability_api_key, enhanced_prompt, style, negative_prompt)
|
| 99 |
-
|
| 100 |
-
# Save image to a file
|
| 101 |
-
with open("generated_image.jpeg", "wb") as f:
|
| 102 |
-
f.write(image_bytes)
|
| 103 |
-
|
| 104 |
-
# Return the file path and enhanced prompt
|
| 105 |
-
return "generated_image.jpeg", enhanced_prompt
|
| 106 |
except Exception as e:
|
| 107 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
| 108 |
-
return str(e)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
|
| 126 |
-
|
| 127 |
-
process_and_generate,
|
| 128 |
-
inputs=[google_api_key, stability_api_key, prompt, style, negative_prompt],
|
| 129 |
-
outputs=[image_output, enhanced_prompt_output]
|
| 130 |
-
)
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import dash
|
| 3 |
+
from dash import dcc, html, Input, Output, State
|
| 4 |
+
import dash_bootstrap_components as dbc
|
| 5 |
+
from dash.exceptions import PreventUpdate
|
| 6 |
import google.generativeai as genai
|
| 7 |
import requests
|
| 8 |
import logging
|
| 9 |
+
import threading
|
| 10 |
+
import io
|
| 11 |
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 27 |
cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 31 |
+
|
| 32 |
+
app.layout = dbc.Container([
|
| 33 |
+
html.H1("Stability AI SD3.5 Large Turbo Image Generator with Google Gemini Prompt Enhancement", className="my-4"),
|
| 34 |
+
dbc.Card([
|
| 35 |
+
dbc.CardBody([
|
| 36 |
+
dbc.Input(id="google-api-key", type="password", placeholder="Enter Google AI API Key", className="mb-3"),
|
| 37 |
+
dbc.Input(id="stability-api-key", type="password", placeholder="Enter Stability AI API Key", className="mb-3"),
|
| 38 |
+
dbc.Textarea(id="prompt", placeholder="Enter your prompt", className="mb-3"),
|
| 39 |
+
dcc.Dropdown(id="style", options=[{"label": s, "value": s} for s in STYLES], placeholder="Select style", className="mb-3"),
|
| 40 |
+
dbc.Textarea(id="negative-prompt", value=DEFAULT_NEGATIVE_PROMPT, className="mb-3"),
|
| 41 |
+
dbc.Button("Generate Image", id="submit-btn", color="primary", className="mb-3"),
|
| 42 |
+
])
|
| 43 |
+
], className="mb-4"),
|
| 44 |
+
dbc.Card([
|
| 45 |
+
dbc.CardBody([
|
| 46 |
+
html.Img(id="image-output", className="img-fluid"),
|
| 47 |
+
html.Div(id="enhanced-prompt-output", className="mt-3"),
|
| 48 |
+
])
|
| 49 |
+
])
|
| 50 |
+
], fluid=True)
|
| 51 |
+
|
| 52 |
def enhance_prompt(google_api_key, prompt, style):
|
| 53 |
genai.configure(api_key=google_api_key)
|
| 54 |
model = genai.GenerativeModel("gemini-2.0-flash-lite")
|
|
|
|
| 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"),
|
| 138 |
+
State("prompt", "value"),
|
| 139 |
+
State("style", "value"),
|
| 140 |
+
State("negative-prompt", "value")],
|
| 141 |
+
prevent_initial_call=True
|
| 142 |
+
)
|
| 143 |
+
def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, negative_prompt):
|
| 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)
|
| 157 |
+
thread.start()
|
| 158 |
+
thread.join() # Wait for the thread to complete
|
| 159 |
|
| 160 |
+
return run_process()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
if __name__ == '__main__':
|
| 163 |
+
print("Starting the Dash application...")
|
| 164 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|
| 165 |
+
print("Dash application has finished running.")
|