cyberandy commited on
Commit
b04a418
·
verified ·
1 Parent(s): 1b17d04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -21
app.py CHANGED
@@ -52,19 +52,10 @@ def increment_user_counter(user_id):
52
  return all_user_data[user_id]["test_counter"]
53
 
54
  # Function to get or create user ID from cookie
55
- def get_or_create_user_id(request: gr.Request):
56
- if request is None:
57
- return str(uuid.uuid4()), None
58
-
59
- cookies = SimpleCookie()
60
- cookies.load(request.headers.get('cookie', ''))
61
- user_id = cookies.get('user_id')
62
-
63
- if user_id is None:
64
- user_id = str(uuid.uuid4())
65
- return user_id, ('user_id', user_id, {'httponly': True, 'max_age': 365 * 24 * 60 * 60})
66
- else:
67
- return user_id.value, None
68
 
69
  # Function to encode the image
70
  def encode_image(image_array):
@@ -176,10 +167,37 @@ css = """
176
  }
177
  """
178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  # Initialize user ID
180
  user_id = str(uuid.uuid4())
181
 
182
- with gr.Blocks(css=css) as demo:
183
  gr.Markdown("<h1>WordLift Product Description Generation - [FREE]</h1>")
184
  with gr.Tab(label="WordLift Product Description Generation"):
185
  with gr.Row():
@@ -228,6 +246,10 @@ with gr.Blocks(css=css) as demo:
228
  redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
229
  test_counter_display = gr.Markdown(visible=True, label="Test Counter")
230
  user_id_display = gr.Markdown(visible=True, label="User ID")
 
 
 
 
231
 
232
  # Toggle visibility of custom instruction based on selected type
233
  def toggle_custom_instruction(type_selection):
@@ -239,8 +261,8 @@ with gr.Blocks(css=css) as demo:
239
  outputs=[custom_instruction],
240
  )
241
 
242
- def handle_submit(image, description_type, custom_instruction, request: gr.Request):
243
- user_id, cookie = get_or_create_user_id(request)
244
  check_and_reset_user_counter(user_id) # Check and reset counter if it's a new day
245
  test_counter = increment_user_counter(user_id)
246
 
@@ -256,7 +278,8 @@ with gr.Blocks(css=css) as demo:
256
  gr.update(visible=False),
257
  gr.update(interactive=True),
258
  f"You have {remaining_tests} free test(s) remaining today.",
259
- f"User ID: {user_id}"
 
260
  )
261
  else:
262
  redirect_text = f"""
@@ -282,7 +305,8 @@ with gr.Blocks(css=css) as demo:
282
  gr.update(visible=True, value=redirect_text),
283
  gr.update(interactive=False),
284
  "You've used all your free tests for today. We invite you to book a demo for full access!",
285
- f"User ID: {user_id}"
 
286
  )
287
  else:
288
  redirect_text = f"""
@@ -308,15 +332,17 @@ with gr.Blocks(css=css) as demo:
308
  gr.update(visible=True, value=redirect_text),
309
  gr.update(interactive=False),
310
  "All free tests used. We invite you to book a demo for full access!",
311
- f"User ID: {user_id}"
 
312
  )
313
 
314
 
 
315
  # Update the outputs of the submit button click event
316
  submit_btn.click(
317
  handle_submit,
318
- inputs=[input_img, description_type, custom_instruction],
319
- outputs=[output_text, redirect_message, submit_btn, test_counter_display, user_id_display]
320
  )
321
 
322
  # Launch Gradio app
 
52
  return all_user_data[user_id]["test_counter"]
53
 
54
  # Function to get or create user ID from cookie
55
+ def get_or_create_user_id(user_id):
56
+ if not user_id:
57
+ return str(uuid.uuid4())
58
+ return user_id
 
 
 
 
 
 
 
 
 
59
 
60
  # Function to encode the image
61
  def encode_image(image_array):
 
167
  }
168
  """
169
 
170
+ custom_js = """
171
+ function getUserId() {
172
+ let userId = localStorage.getItem('wordlift_user_id');
173
+ if (!userId) {
174
+ userId = generateUUID();
175
+ localStorage.setItem('wordlift_user_id', userId);
176
+ }
177
+ return userId;
178
+ }
179
+
180
+ function generateUUID() {
181
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
182
+ var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
183
+ return v.toString(16);
184
+ });
185
+ }
186
+
187
+ function setUserId(userId) {
188
+ document.getElementById('user-id-input').value = userId;
189
+ }
190
+
191
+ // Set user ID when page loads
192
+ document.addEventListener('DOMContentLoaded', (event) => {
193
+ setUserId(getUserId());
194
+ });
195
+ """
196
+
197
  # Initialize user ID
198
  user_id = str(uuid.uuid4())
199
 
200
+ with gr.Blocks(css=custom_css, js=custom_js) as demo:
201
  gr.Markdown("<h1>WordLift Product Description Generation - [FREE]</h1>")
202
  with gr.Tab(label="WordLift Product Description Generation"):
203
  with gr.Row():
 
246
  redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
247
  test_counter_display = gr.Markdown(visible=True, label="Test Counter")
248
  user_id_display = gr.Markdown(visible=True, label="User ID")
249
+
250
+ # Hidden textbox to store user ID
251
+ user_id_input = gr.Textbox(elem_id="user-id-input", visible=False)
252
+
253
 
254
  # Toggle visibility of custom instruction based on selected type
255
  def toggle_custom_instruction(type_selection):
 
261
  outputs=[custom_instruction],
262
  )
263
 
264
+ def handle_submit(image, description_type, custom_instruction, user_id):
265
+ user_id = get_or_create_user_id(user_id)
266
  check_and_reset_user_counter(user_id) # Check and reset counter if it's a new day
267
  test_counter = increment_user_counter(user_id)
268
 
 
278
  gr.update(visible=False),
279
  gr.update(interactive=True),
280
  f"You have {remaining_tests} free test(s) remaining today.",
281
+ f"User ID: {user_id}",
282
+ user_id # Return user_id to update hidden textbox
283
  )
284
  else:
285
  redirect_text = f"""
 
305
  gr.update(visible=True, value=redirect_text),
306
  gr.update(interactive=False),
307
  "You've used all your free tests for today. We invite you to book a demo for full access!",
308
+ f"User ID: {user_id}",
309
+ user_id # Return user_id to update hidden textbox
310
  )
311
  else:
312
  redirect_text = f"""
 
332
  gr.update(visible=True, value=redirect_text),
333
  gr.update(interactive=False),
334
  "All free tests used. We invite you to book a demo for full access!",
335
+ f"User ID: {user_id}",
336
+ user_id # Return user_id to update hidden textbox
337
  )
338
 
339
 
340
+
341
  # Update the outputs of the submit button click event
342
  submit_btn.click(
343
  handle_submit,
344
+ inputs=[input_img, description_type, custom_instruction, user_id_input],
345
+ outputs=[output_text, redirect_message, submit_btn, test_counter_display, user_id_display, user_id_input]
346
  )
347
 
348
  # Launch Gradio app