import base64 import gradio as gr from openai import OpenAI import os import dotenv dotenv.load_dotenv() client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) script_dir = os.path.dirname(os.path.abspath(__file__)) printer_1 = """ # PrintPronto Product: Business card Color: - Black (default) - Blue - Brown - Orange - Pink Sizes: 3.5x2: multiplier 1.0 2.5x2.5: multiplier 0.95 2.125x3.375: multiplier 0.9 Custom size: This vendor doesn't support custom sizes Material: Standard (default): multiplier 1.0 Uncoated: multiplier 0.9 Base Prices (Quantity,Price per Unit $): 100, 0.23 250, 0.2 500, 0.19 1000, 0.175 """ printer_2 = """ # BannerLord Product: Business card Color: - Black (default) - Blue Sizes: 3.5x2: multiplier 1.0 2.5x2.5: multiplier 0.95 2.125x3.375: multiplier 0.9 3x2: multiplier 1.5 4x3: multiplier 1.5 Custom size: multiplier 2.0 Material: Standard (default): multiplier 1.0 Uncoated: multiplier 0.95 Base Prices (Quantity,Price per Unit $): 100, 0.25 250, 0.22 500, 0.2 1000, 0.185 """ printer_3 = """ # PrintMaster Product: Business card Color: - Black (default) - Blue - Brown Sizes: 3.5x2: multiplier 1.0 2.5x2.5: multiplier 0.95 2.125x3.375: multiplier 0.9 3x2: multiplier 1.5 4x3: multiplier 1.5 Custom size: multiplier 2.0 Material: Standard (default): multiplier 1.0 Uncoated: multiplier 0.85 Base Prices (Quantity,Price per Unit $): 100, 0.21 250, 0.2 500, 0.18 1000, 0.17 """ def find_best_price(request): chat_prompt = ( "You are a customer assistant and you have to find the best price for the customer. " "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 request, you should:\n" "1. Check if the requested size is available. If not, check if vendor supports custom sizes.\n" "2. 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 material\n" "3. Do not use linear interpolation for quantities - use the nearest lower quantity pricing.\n\n" "Example:\n" "User: I want to print 600 business cards, 6x6 cm size, \n" "Answer:\n" "PrintPronto: Doesn't support custom size\n" "###\n" "BannerLord: 600 * multiplier for custom size * multiplier for material. Total for 600: $240\n" "###\n" "PrintMaster: 600 * multiplier for custom size * multiplier for material. Total for 600: $216\n\n" "If user haven't specified one of the parameters, use the default one" # "Your response should display ONLY final pricing for each available option, nothing more" "Explain your calculations" "The '###' symbol must separate each printer's price calculation.\n" ) chat_response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": chat_prompt}, {"role": "user", "content": request}, ], ) chat_text = chat_response.choices[0].message.content html_page = render_html_page(chat_text) chat_text = chat_text.replace("###", "") return html_page, chat_text def render_html_page(chat_text): images = [ os.path.join(script_dir, "public/src/brochure3-400x400.jpg"), os.path.join(script_dir, "public/src/quality-unfolded-brochures_1_.png"), os.path.join(script_dir, "public/src/unfolded-glossy-brochures.png"), ] product_links = [ "https://www.example.com/product_image.jpg", "https://www.example.com/product_image.jpg", "https://www.example.com/product_image.jpg", ] # Split chat_text into product descriptions product_descriptions = chat_text.split("###") # Generate HTML content html_content = """