bluenevus commited on
Commit
8749f9d
·
verified ·
1 Parent(s): d0410ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -23
app.py CHANGED
@@ -14,7 +14,7 @@ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %
14
 
15
  # List of popular styles
16
  STYLES = [
17
- "Hyper-realistic", "Oil Painting", "Watercolor", "Anime",
18
  "Studio Ghibli", "Black and White", "Polaroid", "Sketch",
19
  "3D Render", "Pixel Art", "Cyberpunk", "Steampunk",
20
  "Art Nouveau", "Pop Art", "Minimalist"
@@ -31,21 +31,65 @@ plastic, cartoonish, artificial, fake, unnatural
31
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
32
 
33
  app.layout = dbc.Container([
34
- html.H1("Insta-Image", className="my-4"),
35
  dbc.Card([
36
  dbc.CardBody([
37
  dbc.Input(id="google-api-key", type="password", placeholder="Enter Google AI API Key", className="mb-3"),
38
  dbc.Input(id="stability-api-key", type="password", placeholder="Enter Stability AI API Key", className="mb-3"),
39
  dbc.Textarea(id="prompt", placeholder="Enter your prompt", className="mb-3"),
40
  dcc.Dropdown(id="style", options=[{"label": s, "value": s} for s in STYLES], placeholder="Select style", className="mb-3"),
41
- dbc.Row([
42
- dbc.Col(dcc.Slider(id="cfg-scale", min=1, max=30, step=1, value=7, marks={1: '1', 15: '15', 30: '30'}, tooltip={"placement": "bottom", "always_visible": True}), width=6),
43
- dbc.Col(html.Div("CFG Scale"), width=6),
44
- ], className="mb-3"),
45
- dbc.Row([
46
- dbc.Col(dcc.Slider(id="steps", min=4, max=20, step=1, value=4, marks={4: '4', 12: '12', 20: '20'}, tooltip={"placement": "bottom", "always_visible": True}), width=6),
47
- dbc.Col(html.Div("Steps"), width=6),
48
- ], className="mb-3"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  dbc.Button("Generate Image", id="submit-btn", color="primary", className="mb-3"),
50
  ])
51
  ], className="mb-4"),
@@ -73,13 +117,14 @@ def enhance_prompt(google_api_key, prompt, style):
73
  Original prompt: '{prompt}'
74
 
75
  Instructions:
76
- 1. Expand the prompt to be more detailed, vivid, and realistic in quality.
77
  2. Incorporate elements of the specified style, focusing on realism and natural appearances.
78
  3. Add details that enhance the realism of the scene, especially for elements like trees, textures, and lighting.
79
- 4. Avoid terms that might result in artificial or cartoonish appearances unless specified.
80
- 5. Maintain the original intent of the prompt while significantly improving its descriptive quality.
81
- 6. Provide ONLY the enhanced prompt, without any explanations or options.
82
- 7. Keep the enhanced prompt concise, ideally under 100 words.
 
83
 
84
  Enhanced prompt:
85
  """
@@ -100,7 +145,7 @@ def enhance_prompt(google_api_key, prompt, style):
100
  logging.error(f"Error in enhance_prompt: {str(e)}")
101
  raise
102
 
103
- def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt, cfg_scale, steps):
104
  url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
105
 
106
  headers = {
@@ -109,7 +154,7 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt, c
109
  }
110
 
111
  data = {
112
- "prompt": f"{enhanced_prompt}, Style: {style}, highly detailed, photorealistic, high quality",
113
  "negative_prompt": negative_prompt,
114
  "model": "sd3.5-large-turbo",
115
  "output_format": "jpeg",
@@ -118,10 +163,11 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt, c
118
  "num_images": 1,
119
  "steps": steps,
120
  "cfg_scale": cfg_scale,
 
121
  }
122
 
123
  try:
124
- response = requests.post(url, headers=headers, files={"none": ''}, data=data)
125
  response.raise_for_status()
126
 
127
  logging.debug(f"Response headers: {response.headers}")
@@ -138,13 +184,13 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt, c
138
  logging.error(f"Request failed: {str(e)}")
139
  raise Exception(f"Request failed: {str(e)}")
140
 
141
- def process_and_generate(google_api_key, stability_api_key, prompt, style, cfg_scale, steps, set_status):
142
  try:
143
  set_status("Enhancing prompt...")
144
  enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
145
 
146
  set_status("Generating image...")
147
- image_bytes = generate_image(stability_api_key, enhanced_prompt, style, DEFAULT_NEGATIVE_PROMPT, cfg_scale, steps)
148
 
149
  set_status("Image generated successfully!")
150
  return image_bytes, enhanced_prompt
@@ -163,10 +209,11 @@ def process_and_generate(google_api_key, stability_api_key, prompt, style, cfg_s
163
  State("prompt", "value"),
164
  State("style", "value"),
165
  State("cfg-scale", "value"),
166
- State("steps", "value")],
 
167
  prevent_initial_call=True
168
  )
169
- def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, cfg_scale, steps):
170
  if n_clicks is None:
171
  raise PreventUpdate
172
 
@@ -176,7 +223,7 @@ def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, cf
176
  status["message"] = message
177
 
178
  def run_process():
179
- image_bytes, enhanced_prompt = process_and_generate(google_api_key, stability_api_key, prompt, style, cfg_scale, steps, set_status)
180
  if image_bytes:
181
  encoded_image = base64.b64encode(image_bytes).decode('ascii')
182
  return f"data:image/jpeg;base64,{encoded_image}", f"Enhanced Prompt: {enhanced_prompt}", status["message"]
 
14
 
15
  # List of popular styles
16
  STYLES = [
17
+ "Photorealistic", "Oil Painting", "Watercolor", "Anime",
18
  "Studio Ghibli", "Black and White", "Polaroid", "Sketch",
19
  "3D Render", "Pixel Art", "Cyberpunk", "Steampunk",
20
  "Art Nouveau", "Pop Art", "Minimalist"
 
31
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
32
 
33
  app.layout = dbc.Container([
34
+ html.H1("Stability AI SD3.5 Large Turbo Image Generator with Google Gemini Prompt Enhancement", className="my-4"),
35
  dbc.Card([
36
  dbc.CardBody([
37
  dbc.Input(id="google-api-key", type="password", placeholder="Enter Google AI API Key", className="mb-3"),
38
  dbc.Input(id="stability-api-key", type="password", placeholder="Enter Stability AI API Key", className="mb-3"),
39
  dbc.Textarea(id="prompt", placeholder="Enter your prompt", className="mb-3"),
40
  dcc.Dropdown(id="style", options=[{"label": s, "value": s} for s in STYLES], placeholder="Select style", className="mb-3"),
41
+ dbc.Accordion([
42
+ dbc.AccordionItem(
43
+ [
44
+ dbc.Row([
45
+ dbc.Col([
46
+ html.Label("CFG Scale:", className="mr-2"),
47
+ dcc.Slider(id="cfg-scale", min=1, max=30, step=0.5, value=7,
48
+ marks={1: '1', 15: '15', 30: '30'},
49
+ tooltip={"placement": "bottom", "always_visible": True}),
50
+ dbc.Tooltip(
51
+ "Controls the influence of the prompt. Higher values adhere more closely to the prompt.",
52
+ target="cfg-scale",
53
+ ),
54
+ ], width=12, className="mb-3"),
55
+ ]),
56
+ dbc.Row([
57
+ dbc.Col([
58
+ html.Label("Steps:", className="mr-2"),
59
+ dcc.Slider(id="steps", min=4, max=50, step=1, value=20,
60
+ marks={4: '4', 25: '25', 50: '50'},
61
+ tooltip={"placement": "bottom", "always_visible": True}),
62
+ dbc.Tooltip(
63
+ "Number of denoising steps. More steps can lead to higher quality but longer generation time.",
64
+ target="steps",
65
+ ),
66
+ ], width=12, className="mb-3"),
67
+ ]),
68
+ dbc.Row([
69
+ dbc.Col([
70
+ html.Label("Sampler:", className="mr-2"),
71
+ dcc.Dropdown(
72
+ id="sampler",
73
+ options=[
74
+ {"label": "DDIM", "value": "DDIM"},
75
+ {"label": "PLMS", "value": "PLMS"},
76
+ {"label": "K_EULER", "value": "K_EULER"},
77
+ {"label": "K_EULER_ANCESTRAL", "value": "K_EULER_ANCESTRAL"},
78
+ {"label": "DPM_2", "value": "DPM_2"},
79
+ {"label": "DPM_2_ANCESTRAL", "value": "DPM_2_ANCESTRAL"},
80
+ ],
81
+ value="K_EULER_ANCESTRAL",
82
+ ),
83
+ dbc.Tooltip(
84
+ "The algorithm used for image generation. Different samplers can produce varying results.",
85
+ target="sampler",
86
+ ),
87
+ ], width=12, className="mb-3"),
88
+ ]),
89
+ ],
90
+ title="Advanced Settings",
91
+ )
92
+ ], start_collapsed=True, className="mb-3"),
93
  dbc.Button("Generate Image", id="submit-btn", color="primary", className="mb-3"),
94
  ])
95
  ], className="mb-4"),
 
117
  Original prompt: '{prompt}'
118
 
119
  Instructions:
120
+ 1. Expand the prompt to be more detailed, vivid, and realism and always include the right camera used for the shot
121
  2. Incorporate elements of the specified style, focusing on realism and natural appearances.
122
  3. Add details that enhance the realism of the scene, especially for elements like trees, textures, and lighting.
123
+ 4. Emphasize natural lighting and enhance the realism of textures and colors.
124
+ 5. Avoid terms that might result in artificial or cartoonish appearances unless specifically requested
125
+ 6. Maintain the original intent of the prompt while significantly improving its descriptive quality.
126
+ 7. Provide ONLY the enhanced prompt, without any explanations or options.
127
+ 8. Keep the enhanced prompt concise, ideally under 100 words.
128
 
129
  Enhanced prompt:
130
  """
 
145
  logging.error(f"Error in enhance_prompt: {str(e)}")
146
  raise
147
 
148
+ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt, cfg_scale, steps, sampler):
149
  url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
150
 
151
  headers = {
 
154
  }
155
 
156
  data = {
157
+ "prompt": f"{enhanced_prompt}, Style: {style}, highly detailed, photorealistic, high quality, natural lighting, realistic textures and colors",
158
  "negative_prompt": negative_prompt,
159
  "model": "sd3.5-large-turbo",
160
  "output_format": "jpeg",
 
163
  "num_images": 1,
164
  "steps": steps,
165
  "cfg_scale": cfg_scale,
166
+ "sampler": sampler,
167
  }
168
 
169
  try:
170
+ response = requests.post(url, headers=headers, json=data)
171
  response.raise_for_status()
172
 
173
  logging.debug(f"Response headers: {response.headers}")
 
184
  logging.error(f"Request failed: {str(e)}")
185
  raise Exception(f"Request failed: {str(e)}")
186
 
187
+ def process_and_generate(google_api_key, stability_api_key, prompt, style, cfg_scale, steps, sampler, set_status):
188
  try:
189
  set_status("Enhancing prompt...")
190
  enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
191
 
192
  set_status("Generating image...")
193
+ image_bytes = generate_image(stability_api_key, enhanced_prompt, style, DEFAULT_NEGATIVE_PROMPT, cfg_scale, steps, sampler)
194
 
195
  set_status("Image generated successfully!")
196
  return image_bytes, enhanced_prompt
 
209
  State("prompt", "value"),
210
  State("style", "value"),
211
  State("cfg-scale", "value"),
212
+ State("steps", "value"),
213
+ State("sampler", "value")],
214
  prevent_initial_call=True
215
  )
216
+ def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, cfg_scale, steps, sampler):
217
  if n_clicks is None:
218
  raise PreventUpdate
219
 
 
223
  status["message"] = message
224
 
225
  def run_process():
226
+ image_bytes, enhanced_prompt = process_and_generate(google_api_key, stability_api_key, prompt, style, cfg_scale, steps, sampler, set_status)
227
  if image_bytes:
228
  encoded_image = base64.b64encode(image_bytes).decode('ascii')
229
  return f"data:image/jpeg;base64,{encoded_image}", f"Enhanced Prompt: {enhanced_prompt}", status["message"]