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 = ( "
{summary_text}