Spaces:
Running
Running
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 = """ | |
printerOne | |
Product: Business Cards | |
Size 2x3.5 | |
Quantity,Price per Unit ($) | |
25,2.0 | |
50,1.6 | |
100,1.0 | |
200,0.7 | |
500,0.36 | |
1000,0.2 | |
2000,0.14 | |
5000,0.12 | |
10000,0.09 | |
20000,0.07 | |
""" | |
printer_2 = """ | |
BannerLord | |
Product: Business Cards | |
Size 2x3.5 | |
Quantity,Price per Unit ($) | |
25,2.4 | |
50,1.8 | |
100,1.2 | |
200,0.75 | |
500,0.4 | |
1000,0.22 | |
2000,0.15 | |
5000,0.14 | |
10000,0.1 | |
20000,0.075 | |
""" | |
printer_3 = """ | |
PrintMaster | |
Product: Business Cards | |
Size 2x3.5 | |
Quantity,Price per Unit ($) | |
50,1.4 | |
75,1.2 | |
100,1.05 | |
150,1 | |
200,0.875 | |
250,0.72 | |
500,0.4 | |
750,0.4 | |
1000,0.35 | |
1500,0.3 | |
2000,0.25 | |
""" | |
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 from the printers:\n" | |
f"Printer 1:\n{printer_1}\n" | |
f"Printer 2:\n{printer_2}\n" | |
f"Printer 3:\n{printer_3}\n" | |
"Check if the requested size is available from the printers. If not, provide the nearest size available." | |
"Do not do a linear interpolation. Use the nearest lower quantity pricing and multiply user's amount by nearest price per unit." | |
"Example:\n" | |
"User: I want to print 600 brochures\n" | |
"Answer:\n" | |
"Printer 1: 500 brochures for $0.36 each. 600 * 0.36 = $216\n" | |
"###" | |
"Printer 2: 500 brochures for $0.4 each. 600 * 0.4 = $240\n" | |
"###" | |
"Printer 3: 500 brochures for $0.4 each. 600 * 0.4 = $240\n" | |
"The '###' symbol separates the printer options. It is important to keep it in the response." | |
) | |
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) | |
return chat_text, html_page | |
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 = """ | |
<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; | |
vertical-align: top; | |
} | |
.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; | |
} | |
</style> | |
</head> | |
<body> | |
""" | |
for i, description in enumerate(product_descriptions): | |
image = images[i % len(images)] | |
with open(image, "rb") as img_file: | |
base64_image = base64.b64encode(img_file.read()).decode("utf-8") | |
product_link = product_links[i % len(product_links)] | |
html_content += f""" | |
<div class="product-card"> | |
<img src="data:image/jpeg;base64,{base64_image}" alt="Product Image"> | |
<p>{description}</p> | |
<a href="{product_link}" target="_blank"><button>Buy Now</button></a> | |
</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.Textbox(label="Result"), | |
gr.HTML(label="Product Image"), | |
], | |
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", | |
) | |
valid_users = [("admin", "password123"), ("user1", "pass456")] | |
iface.launch( | |
debug=True, | |
auth=("username", "password"), | |
auth_message="Try this", | |
) | |