Update app.py
Browse files
app.py
CHANGED
|
@@ -6,42 +6,50 @@ import numpy as np
|
|
| 6 |
from datetime import datetime, date
|
| 7 |
import os
|
| 8 |
import json
|
|
|
|
| 9 |
|
| 10 |
# File to store session data
|
| 11 |
-
SESSION_FILE = "
|
| 12 |
|
| 13 |
# OpenAI API Key
|
| 14 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
| 16 |
|
| 17 |
-
# Function to load session data
|
| 18 |
-
def
|
| 19 |
if os.path.exists(SESSION_FILE):
|
| 20 |
with open(SESSION_FILE, 'r') as f:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
with open(SESSION_FILE, 'w') as f:
|
| 27 |
-
json.dump(
|
| 28 |
|
| 29 |
-
# Function to check and reset the counter if necessary
|
| 30 |
-
def
|
| 31 |
-
|
|
|
|
| 32 |
today = str(date.today())
|
| 33 |
-
if
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
# Function to increment the counter
|
| 40 |
-
def
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return
|
| 45 |
|
| 46 |
# Function to encode the image
|
| 47 |
def encode_image(image_array):
|
|
@@ -153,8 +161,8 @@ css = """
|
|
| 153 |
}
|
| 154 |
"""
|
| 155 |
|
| 156 |
-
# Initialize
|
| 157 |
-
|
| 158 |
|
| 159 |
with gr.Blocks(css=css) as demo:
|
| 160 |
gr.Markdown("<h1>WordLift Product Description Generation - [FREE]</h1>")
|
|
@@ -196,7 +204,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 196 |
value="Short Formal 📝",
|
| 197 |
elem_classes="gr-box"
|
| 198 |
)
|
| 199 |
-
|
| 200 |
custom_instruction = gr.Textbox(
|
| 201 |
label="Custom Instruction (Only for 'Other')", visible=False, elem_classes="gr-box"
|
| 202 |
)
|
|
@@ -206,6 +213,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 206 |
redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
|
| 207 |
test_counter_display = gr.Markdown(visible=True, label="Test Counter")
|
| 208 |
|
|
|
|
| 209 |
# Toggle visibility of custom instruction based on selected type
|
| 210 |
def toggle_custom_instruction(type_selection):
|
| 211 |
return gr.update(visible=(type_selection == "Other"))
|
|
@@ -216,10 +224,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 216 |
outputs=[custom_instruction],
|
| 217 |
)
|
| 218 |
|
| 219 |
-
# Modified function to handle test limit
|
| 220 |
def handle_submit(image, description_type, custom_instruction):
|
| 221 |
-
|
| 222 |
-
|
|
|
|
| 223 |
if test_counter <= 4:
|
| 224 |
result = generate_product_description(image, description_type, custom_instruction)
|
| 225 |
remaining_tests = 4 - test_counter
|
|
|
|
| 6 |
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"
|
| 13 |
|
| 14 |
# OpenAI API Key
|
| 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)
|
| 39 |
+
user_data = all_user_data[user_id]
|
| 40 |
today = str(date.today())
|
| 41 |
+
if user_data["last_reset_date"] != today:
|
| 42 |
+
user_data["last_reset_date"] = today
|
| 43 |
+
user_data["test_counter"] = 0
|
| 44 |
+
save_user_session_data(all_user_data)
|
| 45 |
+
return user_data["test_counter"]
|
| 46 |
+
|
| 47 |
+
# Function to increment the counter for a specific user
|
| 48 |
+
def increment_user_counter(user_id):
|
| 49 |
+
all_user_data = load_user_session_data(user_id)
|
| 50 |
+
all_user_data[user_id]["test_counter"] += 1
|
| 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):
|
|
|
|
| 161 |
}
|
| 162 |
"""
|
| 163 |
|
| 164 |
+
# Initialize user ID
|
| 165 |
+
user_id = str(uuid.uuid4())
|
| 166 |
|
| 167 |
with gr.Blocks(css=css) as demo:
|
| 168 |
gr.Markdown("<h1>WordLift Product Description Generation - [FREE]</h1>")
|
|
|
|
| 204 |
value="Short Formal 📝",
|
| 205 |
elem_classes="gr-box"
|
| 206 |
)
|
|
|
|
| 207 |
custom_instruction = gr.Textbox(
|
| 208 |
label="Custom Instruction (Only for 'Other')", visible=False, elem_classes="gr-box"
|
| 209 |
)
|
|
|
|
| 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 |
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 |
+
global user_id
|
| 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:
|
| 233 |
result = generate_product_description(image, description_type, custom_instruction)
|
| 234 |
remaining_tests = 4 - test_counter
|