Spaces:
Running
Running
File size: 18,461 Bytes
a36c30d 21e0e87 49a0cfc a36c30d e238d1a a36c30d 49a0cfc a36c30d 0fc96a0 49a0cfc 21f3db9 0fc96a0 21f3db9 0fc96a0 49a0cfc 0fc96a0 21f3db9 0fc96a0 a9d3c11 21f3db9 0fc96a0 48ec5fb 49a0cfc 3f3324e a36c30d 21f3db9 49a0cfc 21f3db9 0fc96a0 21f3db9 0fc96a0 49a0cfc 0fc96a0 e19fc16 21f3db9 0fc96a0 a9d3c11 21f3db9 0fc96a0 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc 3f3324e a36c30d 21f3db9 49a0cfc 0fc96a0 21f3db9 49a0cfc 0fc96a0 e19fc16 21f3db9 0fc96a0 a9d3c11 21f3db9 0fc96a0 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc 3f3324e a36c30d 49a0cfc a36c30d 49a0cfc 21f3db9 75c2197 2415428 21f3db9 49a0cfc 75c2197 a36c30d 49a0cfc a36c30d 49a0cfc e19fc16 49a0cfc e19fc16 49a0cfc 75c2197 49a0cfc a36c30d 49a0cfc a36c30d 49a0cfc a36c30d 49a0cfc a36c30d fb601ea a36c30d 49a0cfc 72a5665 49a0cfc e238d1a 49a0cfc 72a5665 49a0cfc 72a5665 b9a8157 72a5665 49a0cfc a36c30d 49a0cfc a36c30d 75c2197 a36c30d 75c2197 21e0e87 49a0cfc 75c2197 21e0e87 08066b1 21e0e87 08066b1 21e0e87 49a0cfc a36c30d 21e0e87 a36c30d 21e0e87 49a0cfc 75c2197 a36c30d 49a0cfc a36c30d 0fc96a0 a36c30d 49a0cfc a36c30d 9298378 371f28f f701a41 371f28f 0c5d4e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
import base64
import os
import random
import re
import dotenv
import gradio as gr
from openai import OpenAI
dotenv.load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
script_dir = os.path.dirname(os.path.abspath(__file__))
COMPANIES = ["PrintPronto", "BannerLord", "PrintMaster"]
printer_1 = """
# PrintPronto
## Product: Business cards
Color:
- Black (default)
- Blue
- Brown
- Orange
- Pink
Sizes:
3.5x2: multiplier 1.0 (default)
2.5x2.5: multiplier 0.95
2.125x3.375: multiplier 0.9
Custom size: This vendor doesn't support custom sizes
Material:
- Standard (default)
- Uncoated
Base Prices (Quantity,Price per Unit $):
100, 0.23
250, 0.2
500, 0.19
1000, 0.17
## Product: Fabric Banners
Sizes:
2.5x4: multiplier 1.0
4x4: multiplier 1.1
2.5x6: multiplier 1.0 (default)
2.5x8: multiplier 1.2
4x6: multiplier 1.5
Custom size: This vendor doesn't support custom sizes
Base Prices (Quantity,Price per Unit $):
1, 230.00
5, 210.00
10, 201.50
25, 195.26
## Product: Bumper Stickers
Shapes:
Rectangles: multiplier 1.0
Squares: multiplier 1.0
Circles: multiplier 1.0
Ovals: multiplier 1.0
Rounded Rectangles: multiplier 1.0
Custom shape: This vendor doesn't support custom shapes
Sizes:
2x3: multiplier 1.0
2x4: multiplier 1.1
3x4: multiplier 1.0 (default)
3x6: multiplier 1.2
2x8: multiplier 1.5
Custom size: This vendor doesn't support custom sizes
Base Prices (Quantity,Price per Unit $):
50, 1.80
100, 1.10
250, 0.80
500, 0.44
1000, 0.24
## Product: Paper Stickers
Shapes:
Rectangles: multiplier 1.0
Squares: multiplier 1.0
Circles: multiplier 1.0
Ovals: multiplier 1.0
Custom size: This vendor doesn't support custom shapes
Sizes:
0.79: multiplier 1.0 (default)
1: multiplier 1.1
1.18: multiplier 1.0
1.26: multiplier 1.2
1.38: multiplier 1.5
Custom size: This vendor doesn't support custom sizes
Material:
- Gloss Paper (default)
- Matte Paper
Base Prices (Quantity,Price per Unit $):
250, 0,40
500, 0.22
1000, 0.12
2000, 0.09
"""
printer_2 = """
# BannerLord
## Product: Business cards
Color:
- Black (default)
- Blue
Sizes:
3.5x2: multiplier 1.0 (default)
2.5x2.5: multiplier 0.95
2.125x3.375: multiplier 0.9
3x2: multiplier 1.5
4x3: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Material:
- Standard (default)
- Uncoated
Base Prices (Quantity,Price per Unit $):
100, 0.25
250, 0.22
500, 0.2
1000, 0.19
## Product: Fabric Banners
Sizes:
2.5x4: multiplier 1.0
4x4: multiplier 1.1
2.5x6: multiplier 1.0 (default)
2.5x8: multiplier 1.2
4x6: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Base Prices (Quantity,Price per Unit $):
1, 228.00
5, 205.00
10, 204.90
25, 193.56
## Product: Bumper Stickers
Shapes:
Rectangles: multiplier 1.0
Squares: multiplier 1.0
Circles: multiplier 1.0
Ovals: multiplier 1.0
Rounded Rectangles: multiplier 1.0
Custom shape: This vendor supports custom shapes with a multiplier of 1.2
Sizes:
2x3: multiplier 1.0
2x4: multiplier 1.1
3x4: multiplier 1.0 (default)
3x6: multiplier 1.2
2x8: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Base Prices (Quantity,Price per Unit $):
50, 1.90
100, 1.15
250, 0.85
500, 0.45
1000, 0.25
## Product: Paper Stickers
Shapes:
Rectangles: multiplier 1.0
Squares: multiplier 1.0
Circles: multiplier 1.0
Ovals: multiplier 1.0
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Sizes:
0.79: multiplier 1.0 (default)
1: multiplier 1.1
1.18: multiplier 1.0
1.26: multiplier 1.2
1.38: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Material:
- Gloss Paper (default)
- Matte Paper
Base Prices (Quantity,Price per Unit $):
250, 0.42
500, 0.23
1000, 0.13
2000, 0.10
"""
printer_3 = """
# PrintMaster
## Product: Business cards
Color:
- Black (default)
- Blue
- Brown
Sizes:
3.5x2: multiplier 1.0 (default)
2.5x2.5: multiplier 0.95
2.125x3.375: multiplier 0.9
3x2: multiplier 1.5
4x3: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Material:
- Standard (default)
- Uncoated
Base Prices (Quantity,Price per Unit $):
100, 0.21
250, 0.2
500, 0.19
1000, 0.18
## Product: Fabric Banners
Sizes:
2.5x4: multiplier 1.0 (default)
4x4: multiplier 1.1
2.5x6: multiplier 1.0
2.5x8: multiplier 1.2
4x6: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Base Prices (Quantity,Price per Unit $):
1, 231.00
5, 209.50
10, 203.45
25, 194.00
## Product: Bumper Stickers
Shapes:
Rectangles: multiplier 1.0
Squares: multiplier 1.0
Circles: multiplier 1.0
Ovals: multiplier 1.0
Rounded Rectangles: multiplier 1.0
Custom shape: This vendor supports custom shapes with a multiplier of 1.2
Sizes:
2x3: multiplier 1.0 (default)
2x4: multiplier 1.1
3x4: multiplier 1.0
3x6: multiplier 1.2
2x8: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Base Prices (Quantity,Price per Unit $):
50, 1.85
100, 1.10
250, 0.82
500, 0.43
1000, 0.23
## Product: Paper Stickers
Shapes:
Rectangles: multiplier 1.0
Squares: multiplier 1.0
Circles: multiplier 1.0
Ovals: multiplier 1.0
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Sizes:
0.79: multiplier 1.0 (default)
1: multiplier 1.1
1.18: multiplier 1.0
1.26: multiplier 1.2
1.38: multiplier 1.5
Custom size: This vendor supports custom sizes with a multiplier of 1.2
Material:
- Gloss Paper (default)
- Matte Paper
Base Prices (Quantity,Price per Unit $):
250, 0.40
500, 0.21
1000, 0.11
2000, 0.08
"""
IMAGES = {
"Business Cards": {
"images": [
os.path.join(script_dir, "public/src/painted-edge-business-card-05.png"),
os.path.join(
script_dir,
"public/src/painted-edge-cards---w-brown-box-_4-4-color_.png",
),
os.path.join(script_dir, "public/src/silk-business-card-03_1.png"),
],
"links": [
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
],
},
"Fabric Banners": {
"images": [
os.path.join(script_dir, "public/src/03.jpg"),
os.path.join(
script_dir,
"public/src/fabric-banner-01.webp",
),
os.path.join(script_dir, "public/src/fabric-banner-02.webp"),
],
"links": [
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
],
},
"Bumper Stickers": {
"images": [
os.path.join(script_dir, "public/src/clear-bumper-sticker.jpg"),
os.path.join(
script_dir,
"public/src/copy_of_untitled_design_9_.webp",
),
os.path.join(script_dir, "public/src/removable-bumper-stickers.jpg"),
],
"links": [
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
],
},
"Paper Stickers": {
"images": [
os.path.join(script_dir, "public/src/us_images_1_.webp"),
os.path.join(
script_dir,
"public/src/copy_of_us_images_1__1.webp",
),
os.path.join(script_dir, "public/src/copy_of_us_images_1__1.webp"),
],
"links": [
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
"https://www.example.com/product_image.jpg",
],
},
}
def find_best_price(request):
chat_prompt = (
"You are a customer assistant tasked with finding the best price for multiple items from different printers.\n"
"Here are the prices and options from the printers:\n"
f"Printer 1: {printer_1}\n"
f"Printer 2: {printer_2}\n"
f"Printer 3: {printer_3}\n"
"For each product in the request, follow these steps:\n"
"1. Verify if the requested size is available. If the requested size is unavailable, check if the vendor supports custom sizes.\n"
"2. If the user has not specified one or more parameters (e.g., size, shape), use the vendor's default values for those parameters.\n"
"3. Apply the custom size multiplier only under the following conditions:\n"
"- The user explicitly requests a specific size.\n"
"- The requested size is not standard (i.e., it is custom).\n"
"- The vendor supports custom sizes.\n"
"4. Calculate the final price by:\n"
" - Finding the nearest lower quantity in the base price list.\n"
" - Multiplying by the size multiplier\n"
" - Multiplying by the user requested amount. IMPORTANT. Do not multiply by nearest lower quantity!\n"
"5. Do not use linear interpolation for quantities - use the nearest lower quantity pricing.\n\n"
"Example:\n"
"User: I want 25 banners in size 6x6, 2000 business cards and 5000 circle stickers in size 6x6 on matte paper\n"
"Answer:\n"
"Banners:\n"
"- PrintPronto: Doesn't support custom size\n"
"- BannerLord: amount (25) * multiplier for custom size (1.2) * price per unit for nearest pack size ($193.56). Total for 25: $5806.80\n"
"- PrintMaster: amount (25) * multiplier for custom size (1.2) * price per unit for nearest pack size ($194.00). Total for 25: $5820.00\n\n"
"Business cards:\n"
"- PrintPronto: amount (2000) * multiplier for default size (1.0) * price per unit for nearest pack size ($0.17). Total for 2000: $340.00\n"
"- BannerLord: amount (2000) * multiplier for default size (1.0) * price per unit for nearest pack size ($0.19). Total for 2000: $380.00\n"
"- PrintMaster: amount (2000) * multiplier for default size (1.0) * price per unit for nearest pack size ($0.18). Total for 2000: $360.00\n\n"
" Paper stickers:\n"
"- PrintPronto: Doesn't support custom size\n"
"- BannerLord: amount (5000) * multiplier for circle shape (1.0) * multiplier for custom size (1.2) * price per unit for nearest pack size ($0.13). Total for 5000: $780.00\n"
"- PrintMaster: amount (5000) * multiplier for circle shape (1.0) * multiplier for custom size (1.2) * price per unit for nearest pack size ($0.11). Total for 5000: $660.00\n\n"
"Your response should display ONLY final pricing for each available option, nothing more.\n"
"At the end of your response, write summary in the following format:\n"
"Full quote (all products): *all items, that user's requested*\n"
"- Printer1: $price of full request\n"
"- Printer2: $price of full request\n"
"- Printer3: $price of full request\n"
)
chat_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": chat_prompt},
{"role": "user", "content": request},
],
temperature=0.5,
)
chat_text = chat_response.choices[0].message.content
chat_text = chat_text.replace("###", "")
images_to_use = extract_requested_products(
request=request, product_names=list(IMAGES.keys())
)
if images_to_use is None:
html_page = (
"<html><body><h3>Requested product is not available.</h3></body></html>"
)
chat_text = "Requested product is not available."
return html_page, chat_text
html_page = render_html_page(chat_text, images_to_use)
return html_page, chat_text
def extract_total_prices(text):
"""
Extracts total prices and availability information from a text block,
preserving the order of appearance.
Args:
text: The input text.
Returns:
A list of strings containing prices and availability messages in order.
"""
results = []
# Combine patterns using the OR operator | and capture groups
pattern = (
r"(Total for \d+: (\$\d+(?:,\d{3})*(?:\.\d{2})?))|(Doesn't support custom size)"
)
matches = re.findall(pattern, text)
for match in matches:
# Check which group matched
if match[0]: # Price match
results.append(match[1])
elif match[2]: # Availability match
results.append(match[2])
return results
def extract_requested_products(request, product_names):
product_list = "\n".join(f"- {product}" for product in product_names)
prompt = (
"Here are the available product names:\n"
f"{product_list}\n"
"Identify any products explicitly or implicitly mentioned in the user's request. "
"If a product is mentioned in different sizes, consider each size as a separate product and include them in the list. "
"However, do not include duplicate entries of the same product unless explicitly mentioned multiple times in the request. "
"If no products match, respond with 'None'. Output the result as a Python list. "
"Your response should contain only the list or 'None'."
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": request},
],
temperature=0.1,
)
response_text = response.choices[0].message.content
return eval(response_text)
def render_html_page(chat_text, images_to_use):
if images_to_use is None:
return "<html><body><h3>Requested product is not available.</h3></body></html>"
html_content = """
<html>
<head>
<title>Product Cards</title>
<style>
.product-card {
border: 1px solid #ccc;
border-radius: 5px;
padding: 16px;
margin: 16px;
text-align: center;
width: 200px;
display: inline-block;
}
.product-card img {
max-width: 100%;
height: auto;
}
.product-card button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.product-card button:hover {
background-color: #45a049;
}
.summary {
border-top: 2px solid #ccc;
padding-top: 16px;
margin-top: 20px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
"""
# Assuming processed_prices is a flat list
processed_prices = extract_total_prices(chat_text)
# Process images and links
processed_images = [
image for img in images_to_use for image in IMAGES[img]["images"]
]
processed_links = [link for img in images_to_use for link in IMAGES[img]["links"]]
# Initialize labels for all products
labels = [""] * len(processed_prices)
# Group prices in subsets of 3 and assign labels
for group_start in range(0, len(processed_prices), 3):
subset_indices = list(
range(group_start, min(group_start + 3, len(processed_prices)))
)
subset_prices = [processed_prices[i] for i in subset_indices]
# Find the index of the lowest price in this subset
min_price_index = subset_indices[subset_prices.index(min(subset_prices))]
labels[min_price_index] = "<b>BEST VALUE</b>"
# Assign remaining labels randomly
remaining_indices = [i for i in subset_indices if i != min_price_index]
random.shuffle(remaining_indices)
labels[remaining_indices[0]] = "<b>BEST QUALITY</b>"
labels[remaining_indices[1]] = (
"<b>FAST PRODUCTION</b>" if len(remaining_indices) > 1 else ""
)
# Generate HTML content
for i in range(len(processed_images)):
image = processed_images[i]
with open(image, "rb") as img_file:
base64_image = base64.b64encode(img_file.read()).decode("utf-8")
# Add price with label
price_with_label = (
f"{labels[i]}<br>{COMPANIES[i % 3]} "
f"{images_to_use[i // 3].lower()}: {processed_prices[i]}"
)
html_content += f"""
<div class="product-card">
<img src="data:image/jpeg;base64,{base64_image}" alt="Product Image">
<p>{price_with_label}</p>
<a href="{processed_links[i]}" target="_blank"><button>Buy Now</button></a>
</div>
"""
# Extract the summary from chat_text
# print(chat_text)
summary_start = chat_text.find("Full quote (all products):")
# print(summary_start)
if summary_start != -1:
summary_text = chat_text[summary_start:].strip()
html_content += f"""
<div class="summary">
<h3>Quote Summary</h3>
<pre>{summary_text}</pre>
</div>
"""
html_content += "</body></html>"
return html_content
logo = os.path.join(script_dir, "public/src/logo.svg")
with open(logo, "rb") as logo_file:
base64_logo = base64.b64encode(logo_file.read()).decode("utf-8")
iface = gr.Interface(
fn=find_best_price,
inputs=gr.Textbox(lines=3, placeholder="Enter what are you looking for"),
outputs=[
gr.HTML(label="Product Image"),
gr.Textbox(label="AI response: text mode"),
],
title="Get Instant Quote",
description=f"""
<div style="display: flex; justify-content: center; align-items: center; text-align: center; margin-bottom: 20px;">
<img src="data:image/svg+xml;base64,{base64_logo}" alt="Logo" style="width: 150px; height: auto;">
</div>
""",
submit_btn="Get quote",
)
def auth_function(username, password):
# valid_users = {"admin": "demo4anthony1", "123": "123"}
valid_users = {"admin": "demo4anthony1"}
return username in valid_users and valid_users[username] == password
iface.launch(auth=auth_function, share=True, ssr_mode=False)
|