Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,44 @@ from PIL import Image
|
|
5 |
import numpy as np
|
6 |
from datetime import datetime
|
7 |
import os
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# OpenAI API Key
|
10 |
api_key = os.getenv("OPENAI_API_KEY")
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Function to encode the image
|
14 |
def encode_image(image_array):
|
15 |
# Convert numpy array to an image file and encode it in base64
|
@@ -168,7 +201,6 @@ with gr.Blocks(css=css) as demo:
|
|
168 |
label="Custom Instruction (Only for 'Other')", visible=False, elem_classes="gr-box"
|
169 |
)
|
170 |
submit_btn = gr.Button(value="Submit", elem_classes="gr-box")
|
171 |
-
|
172 |
with gr.Column():
|
173 |
output_text = gr.Markdown(label="Output Text", show_copy_button=True, elem_classes="output-text")
|
174 |
redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
|
@@ -186,9 +218,9 @@ with gr.Blocks(css=css) as demo:
|
|
186 |
|
187 |
# Modified function to handle test limit
|
188 |
def handle_submit(image, description_type, custom_instruction):
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
result = generate_product_description(image, description_type, custom_instruction)
|
193 |
remaining_tests = 4 - test_counter
|
194 |
if remaining_tests > 0:
|
@@ -196,27 +228,29 @@ with gr.Blocks(css=css) as demo:
|
|
196 |
result,
|
197 |
gr.update(visible=False),
|
198 |
gr.update(interactive=True),
|
199 |
-
f"You have {remaining_tests} test(s) remaining.",
|
200 |
)
|
201 |
else:
|
202 |
redirect_text = """
|
203 |
-
You've reached the limit of 4 free tests. To continue using our product description generation tool,
|
204 |
please book a demo of our solution for content generation with our experts.
|
205 |
|
206 |
[Book a Demo](https://wordlift.io/book-a-demo/)
|
|
|
|
|
207 |
"""
|
208 |
return (
|
209 |
result,
|
210 |
gr.update(visible=True, value=redirect_text),
|
211 |
gr.update(interactive=False),
|
212 |
-
"You've used all your free tests.",
|
213 |
)
|
214 |
else:
|
215 |
return (
|
216 |
"",
|
217 |
gr.update(visible=True),
|
218 |
gr.update(interactive=False),
|
219 |
-
"You've used all your free tests.",
|
220 |
)
|
221 |
|
222 |
submit_btn.click(
|
@@ -225,7 +259,6 @@ with gr.Blocks(css=css) as demo:
|
|
225 |
outputs=[output_text, redirect_message, submit_btn, test_counter_display],
|
226 |
)
|
227 |
|
228 |
-
|
229 |
# Launch Gradio app
|
230 |
demo.queue(api_open=False)
|
231 |
demo.launch(debug=True)
|
|
|
5 |
import numpy as np
|
6 |
from datetime import datetime
|
7 |
import os
|
8 |
+
import json
|
9 |
+
|
10 |
+
# File to store session data
|
11 |
+
SESSION_FILE = "session_data.json"
|
12 |
|
13 |
# OpenAI API Key
|
14 |
api_key = os.getenv("OPENAI_API_KEY")
|
15 |
|
16 |
|
17 |
+
# Function to load session data
|
18 |
+
def load_session_data():
|
19 |
+
if os.path.exists(SESSION_FILE):
|
20 |
+
with open(SESSION_FILE, 'r') as f:
|
21 |
+
return json.load(f)
|
22 |
+
return {"last_reset_date": str(date.today()), "test_counter": 0}
|
23 |
+
|
24 |
+
# Function to save session data
|
25 |
+
def save_session_data(data):
|
26 |
+
with open(SESSION_FILE, 'w') as f:
|
27 |
+
json.dump(data, f)
|
28 |
+
|
29 |
+
# Function to check and reset the counter if necessary
|
30 |
+
def check_and_reset_counter():
|
31 |
+
session_data = load_session_data()
|
32 |
+
today = str(date.today())
|
33 |
+
if session_data["last_reset_date"] != today:
|
34 |
+
session_data["last_reset_date"] = today
|
35 |
+
session_data["test_counter"] = 0
|
36 |
+
save_session_data(session_data)
|
37 |
+
return session_data["test_counter"]
|
38 |
+
|
39 |
+
# Function to increment the counter
|
40 |
+
def increment_counter():
|
41 |
+
session_data = load_session_data()
|
42 |
+
session_data["test_counter"] += 1
|
43 |
+
save_session_data(session_data)
|
44 |
+
return session_data["test_counter"]
|
45 |
+
|
46 |
# Function to encode the image
|
47 |
def encode_image(image_array):
|
48 |
# Convert numpy array to an image file and encode it in base64
|
|
|
201 |
label="Custom Instruction (Only for 'Other')", visible=False, elem_classes="gr-box"
|
202 |
)
|
203 |
submit_btn = gr.Button(value="Submit", elem_classes="gr-box")
|
|
|
204 |
with gr.Column():
|
205 |
output_text = gr.Markdown(label="Output Text", show_copy_button=True, elem_classes="output-text")
|
206 |
redirect_message = gr.Markdown(visible=False, elem_classes="redirect-message")
|
|
|
218 |
|
219 |
# Modified function to handle test limit
|
220 |
def handle_submit(image, description_type, custom_instruction):
|
221 |
+
check_and_reset_counter() # Check and reset counter if it's a new day
|
222 |
+
test_counter = increment_counter()
|
223 |
+
if test_counter <= 4:
|
224 |
result = generate_product_description(image, description_type, custom_instruction)
|
225 |
remaining_tests = 4 - test_counter
|
226 |
if remaining_tests > 0:
|
|
|
228 |
result,
|
229 |
gr.update(visible=False),
|
230 |
gr.update(interactive=True),
|
231 |
+
f"You have {remaining_tests} test(s) remaining today.",
|
232 |
)
|
233 |
else:
|
234 |
redirect_text = """
|
235 |
+
You've reached the limit of 4 free tests for today. To continue using our product description generation tool,
|
236 |
please book a demo of our solution for content generation with our experts.
|
237 |
|
238 |
[Book a Demo](https://wordlift.io/book-a-demo/)
|
239 |
+
|
240 |
+
The test counter will reset tomorrow, allowing you to try 4 more tests.
|
241 |
"""
|
242 |
return (
|
243 |
result,
|
244 |
gr.update(visible=True, value=redirect_text),
|
245 |
gr.update(interactive=False),
|
246 |
+
"You've used all your free tests for today.",
|
247 |
)
|
248 |
else:
|
249 |
return (
|
250 |
"",
|
251 |
gr.update(visible=True),
|
252 |
gr.update(interactive=False),
|
253 |
+
"You've used all your free tests for today.",
|
254 |
)
|
255 |
|
256 |
submit_btn.click(
|
|
|
259 |
outputs=[output_text, redirect_message, submit_btn, test_counter_display],
|
260 |
)
|
261 |
|
|
|
262 |
# Launch Gradio app
|
263 |
demo.queue(api_open=False)
|
264 |
demo.launch(debug=True)
|