artintel235 commited on
Commit
c62b281
·
verified ·
1 Parent(s): f866df7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -49
app.py CHANGED
@@ -347,57 +347,60 @@ def main_app():
347
  if prompt:
348
  with st.spinner("Generating Image..."):
349
  image_result = generate_image(prompt, aspect_ratio, realism)
350
- if isinstance(image_result, tuple) and len(image_result) == 2 and image_result[0] is not None:
351
- image, image_url = image_result
352
 
353
- # Define the boundary size
354
- preview_size = 400
355
-
356
- # Get original image dimensions
357
- original_width, original_height = image.size
358
-
359
- # Calculate scaling factor to fit within the boundary
360
- width_ratio = preview_size / original_width
361
- height_ratio = preview_size / original_height
362
-
363
- scaling_factor = min(width_ratio, height_ratio)
364
-
365
- # Calculate new dimensions
366
- new_width = int(original_width * scaling_factor)
367
- new_height = int(original_height * scaling_factor)
368
-
369
- # Resize the image
370
- resized_image = image.resize((new_width, new_height), Image.LANCZOS)
371
-
372
- # Upload the high-resolution image
373
- cloud_storage_url = upload_image_to_storage(image, st.session_state.current_user, is_thumbnail=False)
374
-
375
- if cloud_storage_url:
376
- # Create thumbnail from the high-resolution image
377
- thumbnail = create_thumbnail(image)
378
-
379
- if thumbnail:
380
- # Upload thumbnail to cloud storage and store url
381
- thumbnail_url = upload_image_to_storage(thumbnail, st.session_state.current_user, is_thumbnail=True)
382
-
383
- if thumbnail_url:
384
- # Store image data in database
385
- store_image_data_in_db(st.session_state.current_user, prompt, aspect_ratio, realism, cloud_storage_url, thumbnail_url)
386
- st.success("Image stored to database successfully!")
387
- with st.container(border=True):
388
- st.image(resized_image, use_column_width=False) # Display the resized image
389
- st.write(f"**Prompt:** {prompt}")
390
- st.write(f"**Aspect Ratio:** {aspect_ratio}")
391
- st.write(f"**Realism:** {realism}")
392
- download_path = download_image(image_url)
393
- if download_path:
394
- st.download_button(label="Download Image", data = open(download_path, "rb"), file_name = f"image.png", key=f"download_high_res_{uuid.uuid4()}")
395
- else:
396
- st.error("Failed to upload thumbnail to cloud storage.")
397
- else:
398
- st.error("Failed to create thumbnail")
 
 
 
 
 
399
  else:
400
- st.error("Failed to upload image to cloud storage.")
401
 
402
  else:
403
  st.error(f"Image generation failed: {image_result}")
 
347
  if prompt:
348
  with st.spinner("Generating Image..."):
349
  image_result = generate_image(prompt, aspect_ratio, realism)
 
 
350
 
351
+ if isinstance(image_result, tuple) and len(image_result) == 2:
352
+ image, image_url = image_result
353
+ if isinstance(image, Image.Image):
354
+ # Define the boundary size
355
+ preview_size = 400
356
+
357
+ # Get original image dimensions
358
+ original_width, original_height = image.size
359
+
360
+ # Calculate scaling factor to fit within the boundary
361
+ width_ratio = preview_size / original_width
362
+ height_ratio = preview_size / original_height
363
+
364
+ scaling_factor = min(width_ratio, height_ratio)
365
+
366
+ # Calculate new dimensions
367
+ new_width = int(original_width * scaling_factor)
368
+ new_height = int(original_height * scaling_factor)
369
+
370
+ # Resize the image
371
+ resized_image = image.resize((new_width, new_height), Image.LANCZOS)
372
+
373
+ # Upload the high-resolution image
374
+ cloud_storage_url = upload_image_to_storage(image, st.session_state.current_user, is_thumbnail=False)
375
+
376
+ if cloud_storage_url:
377
+ # Create thumbnail from the high-resolution image
378
+ thumbnail = create_thumbnail(image)
379
+
380
+ if thumbnail:
381
+ # Upload thumbnail to cloud storage and store url
382
+ thumbnail_url = upload_image_to_storage(thumbnail, st.session_state.current_user, is_thumbnail=True)
383
+
384
+ if thumbnail_url:
385
+ # Store image data in database
386
+ store_image_data_in_db(st.session_state.current_user, prompt, aspect_ratio, realism, cloud_storage_url, thumbnail_url)
387
+ st.success("Image stored to database successfully!")
388
+ with st.container(border=True):
389
+ st.image(resized_image, use_column_width=False) # Display the resized image
390
+ st.write(f"**Prompt:** {prompt}")
391
+ st.write(f"**Aspect Ratio:** {aspect_ratio}")
392
+ st.write(f"**Realism:** {realism}")
393
+ download_path = download_image(image_url)
394
+ if download_path:
395
+ st.download_button(label="Download Image", data = open(download_path, "rb"), file_name = f"image.png", key=f"download_high_res_{uuid.uuid4()}")
396
+ else:
397
+ st.error("Failed to upload thumbnail to cloud storage.")
398
+ else:
399
+ st.error("Failed to create thumbnail")
400
+ else:
401
+ st.error("Failed to upload image to cloud storage.")
402
  else:
403
+ st.error(f"Image generation failed: {image}")
404
 
405
  else:
406
  st.error(f"Image generation failed: {image_result}")