Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
|
|
|
|
|
|
|
|
|
|
399 |
else:
|
400 |
-
|
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}")
|