Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,7 +43,6 @@ if "current_window_start" not in st.session_state:
|
|
43 |
if "selected_image" not in st.session_state:
|
44 |
st.session_state.selected_image = None
|
45 |
|
46 |
-
|
47 |
TOKEN = os.getenv("TOKEN0")
|
48 |
API_URL = os.getenv("API_URL")
|
49 |
token_id = 0
|
@@ -58,10 +57,10 @@ def send_verification_email(id_token):
|
|
58 |
'requestType': 'VERIFY_EMAIL',
|
59 |
'idToken': id_token
|
60 |
}
|
61 |
-
|
62 |
response = requests.post(url, headers=headers, json=data)
|
63 |
result = response.json()
|
64 |
-
|
65 |
if 'error' in result:
|
66 |
return {'status': 'error', 'message': result['error']['message']}
|
67 |
else:
|
@@ -75,10 +74,10 @@ def register_callback():
|
|
75 |
try:
|
76 |
# Step 1: Create a new user in Firebase
|
77 |
user = auth.create_user(email=email, password=password)
|
78 |
-
|
79 |
# Step 2: Update the user profile with the display name
|
80 |
auth.update_user(user.uid, display_name=display_name)
|
81 |
-
|
82 |
st.success("Registration successful! Sending verification email...")
|
83 |
|
84 |
# Step 3: Sign in the user programmatically to get the id_token
|
@@ -150,7 +149,7 @@ def login_callback():
|
|
150 |
st.session_state.display_name = user.display_name # Store the display name
|
151 |
st.success("Logged in successfully!")
|
152 |
return
|
153 |
-
|
154 |
raise Exception("User not found with provided credentials.") # if not found, raise exception.
|
155 |
except Exception as e:
|
156 |
st.error(f"Login failed: {e}") # if any error, display this message.
|
@@ -191,7 +190,7 @@ def generate_image(prompt, aspect_ratio, realism):
|
|
191 |
global no_of_accounts
|
192 |
global model_id
|
193 |
payload = {
|
194 |
-
"id": model_id,
|
195 |
"inputs": [prompt, aspect_ratio, str(realism).lower()],
|
196 |
}
|
197 |
headers = {"Authorization": f"Bearer {TOKEN}"}
|
@@ -258,7 +257,6 @@ def store_image_data_in_db(user_id, prompt, aspect_ratio, realism, image_url, th
|
|
258 |
except Exception as e:
|
259 |
st.error(f"Failed to save image data: {e}")
|
260 |
|
261 |
-
#Function to upload image to cloud storage
|
262 |
#Function to upload image to cloud storage
|
263 |
def upload_image_to_storage(image, user_id, is_thumbnail = False):
|
264 |
try:
|
@@ -291,7 +289,7 @@ def load_image_data(user_id, start_index, batch_size):
|
|
291 |
if snapshot:
|
292 |
image_list = list(snapshot.items())
|
293 |
image_list.reverse() # Reverse to show latest first
|
294 |
-
|
295 |
new_images = []
|
296 |
for key, val in image_list[start_index:]:
|
297 |
new_images.append(val)
|
@@ -339,8 +337,8 @@ def main_app():
|
|
339 |
# Input fields
|
340 |
prompt = st.text_input("Prompt", key="image_prompt", placeholder="Describe the image you want to generate")
|
341 |
aspect_ratio = st.radio(
|
342 |
-
"Aspect Ratio",
|
343 |
-
options=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"],
|
344 |
index=5
|
345 |
)
|
346 |
realism = st.checkbox("Realism", value=False)
|
@@ -368,7 +366,7 @@ def main_app():
|
|
368 |
store_image_data_in_db(st.session_state.current_user, prompt, aspect_ratio, realism, cloud_storage_url, thumbnail_url)
|
369 |
st.success("Image stored to database successfully!")
|
370 |
with st.container(border=True):
|
371 |
-
st.image(
|
372 |
st.write(f"**Prompt:** {prompt}")
|
373 |
st.write(f"**Aspect Ratio:** {aspect_ratio}")
|
374 |
st.write(f"**Realism:** {realism}")
|
@@ -394,10 +392,10 @@ def main_app():
|
|
394 |
|
395 |
if "window_size" not in st.session_state:
|
396 |
st.session_state.window_size = 5 # The number of images to display at a time
|
397 |
-
|
398 |
if "selected_image" not in st.session_state:
|
399 |
st.session_state.selected_image = None
|
400 |
-
|
401 |
# Create left and right arrow buttons
|
402 |
col_left, col_center, col_right = st.columns([1,8,1])
|
403 |
|
@@ -411,14 +409,14 @@ def main_app():
|
|
411 |
|
412 |
# Dynamically load images for the window
|
413 |
all_images = load_image_data(st.session_state.current_user, 0, 1000) # load all images
|
414 |
-
|
415 |
if all_images:
|
416 |
num_images = len(all_images)
|
417 |
|
418 |
# Calculate the range for images to display
|
419 |
start_index = st.session_state.current_window_start
|
420 |
end_index = min(start_index + st.session_state.window_size, num_images)
|
421 |
-
|
422 |
images_for_window = all_images[start_index:end_index]
|
423 |
|
424 |
# Setup columns for horizontal slider layout
|
@@ -441,17 +439,16 @@ def main_app():
|
|
441 |
else:
|
442 |
st.write("No image generated yet!")
|
443 |
|
444 |
-
|
445 |
# Display modal if an image is selected
|
446 |
if st.session_state.selected_image:
|
447 |
-
with st.container(border=True):
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
|
456 |
if st.button("Close"):
|
457 |
st.session_state.selected_image = None # close the modal when "close" is clicked
|
|
|
43 |
if "selected_image" not in st.session_state:
|
44 |
st.session_state.selected_image = None
|
45 |
|
|
|
46 |
TOKEN = os.getenv("TOKEN0")
|
47 |
API_URL = os.getenv("API_URL")
|
48 |
token_id = 0
|
|
|
57 |
'requestType': 'VERIFY_EMAIL',
|
58 |
'idToken': id_token
|
59 |
}
|
60 |
+
|
61 |
response = requests.post(url, headers=headers, json=data)
|
62 |
result = response.json()
|
63 |
+
|
64 |
if 'error' in result:
|
65 |
return {'status': 'error', 'message': result['error']['message']}
|
66 |
else:
|
|
|
74 |
try:
|
75 |
# Step 1: Create a new user in Firebase
|
76 |
user = auth.create_user(email=email, password=password)
|
77 |
+
|
78 |
# Step 2: Update the user profile with the display name
|
79 |
auth.update_user(user.uid, display_name=display_name)
|
80 |
+
|
81 |
st.success("Registration successful! Sending verification email...")
|
82 |
|
83 |
# Step 3: Sign in the user programmatically to get the id_token
|
|
|
149 |
st.session_state.display_name = user.display_name # Store the display name
|
150 |
st.success("Logged in successfully!")
|
151 |
return
|
152 |
+
|
153 |
raise Exception("User not found with provided credentials.") # if not found, raise exception.
|
154 |
except Exception as e:
|
155 |
st.error(f"Login failed: {e}") # if any error, display this message.
|
|
|
190 |
global no_of_accounts
|
191 |
global model_id
|
192 |
payload = {
|
193 |
+
"id": model_id,
|
194 |
"inputs": [prompt, aspect_ratio, str(realism).lower()],
|
195 |
}
|
196 |
headers = {"Authorization": f"Bearer {TOKEN}"}
|
|
|
257 |
except Exception as e:
|
258 |
st.error(f"Failed to save image data: {e}")
|
259 |
|
|
|
260 |
#Function to upload image to cloud storage
|
261 |
def upload_image_to_storage(image, user_id, is_thumbnail = False):
|
262 |
try:
|
|
|
289 |
if snapshot:
|
290 |
image_list = list(snapshot.items())
|
291 |
image_list.reverse() # Reverse to show latest first
|
292 |
+
|
293 |
new_images = []
|
294 |
for key, val in image_list[start_index:]:
|
295 |
new_images.append(val)
|
|
|
337 |
# Input fields
|
338 |
prompt = st.text_input("Prompt", key="image_prompt", placeholder="Describe the image you want to generate")
|
339 |
aspect_ratio = st.radio(
|
340 |
+
"Aspect Ratio",
|
341 |
+
options=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"],
|
342 |
index=5
|
343 |
)
|
344 |
realism = st.checkbox("Realism", value=False)
|
|
|
366 |
store_image_data_in_db(st.session_state.current_user, prompt, aspect_ratio, realism, cloud_storage_url, thumbnail_url)
|
367 |
st.success("Image stored to database successfully!")
|
368 |
with st.container(border=True):
|
369 |
+
st.image(cloud_storage_url, use_column_width=True) # Display high-res image from URL
|
370 |
st.write(f"**Prompt:** {prompt}")
|
371 |
st.write(f"**Aspect Ratio:** {aspect_ratio}")
|
372 |
st.write(f"**Realism:** {realism}")
|
|
|
392 |
|
393 |
if "window_size" not in st.session_state:
|
394 |
st.session_state.window_size = 5 # The number of images to display at a time
|
395 |
+
|
396 |
if "selected_image" not in st.session_state:
|
397 |
st.session_state.selected_image = None
|
398 |
+
|
399 |
# Create left and right arrow buttons
|
400 |
col_left, col_center, col_right = st.columns([1,8,1])
|
401 |
|
|
|
409 |
|
410 |
# Dynamically load images for the window
|
411 |
all_images = load_image_data(st.session_state.current_user, 0, 1000) # load all images
|
412 |
+
|
413 |
if all_images:
|
414 |
num_images = len(all_images)
|
415 |
|
416 |
# Calculate the range for images to display
|
417 |
start_index = st.session_state.current_window_start
|
418 |
end_index = min(start_index + st.session_state.window_size, num_images)
|
419 |
+
|
420 |
images_for_window = all_images[start_index:end_index]
|
421 |
|
422 |
# Setup columns for horizontal slider layout
|
|
|
439 |
else:
|
440 |
st.write("No image generated yet!")
|
441 |
|
|
|
442 |
# Display modal if an image is selected
|
443 |
if st.session_state.selected_image:
|
444 |
+
with st.container(border = True):
|
445 |
+
st.image(st.session_state.selected_image['image_url'], use_column_width=True)
|
446 |
+
st.write(f"**Prompt:** {st.session_state.selected_image['prompt']}")
|
447 |
+
st.write(f"**Aspect Ratio:** {st.session_state.selected_image['aspect_ratio']}")
|
448 |
+
st.write(f"**Realism:** {st.session_state.selected_image['realism']}")
|
449 |
+
download_path = download_image(st.session_state.selected_image['image_url'])
|
450 |
+
if download_path:
|
451 |
+
st.download_button(label="Download Image", data = open(download_path, "rb"), file_name = f"image.png", key=f"download_overlay_{uuid.uuid4()}")
|
452 |
|
453 |
if st.button("Close"):
|
454 |
st.session_state.selected_image = None # close the modal when "close" is clicked
|