sumityadav329 commited on
Commit
8626742
·
verified ·
1 Parent(s): 169415d

download image as fixed

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -146,7 +146,7 @@ def query_hf_api(
146
 
147
  raise RuntimeError("Unexpected error in image generation")
148
 
149
- def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Image.Image], str, Optional[str]]:
150
  """
151
  Generate an image from a text prompt.
152
 
@@ -155,8 +155,8 @@ def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Im
155
  output_format (str): Desired output format
156
 
157
  Returns:
158
- Tuple[Optional[Image.Image], str, Optional[str]]:
159
- Generated PIL Image, status message, and path to downloadable image
160
  """
161
  try:
162
  # Validate prompt
@@ -170,17 +170,9 @@ def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Im
170
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
171
 
172
  # Convert image to specified format
173
- image_data = convert_image(image, output_format)
174
 
175
- # Create a temporary file
176
- with tempfile.NamedTemporaryFile(
177
- delete=False,
178
- suffix=f'.{output_format.lower()}'
179
- ) as temp_file:
180
- temp_file.write(image_data)
181
- temp_file_path = temp_file.name
182
-
183
- return image, "Image generated successfully!", temp_file_path
184
 
185
  except Exception as e:
186
  print(f"Image generation error: {e}")
@@ -201,6 +193,9 @@ def create_gradio_interface():
201
  gr.Markdown("# 🎨 AI Image Generator")
202
  gr.Markdown("Generate stunning images from your text prompts using AI!")
203
 
 
 
 
204
  # Input and Output Components
205
  with gr.Row():
206
  with gr.Column(scale=3):
@@ -220,6 +215,9 @@ def create_gradio_interface():
220
 
221
  # Generate Button
222
  generate_button = gr.Button("✨ Generate Image", variant="primary")
 
 
 
223
 
224
  # Output Image Display
225
  with gr.Column(scale=4):
@@ -232,18 +230,31 @@ def create_gradio_interface():
232
  # Status Output
233
  status_output = gr.Textbox(label="Status")
234
 
235
- # Download Button
236
- download_button = gr.File(
237
- label="Download Image",
238
- file_count="single",
239
- type="filepath" # Use filepath type
240
- )
241
-
242
  # Event Handlers
243
  generate_result = generate_button.click(
244
  fn=generate_image,
245
  inputs=[text_input, format_dropdown],
246
- outputs=[output_image, status_output, download_button]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  )
248
 
249
  return demo
 
146
 
147
  raise RuntimeError("Unexpected error in image generation")
148
 
149
+ def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Image.Image], str, Optional[bytes]]:
150
  """
151
  Generate an image from a text prompt.
152
 
 
155
  output_format (str): Desired output format
156
 
157
  Returns:
158
+ Tuple[Optional[Image.Image], str, Optional[bytes]]:
159
+ Generated PIL Image, status message, and downloadable image bytes
160
  """
161
  try:
162
  # Validate prompt
 
170
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
171
 
172
  # Convert image to specified format
173
+ downloadable_image = convert_image(image, output_format)
174
 
175
+ return image, "Image generated successfully!", downloadable_image
 
 
 
 
 
 
 
 
176
 
177
  except Exception as e:
178
  print(f"Image generation error: {e}")
 
193
  gr.Markdown("# 🎨 AI Image Generator")
194
  gr.Markdown("Generate stunning images from your text prompts using AI!")
195
 
196
+ # State to store generated image bytes
197
+ image_bytes_state = gr.State(None)
198
+
199
  # Input and Output Components
200
  with gr.Row():
201
  with gr.Column(scale=3):
 
215
 
216
  # Generate Button
217
  generate_button = gr.Button("✨ Generate Image", variant="primary")
218
+
219
+ # Download Button
220
+ download_button = gr.Button("💾 Download Image", variant="secondary")
221
 
222
  # Output Image Display
223
  with gr.Column(scale=4):
 
230
  # Status Output
231
  status_output = gr.Textbox(label="Status")
232
 
 
 
 
 
 
 
 
233
  # Event Handlers
234
  generate_result = generate_button.click(
235
  fn=generate_image,
236
  inputs=[text_input, format_dropdown],
237
+ outputs=[output_image, status_output, image_bytes_state]
238
+ )
239
+
240
+ # Download event handler
241
+ def download_image(image_bytes):
242
+ if image_bytes is None:
243
+ return None
244
+
245
+ # Create a temporary file
246
+ with tempfile.NamedTemporaryFile(
247
+ delete=False,
248
+ suffix='.png', # Default to PNG
249
+ prefix='ai_generated_'
250
+ ) as temp_file:
251
+ temp_file.write(image_bytes)
252
+ return temp_file.name
253
+
254
+ download_button.click(
255
+ fn=download_image,
256
+ inputs=[image_bytes_state],
257
+ outputs=gr.File(label="Download Generated Image")
258
  )
259
 
260
  return demo