Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -344,65 +344,65 @@ def main_app():
|
|
344 |
realism = st.checkbox("Realism", value=False)
|
345 |
|
346 |
if st.button("Generate Image"):
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
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 |
else:
|
396 |
-
st.error("Failed to
|
397 |
else:
|
398 |
-
st.error("Failed to
|
|
|
399 |
else:
|
400 |
-
st.error("
|
401 |
-
|
402 |
-
|
403 |
-
st.error(f"Image generation failed: {image_result}")
|
404 |
-
else:
|
405 |
-
st.warning("Please enter a prompt to generate an image.")
|
406 |
st.header("Your Generated Images")
|
407 |
# Initialize the current window, if it doesn't exist in session state
|
408 |
if "current_window_start" not in st.session_state:
|
|
|
344 |
realism = st.checkbox("Realism", value=False)
|
345 |
|
346 |
if st.button("Generate Image"):
|
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}")
|
404 |
+
else:
|
405 |
+
st.warning("Please enter a prompt to generate an image.")
|
|
|
|
|
|
|
406 |
st.header("Your Generated Images")
|
407 |
# Initialize the current window, if it doesn't exist in session state
|
408 |
if "current_window_start" not in st.session_state:
|