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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -14,30 +14,38 @@ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %
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"
21
  ]
22
 
23
- # Default negative prompt
24
  DEFAULT_NEGATIVE_PROMPT = """
25
  ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame,
26
  extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, signature,
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"),
@@ -60,16 +68,18 @@ def enhance_prompt(google_api_key, prompt, style):
60
  genai.configure(api_key=google_api_key)
61
  model = genai.GenerativeModel("gemini-2.0-flash-lite")
62
  enhanced_prompt_request = f"""
63
- Task: Enhance the following prompt for image generation.
64
  Style: {style}
65
  Original prompt: '{prompt}'
66
 
67
  Instructions:
68
- 1. Expand the prompt to be more detailed and vivid.
69
- 2. Incorporate elements of the specified style.
70
- 3. Maintain the original intent of the prompt.
71
- 4. Provide ONLY the enhanced prompt, without any explanations or options.
72
- 5. Keep the enhanced prompt concise, ideally under 100 words.
 
 
73
 
74
  Enhanced prompt:
75
  """
@@ -90,7 +100,7 @@ def enhance_prompt(google_api_key, prompt, style):
90
  logging.error(f"Error in enhance_prompt: {str(e)}")
91
  raise
92
 
93
- def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
94
  url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
95
 
96
  headers = {
@@ -99,14 +109,15 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
99
  }
100
 
101
  data = {
102
- "prompt": f"{enhanced_prompt}, Style: {style}",
103
  "negative_prompt": negative_prompt,
104
  "model": "sd3.5-large-turbo",
105
  "output_format": "jpeg",
106
  "width": 1024,
107
  "height": 1024,
108
  "num_images": 1,
109
- "steps": 4, # SD3.5 Large Turbo generates high-quality images in just 4 steps
 
110
  }
111
 
112
  try:
@@ -127,13 +138,13 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
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
@@ -151,10 +162,11 @@ def process_and_generate(google_api_key, stability_api_key, prompt, style, negat
151
  State("stability-api-key", "value"),
152
  State("prompt", "value"),
153
  State("style", "value"),
154
- State("negative-prompt", "value")],
 
155
  prevent_initial_call=True
156
  )
157
- def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, negative_prompt):
158
  if n_clicks is None:
159
  raise PreventUpdate
160
 
@@ -164,7 +176,7 @@ def update_output(n_clicks, google_api_key, stability_api_key, prompt, style, ne
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"]
 
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"
21
  ]
22
 
23
+ # Default negative prompt (hidden from UI)
24
  DEFAULT_NEGATIVE_PROMPT = """
25
  ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame,
26
  extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, signature,
27
+ cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face,
28
+ plastic, cartoonish, artificial, fake, unnatural
29
  """
30
 
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"),
 
68
  genai.configure(api_key=google_api_key)
69
  model = genai.GenerativeModel("gemini-2.0-flash-lite")
70
  enhanced_prompt_request = f"""
71
+ Task: Enhance the following prompt for high-quality, realistic image generation.
72
  Style: {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
  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
  }
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",
116
  "width": 1024,
117
  "height": 1024,
118
  "num_images": 1,
119
+ "steps": steps,
120
+ "cfg_scale": cfg_scale,
121
  }
122
 
123
  try:
 
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
 
162
  State("stability-api-key", "value"),
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
  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"]