artintel235 commited on
Commit
9fc847d
·
verified ·
1 Parent(s): ca14d7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -35,12 +35,18 @@ if "current_user" not in st.session_state:
35
  st.session_state.current_user = None
36
  if "display_name" not in st.session_state:
37
  st.session_state.display_name = None
 
 
 
 
 
 
38
 
39
  TOKEN = os.getenv("TOKEN0")
40
  API_URL = os.getenv("API_URL")
41
  token_id = 0
42
  tokens_tried = 0
43
- no_of_accounts = 7
44
  model_id = os.getenv("MODEL_ID")
45
 
46
  def send_verification_email(id_token):
@@ -118,6 +124,8 @@ def login_callback():
118
  st.session_state.logged_in = True
119
  st.session_state.current_user = user.uid
120
  st.session_state.display_name = user.display_name # Store the display name
 
 
121
  st.success("Logged in successfully!")
122
  except Exception as e:
123
  st.error(f"Login failed: {e}")
@@ -127,6 +135,8 @@ def logout_callback():
127
  st.session_state.logged_in = False
128
  st.session_state.current_user = None
129
  st.session_state.display_name = None
 
 
130
  st.info("Logged out successfully!")
131
 
132
  # Function to get image from url
@@ -241,6 +251,24 @@ def upload_image_to_storage(image, user_id):
241
  st.error(f"Failed to upload image to cloud storage: {e}")
242
  return None
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  # Registration form
245
  def registration_form():
246
  with st.form("Registration"):
@@ -296,6 +324,31 @@ def main_app():
296
  else:
297
  st.warning("Please enter a prompt to generate an image.")
298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  # Logout button
300
  if st.button("Logout", on_click=logout_callback):
301
  pass
 
35
  st.session_state.current_user = None
36
  if "display_name" not in st.session_state:
37
  st.session_state.display_name = None
38
+ if "images_data" not in st.session_state:
39
+ st.session_state.images_data = []
40
+ if "start_index" not in st.session_state:
41
+ st.session_state.start_index = 0
42
+ if "batch_size" not in st.session_state:
43
+ st.session_state.batch_size = 6
44
 
45
  TOKEN = os.getenv("TOKEN0")
46
  API_URL = os.getenv("API_URL")
47
  token_id = 0
48
  tokens_tried = 0
49
+ no_of_accounts = 11
50
  model_id = os.getenv("MODEL_ID")
51
 
52
  def send_verification_email(id_token):
 
124
  st.session_state.logged_in = True
125
  st.session_state.current_user = user.uid
126
  st.session_state.display_name = user.display_name # Store the display name
127
+ st.session_state.images_data = [] # Reset images data on login
128
+ st.session_state.start_index = 0
129
  st.success("Logged in successfully!")
130
  except Exception as e:
131
  st.error(f"Login failed: {e}")
 
135
  st.session_state.logged_in = False
136
  st.session_state.current_user = None
137
  st.session_state.display_name = None
138
+ st.session_state.images_data = [] # Clear images data on logout
139
+ st.session_state.start_index = 0
140
  st.info("Logged out successfully!")
141
 
142
  # Function to get image from url
 
251
  st.error(f"Failed to upload image to cloud storage: {e}")
252
  return None
253
 
254
+ #Function to load image data from the database
255
+ def load_image_data(user_id, start_index, batch_size):
256
+ try:
257
+ ref = db.reference(f'users/{user_id}/images')
258
+ snapshot = ref.order_by_child('timestamp').limit_to_last(start_index + batch_size).get()
259
+ if snapshot:
260
+ image_list = list(snapshot.items())
261
+ image_list.reverse() # Reverse to show latest first
262
+ new_images = []
263
+ for key, val in image_list[start_index:]:
264
+ new_images.append(val)
265
+ return new_images
266
+ else:
267
+ return []
268
+ except Exception as e:
269
+ st.error(f"Failed to fetch image data from database: {e}")
270
+ return []
271
+
272
  # Registration form
273
  def registration_form():
274
  with st.form("Registration"):
 
324
  else:
325
  st.warning("Please enter a prompt to generate an image.")
326
 
327
+ st.header("Your Generated Images")
328
+ # Load initial batch of images
329
+ if st.session_state.images_data == []:
330
+ st.session_state.images_data = load_image_data(st.session_state.current_user, st.session_state.start_index, st.session_state.batch_size)
331
+
332
+ if st.session_state.images_data:
333
+ for image_data in st.session_state.images_data:
334
+ col1, col2 = st.columns([1,2])
335
+ with col1:
336
+ st.image(image_data['image_url'], width = 150)
337
+ with col2:
338
+ st.write(f"**Prompt:** {image_data['prompt']}")
339
+ st.write(f"**Aspect Ratio:** {image_data['aspect_ratio']}")
340
+ st.write(f"**Realism:** {image_data['realism']}")
341
+ st.markdown("---")
342
+
343
+ # Load more button
344
+ if st.button("Load More Images"):
345
+ st.session_state.start_index += st.session_state.batch_size
346
+ new_images = load_image_data(st.session_state.current_user, st.session_state.start_index, st.session_state.batch_size)
347
+ if new_images:
348
+ st.session_state.images_data.extend(new_images)
349
+ else:
350
+ st.write("No more images available")
351
+
352
  # Logout button
353
  if st.button("Logout", on_click=logout_callback):
354
  pass