Update app.py
Browse files
app.py
CHANGED
|
@@ -56,24 +56,50 @@ def get_or_create_user_id(user_id):
|
|
| 56 |
if not user_id:
|
| 57 |
return str(uuid.uuid4())
|
| 58 |
return user_id
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
def encode_image(image_array):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Function to generate product description using OpenAI API
|
| 74 |
def generate_product_description(image, description_type, custom_instruction=None):
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
|
| 79 |
|
|
@@ -234,80 +260,100 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 234 |
outputs=[custom_instruction],
|
| 235 |
)
|
| 236 |
|
| 237 |
-
# Modify the handle_submit function
|
| 238 |
def handle_submit(image, description_type, custom_instruction, user_id):
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
test_counter = increment_user_counter(user_id)
|
| 244 |
|
| 245 |
-
|
| 246 |
-
demo_link = "https://wordlift.io/book-a-demo/?utm_source=free-app&utm_medium=app&utm_campaign=product-description-generator"
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
else:
|
| 261 |
redirect_text = f"""
|
| 262 |
-
Thank you for
|
| 263 |
-
|
| 264 |
-
You've used
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
[Book a Personalized Demo with Our Experts]({demo_link})
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
•
|
| 272 |
-
•
|
| 273 |
-
•
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
(
|
| 278 |
"""
|
| 279 |
return (
|
| 280 |
-
|
| 281 |
gr.update(visible=True, value=redirect_text),
|
| 282 |
gr.update(interactive=False),
|
| 283 |
-
"
|
| 284 |
f"User ID: {user_id}",
|
| 285 |
user_id
|
| 286 |
)
|
| 287 |
-
|
| 288 |
-
redirect_text = f"""
|
| 289 |
-
Thank you for your interest in our product description generation tool!
|
| 290 |
-
|
| 291 |
-
You've already used your 4 free tests for today. We hope they've given you a glimpse of how our AI can enhance your content creation.
|
| 292 |
-
|
| 293 |
-
Ready to explore the full potential of our solution?
|
| 294 |
-
|
| 295 |
-
[Book a Personalized Demo with Our Experts]({demo_link})
|
| 296 |
-
|
| 297 |
-
In this demo, you'll discover:<br>
|
| 298 |
-
• How our AI can be customized for your specific needs<br>
|
| 299 |
-
• Advanced features for scaling your content production using a [Product Knowledge Graph](https://wordlift.io/blog/en/product-knowledge-graph/)<br>
|
| 300 |
-
• ROI projections based on your current content workflow<br>
|
| 301 |
-
|
| 302 |
-
Let's discuss how we can help you achieve your content goals!
|
| 303 |
-
|
| 304 |
-
(Remember, your free tests will reset tomorrow if you need more time to evaluate.)
|
| 305 |
-
"""
|
| 306 |
return (
|
| 307 |
-
"",
|
| 308 |
-
gr.update(visible=
|
| 309 |
-
gr.update(interactive=
|
| 310 |
-
"
|
| 311 |
f"User ID: {user_id}",
|
| 312 |
user_id
|
| 313 |
)
|
|
|
|
| 56 |
if not user_id:
|
| 57 |
return str(uuid.uuid4())
|
| 58 |
return user_id
|
| 59 |
+
|
| 60 |
+
# Modify the encode_image function to handle AVIF files
|
| 61 |
def encode_image(image_array):
|
| 62 |
+
try:
|
| 63 |
+
# Convert numpy array to PIL Image
|
| 64 |
+
img = Image.fromarray(np.uint8(image_array))
|
| 65 |
+
|
| 66 |
+
# Create a temporary file path
|
| 67 |
+
img_buffer = os.path.join(
|
| 68 |
+
"/tmp", f"temp_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Convert to RGB mode if the image is in a different mode
|
| 72 |
+
if img.mode != 'RGB':
|
| 73 |
+
img = img.convert('RGB')
|
| 74 |
+
|
| 75 |
+
# Save as JPEG with error handling
|
| 76 |
+
try:
|
| 77 |
+
img.save(img_buffer, format="JPEG", quality=95)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error saving image: {e}")
|
| 80 |
+
# If saving fails, try to convert to RGB first
|
| 81 |
+
img = img.convert('RGB')
|
| 82 |
+
img.save(img_buffer, format="JPEG", quality=95)
|
| 83 |
|
| 84 |
+
# Read and encode the saved image
|
| 85 |
+
with open(img_buffer, "rb") as image_file:
|
| 86 |
+
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
|
| 87 |
+
|
| 88 |
+
# Clean up temporary file
|
| 89 |
+
if os.path.exists(img_buffer):
|
| 90 |
+
os.remove(img_buffer)
|
| 91 |
+
|
| 92 |
+
return encoded_image
|
| 93 |
+
except Exception as e:
|
| 94 |
+
raise ValueError(f"Error processing image: {str(e)}")
|
| 95 |
|
| 96 |
# Function to generate product description using OpenAI API
|
| 97 |
def generate_product_description(image, description_type, custom_instruction=None):
|
| 98 |
+
try:
|
| 99 |
+
# Encode the uploaded image
|
| 100 |
+
base64_image = encode_image(image)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
raise gr.Error(f"Error processing image: {str(e)}. Please try uploading a different image format.")
|
| 103 |
|
| 104 |
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
|
| 105 |
|
|
|
|
| 260 |
outputs=[custom_instruction],
|
| 261 |
)
|
| 262 |
|
|
|
|
| 263 |
def handle_submit(image, description_type, custom_instruction, user_id):
|
| 264 |
+
try:
|
| 265 |
+
if not user_id:
|
| 266 |
+
user_id = str(uuid.uuid4())
|
| 267 |
+
|
| 268 |
+
check_and_reset_user_counter(user_id)
|
| 269 |
+
test_counter = increment_user_counter(user_id)
|
| 270 |
+
|
| 271 |
+
demo_link = "https://wordlift.io/book-a-demo/?utm_source=free-app&utm_medium=app&utm_campaign=product-description-generator"
|
| 272 |
+
|
| 273 |
+
if test_counter <= 4:
|
| 274 |
+
try:
|
| 275 |
+
result = generate_product_description(image, description_type, custom_instruction)
|
| 276 |
+
except gr.Error as e:
|
| 277 |
+
return (
|
| 278 |
+
str(e),
|
| 279 |
+
gr.update(visible=False),
|
| 280 |
+
gr.update(interactive=True),
|
| 281 |
+
f"Error occurred. You still have {4 - test_counter + 1} free test(s) remaining today.",
|
| 282 |
+
f"User ID: {user_id}",
|
| 283 |
+
user_id
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
remaining_tests = 4 - test_counter
|
| 287 |
+
|
| 288 |
+
if remaining_tests > 0:
|
| 289 |
+
return (
|
| 290 |
+
result,
|
| 291 |
+
gr.update(visible=False),
|
| 292 |
+
gr.update(interactive=True),
|
| 293 |
+
f"You have {remaining_tests} free test(s) remaining today.",
|
| 294 |
+
f"User ID: {user_id}",
|
| 295 |
+
user_id
|
| 296 |
+
)
|
| 297 |
+
else:
|
| 298 |
+
redirect_text = f"""
|
| 299 |
+
Thank you for trying our product description generation tool!
|
| 300 |
|
| 301 |
+
You've used all 4 of your free tests for today. We hope you found them helpful and insightful.
|
|
|
|
| 302 |
|
| 303 |
+
To unlock unlimited access and discover how our AI-powered solution can revolutionize your content creation process:
|
|
|
|
| 304 |
|
| 305 |
+
[Book a Personalized Demo with Our Experts]({demo_link})
|
| 306 |
+
|
| 307 |
+
During the demo, you'll get:<br>
|
| 308 |
+
• A tailored walkthrough of our full suite of features<br>
|
| 309 |
+
• Insights on how to optimize your specific content workflow<br>
|
| 310 |
+
• Answers to any questions you may have about our solution<br>
|
| 311 |
+
|
| 312 |
+
We're excited to show you how we can help scale your content creation!
|
| 313 |
+
|
| 314 |
+
(Your free tests will reset tomorrow, allowing you to try 4 more if you need additional time to decide.)
|
| 315 |
+
"""
|
| 316 |
+
return (
|
| 317 |
+
result,
|
| 318 |
+
gr.update(visible=True, value=redirect_text),
|
| 319 |
+
gr.update(interactive=False),
|
| 320 |
+
"You've used all your free tests for today. We invite you to book a demo for full access!",
|
| 321 |
+
f"User ID: {user_id}",
|
| 322 |
+
user_id
|
| 323 |
+
)
|
| 324 |
else:
|
| 325 |
redirect_text = f"""
|
| 326 |
+
Thank you for your interest in our product description generation tool!
|
| 327 |
+
|
| 328 |
+
You've already used your 4 free tests for today. We hope they've given you a glimpse of how our AI can enhance your content creation.
|
| 329 |
+
|
| 330 |
+
Ready to explore the full potential of our solution?
|
| 331 |
+
|
| 332 |
[Book a Personalized Demo with Our Experts]({demo_link})
|
| 333 |
+
|
| 334 |
+
In this demo, you'll discover:<br>
|
| 335 |
+
• How our AI can be customized for your specific needs<br>
|
| 336 |
+
• Advanced features for scaling your content production using a [Product Knowledge Graph](https://wordlift.io/blog/en/product-knowledge-graph/)<br>
|
| 337 |
+
• ROI projections based on your current content workflow<br>
|
| 338 |
+
|
| 339 |
+
Let's discuss how we can help you achieve your content goals!
|
| 340 |
+
|
| 341 |
+
(Remember, your free tests will reset tomorrow if you need more time to evaluate.)
|
| 342 |
"""
|
| 343 |
return (
|
| 344 |
+
"",
|
| 345 |
gr.update(visible=True, value=redirect_text),
|
| 346 |
gr.update(interactive=False),
|
| 347 |
+
"All free tests used. We invite you to book a demo for full access!",
|
| 348 |
f"User ID: {user_id}",
|
| 349 |
user_id
|
| 350 |
)
|
| 351 |
+
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
return (
|
| 353 |
+
f"An error occurred: {str(e)}",
|
| 354 |
+
gr.update(visible=False),
|
| 355 |
+
gr.update(interactive=True),
|
| 356 |
+
"Error processing request",
|
| 357 |
f"User ID: {user_id}",
|
| 358 |
user_id
|
| 359 |
)
|