Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from datetime import datetime, date
|
|
7 |
import os
|
8 |
import json
|
9 |
import uuid
|
|
|
10 |
|
11 |
# File to store session data
|
12 |
SESSION_FILE = "user_session_data.json"
|
@@ -15,24 +16,6 @@ SESSION_FILE = "user_session_data.json"
|
|
15 |
api_key = os.getenv("OPENAI_API_KEY")
|
16 |
|
17 |
|
18 |
-
# Function to load user session data
|
19 |
-
def load_user_session_data(user_id):
|
20 |
-
if os.path.exists(SESSION_FILE):
|
21 |
-
with open(SESSION_FILE, 'r') as f:
|
22 |
-
all_user_data = json.load(f)
|
23 |
-
else:
|
24 |
-
all_user_data = {}
|
25 |
-
|
26 |
-
if user_id not in all_user_data:
|
27 |
-
all_user_data[user_id] = {"last_reset_date": str(date.today()), "test_counter": 0}
|
28 |
-
|
29 |
-
return all_user_data
|
30 |
-
|
31 |
-
# Function to save user session data
|
32 |
-
def save_user_session_data(all_user_data):
|
33 |
-
with open(SESSION_FILE, 'w') as f:
|
34 |
-
json.dump(all_user_data, f)
|
35 |
-
|
36 |
# Function to check and reset the counter if necessary for a specific user
|
37 |
def check_and_reset_user_counter(user_id):
|
38 |
all_user_data = load_user_session_data(user_id)
|
@@ -51,6 +34,25 @@ def increment_user_counter(user_id):
|
|
51 |
save_user_session_data(all_user_data)
|
52 |
return all_user_data[user_id]["test_counter"]
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# Function to encode the image
|
55 |
def encode_image(image_array):
|
56 |
# Convert numpy array to an image file and encode it in base64
|
@@ -213,7 +215,6 @@ with gr.Blocks(css=css) as demo:
|
|
213 |
redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
|
214 |
test_counter_display = gr.Markdown(visible=True, label="Test Counter")
|
215 |
|
216 |
-
|
217 |
# Toggle visibility of custom instruction based on selected type
|
218 |
def toggle_custom_instruction(type_selection):
|
219 |
return gr.update(visible=(type_selection == "Other"))
|
@@ -224,9 +225,9 @@ with gr.Blocks(css=css) as demo:
|
|
224 |
outputs=[custom_instruction],
|
225 |
)
|
226 |
|
227 |
-
# Modified function to handle test limit with improved messaging
|
228 |
-
def handle_submit(image, description_type, custom_instruction):
|
229 |
-
|
230 |
check_and_reset_user_counter(user_id) # Check and reset counter if it's a new day
|
231 |
test_counter = increment_user_counter(user_id)
|
232 |
if test_counter <= 4:
|
@@ -238,6 +239,7 @@ with gr.Blocks(css=css) as demo:
|
|
238 |
gr.update(visible=False),
|
239 |
gr.update(interactive=True),
|
240 |
f"You have {remaining_tests} free test(s) remaining today.",
|
|
|
241 |
)
|
242 |
else:
|
243 |
redirect_text = """
|
@@ -263,6 +265,7 @@ with gr.Blocks(css=css) as demo:
|
|
263 |
gr.update(visible=True, value=redirect_text),
|
264 |
gr.update(interactive=False),
|
265 |
"You've used all your free tests for today. We invite you to book a demo for full access!",
|
|
|
266 |
)
|
267 |
else:
|
268 |
redirect_text = """
|
@@ -288,6 +291,7 @@ with gr.Blocks(css=css) as demo:
|
|
288 |
gr.update(visible=True, value=redirect_text),
|
289 |
gr.update(interactive=False),
|
290 |
"All free tests used. We invite you to book a demo for full access!",
|
|
|
291 |
)
|
292 |
|
293 |
submit_btn.click(
|
|
|
7 |
import os
|
8 |
import json
|
9 |
import uuid
|
10 |
+
from http.cookies import SimpleCookie
|
11 |
|
12 |
# File to store session data
|
13 |
SESSION_FILE = "user_session_data.json"
|
|
|
16 |
api_key = os.getenv("OPENAI_API_KEY")
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Function to check and reset the counter if necessary for a specific user
|
20 |
def check_and_reset_user_counter(user_id):
|
21 |
all_user_data = load_user_session_data(user_id)
|
|
|
34 |
save_user_session_data(all_user_data)
|
35 |
return all_user_data[user_id]["test_counter"]
|
36 |
|
37 |
+
# Function to get or create user ID from cookie
|
38 |
+
def get_or_create_user_id(request: gr.Request):
|
39 |
+
if request is None:
|
40 |
+
return str(uuid.uuid4())
|
41 |
+
|
42 |
+
cookies = SimpleCookie()
|
43 |
+
cookies.load(request.headers.get('cookie', ''))
|
44 |
+
user_id = cookies.get('user_id')
|
45 |
+
|
46 |
+
if user_id is None:
|
47 |
+
user_id = str(uuid.uuid4())
|
48 |
+
cookies['user_id'] = user_id
|
49 |
+
cookies['user_id']['httponly'] = True
|
50 |
+
cookies['user_id']['max-age'] = 365 * 24 * 60 * 60 # 1 year
|
51 |
+
else:
|
52 |
+
user_id = user_id.value
|
53 |
+
|
54 |
+
return user_id, cookies.output(header='', sep=';')
|
55 |
+
|
56 |
# Function to encode the image
|
57 |
def encode_image(image_array):
|
58 |
# Convert numpy array to an image file and encode it in base64
|
|
|
215 |
redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
|
216 |
test_counter_display = gr.Markdown(visible=True, label="Test Counter")
|
217 |
|
|
|
218 |
# Toggle visibility of custom instruction based on selected type
|
219 |
def toggle_custom_instruction(type_selection):
|
220 |
return gr.update(visible=(type_selection == "Other"))
|
|
|
225 |
outputs=[custom_instruction],
|
226 |
)
|
227 |
|
228 |
+
# Modified function to handle test limit with improved messaging and cookie-based user sessions
|
229 |
+
def handle_submit(image, description_type, custom_instruction, request: gr.Request):
|
230 |
+
user_id, cookie = get_or_create_user_id(request)
|
231 |
check_and_reset_user_counter(user_id) # Check and reset counter if it's a new day
|
232 |
test_counter = increment_user_counter(user_id)
|
233 |
if test_counter <= 4:
|
|
|
239 |
gr.update(visible=False),
|
240 |
gr.update(interactive=True),
|
241 |
f"You have {remaining_tests} free test(s) remaining today.",
|
242 |
+
cookie,
|
243 |
)
|
244 |
else:
|
245 |
redirect_text = """
|
|
|
265 |
gr.update(visible=True, value=redirect_text),
|
266 |
gr.update(interactive=False),
|
267 |
"You've used all your free tests for today. We invite you to book a demo for full access!",
|
268 |
+
cookie,
|
269 |
)
|
270 |
else:
|
271 |
redirect_text = """
|
|
|
291 |
gr.update(visible=True, value=redirect_text),
|
292 |
gr.update(interactive=False),
|
293 |
"All free tests used. We invite you to book a demo for full access!",
|
294 |
+
cookie,
|
295 |
)
|
296 |
|
297 |
submit_btn.click(
|