email-template / app.py
leadingbridge's picture
Update app.py
c56b036 verified
import gradio as gr
def process_multiline(variable):
# If variable is a list (multiple rows), join them with newlines.
if isinstance(variable, list):
return "\n".join(variable)
return variable
def generate_email(
customer_name,
out_of_stock,
dispatched,
tracking_number,
tracking_link,
template_choice,
available_date,
solution_choice,
status_choice,
):
# Process variables that may contain multiple rows.
out_of_stock = process_multiline(out_of_stock) if out_of_stock else ""
dispatched = process_multiline(dispatched) if dispatched else ""
tracking_link = process_multiline(tracking_link) if tracking_link else ""
# ===== TEMPLATES =====
# --- Standard out of stock templates ---
# Standard out of stock – Option 1: Refund or exchange
template_std_1 = """Dear [variable: customer name],
Thank you for your recent order. We regret to inform you that, according to our vendor, the below product you have ordered are currently out of stock:
[variable: out of stock product]
Since we do not have an estimated time of when it will be back in stock at this moment, as a solution, we would like to offer you either a refund or an exchange for an alternative model. We understand that this situation may have caused inconvenience and we sincerely apologize for that.
Please let us know your preference by replying to this email and we will take care of the rest. If you have any further questions or concerns, please do not hesitate to reach out to us.
Sincerely,
Winson Lai
Trendy Sweet Shop"""
# Standard out of stock – Option 2: Refund
template_std_2 = """Dear [variable: customer name],
Thank you for your recent order. We regret to inform you that, according to our vendor, the below product you have ordered are currently out of stock:
[variable: out of stock product]
Since we do not have an estimated time of when it will be back in stock at this moment. As a result, we are obligated to issue a full refund for your order. We understand that this situation may have caused inconvenience and we sincerely apologize for that.
Please allow us a few days to process the refund. You should expect to see the refund reflected in your credit within 10 business days. Thank you for your understanding and patience.
Sincerely,
Winson Lai
Trendy Sweet Shop"""
# Standard out of stock – Option 3: Sent Later
template_std_3 = """Dear [variable: customer name],
Thank you for your recent order. We regret to inform you that, according to our vendor, the below product you have ordered are currently out of stock:
[variable: out of stock product]
We expect the models above will be available by [variable: available date] and we will dispatch it once available, or you may ask for refund/exchange before we dispatch the model. We apologize for the inconvenience caused and we will keep you updated for the status. Thanks.
Sincerely,
Winson Lai
Trendy Sweet Shop"""
# --- Partial Out of Stock templates (DISPATCHED first) ---
# Partial – Option 1: Refund or exchange
template_partial_1 = """Dear [variable: customer name],
Thanks for your order. We would like to update you your order status as following :
Dispatched:
[variable: dispatched product]
Out of Stock:
[variable: out of stock product]
Since we do not have an estimated time of when the outstanding item will be back in stock at this moment, as a solution, we would like to offer you either a refund or an exchange for an alternative model. We understand that this situation may have caused inconvenience and we sincerely apologize for that.
Please let us know your preference by replying to this email and we will take care of the rest. If you have any further questions or concerns, please do not hesitate to reach out to us.
Sincerely,
Winson Lai
Trendy Sweet Shop"""
# Partial – Option 2: Refund
template_partial_2 = """Dear [variable: customer name],
Thanks for your order. We would like to update you your order status as following :
Dispatched:
[variable: dispatched product]
Out of Stock:
[variable: out of stock product]
Since we do not have an estimated time of when the outstanding item will be back in stock at this moment. As a result, we are obligated to issue a full refund for your order. We understand that this situation may have caused inconvenience and we sincerely apologize for that.
Please allow us a few days to process the refund. You should expect to see the refund reflected in your credit within 10 business days. Thank you for your understanding and patience.
Sincerely,
Winson Lai
Trendy Sweet Shop"""
# Partial – Option 3: Sent Later
template_partial_3 = """Dear [variable: customer name],
Thanks for your order. We would like to update you your order status as following :
Dispatched:
[variable: dispatched product]
Out of Stock:
[variable: out of stock product]
We expect the models above will be available by [variable: available date] and we will dispatch it once available, or you may ask for refund/exchange before we dispatch the model. We apologize for the inconvenience caused and we will keep you updated for the status. Thanks.
Sincerely,
Winson Lai
Trendy Sweet Shop"""
# --- Order Status templates with "status" dropdown ---
# Status – Option 1: Sent with tracking provided
template_status_1 = """Dear [variable: customer name],
Thanks for your order. The tracking number of your package is [variable: tracking number] and below is the tracking link:
[variable: tracking link]
Your package is on its way, and you can monitor the delivery status from the tracking link above.
Should you have any further question, please feel free to contact us again. Thanks.
Regards,
Winson Lai
Trendy Sweet Shop"""
# Status – Option 2: Sent without tracking provided
template_status_2 = """Dear [variable: customer name],
Thank you for your order. We confirm that your package has been dispatched, and the tracking link has been sent to you via our system. If you cannot locate the email, please check your junk or spam folder.
You can monitor its delivery status using the tracking link provided.
Should you have any further question, please feel free to contact us again. Thanks.
Regards,
Winson Lai
Trendy Sweet Shop"""
# Status – Option 3: To be sent
template_status_3 = """Dear [variable: customer name],
Thanks for your order. Your order is still under processing and we will release the tracking code once available.
Should you have any further question, please feel free to contact us again. Thanks.
Regards,
Winson Lai
Trendy Sweet Shop"""
# ===== CHOOSE TEMPLATE =====
email_template = ""
if template_choice == "Standard out of stock":
if solution_choice == "Refund or exchange":
email_template = template_std_1
elif solution_choice == "Refund":
email_template = template_std_2
elif solution_choice == "Sent Later":
email_template = template_std_3
else:
email_template = template_std_1 # fallback
elif template_choice == "Partial Out of Stock":
if solution_choice == "Refund or exchange":
email_template = template_partial_1
elif solution_choice == "Refund":
email_template = template_partial_2
elif solution_choice == "Sent Later":
email_template = template_partial_3
else:
email_template = template_partial_1 # fallback
elif template_choice == "Order Status":
if status_choice == "Sent with tracking provided":
email_template = template_status_1
elif status_choice == "Sent without tracking provided":
email_template = template_status_2
elif status_choice == "To be sent":
email_template = template_status_3
else:
email_template = template_status_1 # fallback
# ===== REPLACE PLACEHOLDERS =====
email_template = email_template.replace(
"[variable: customer name]", customer_name if customer_name else ""
)
email_template = email_template.replace(
"[variable: out of stock product]", out_of_stock
)
email_template = email_template.replace(
"[variable: dispatched product]", dispatched
)
email_template = email_template.replace(
"[variable: tracking link]", tracking_link
)
email_template = email_template.replace(
"[variable: tracking number]", tracking_number if tracking_number else ""
)
email_template = email_template.replace(
"[variable: available date]", available_date if available_date else ""
)
return email_template
def update_visibility(template_choice):
"""
Control which input boxes are visible based on the selected template.
- Standard out of stock:
Customer Name, Out of Stock Product, Available Date, Solutions dropdown
- Partial Out of Stock:
Customer Name, Out of Stock Product, Dispatched Product, Available Date, Solutions dropdown
- Order Status:
Customer Name, Tracking Number, Tracking Link, Status dropdown
"""
if template_choice == "Standard out of stock":
return (
gr.update(visible=True), # customer_name_input
gr.update(visible=True), # out_of_stock_input
gr.update(visible=False), # dispatched_input
gr.update(visible=False), # tracking_number_input
gr.update(visible=False), # tracking_link_input
gr.update(visible=True), # available_date_input
gr.update(visible=True), # solutions_dropdown
gr.update(visible=False), # status_dropdown
)
elif template_choice == "Partial Out of Stock":
return (
gr.update(visible=True), # customer_name_input
gr.update(visible=True), # out_of_stock_input
gr.update(visible=True), # dispatched_input
gr.update(visible=False), # tracking_number_input
gr.update(visible=False), # tracking_link_input
gr.update(visible=True), # available_date_input
gr.update(visible=True), # solutions_dropdown
gr.update(visible=False), # status_dropdown
)
elif template_choice == "Order Status":
return (
gr.update(visible=True), # customer_name_input
gr.update(visible=False), # out_of_stock_input
gr.update(visible=False), # dispatched_input
gr.update(visible=True), # tracking_number_input
gr.update(visible=True), # tracking_link_input
gr.update(visible=False), # available_date_input
gr.update(visible=False), # solutions_dropdown
gr.update(visible=True), # status_dropdown
)
else:
# Blank / default: hide everything
return (
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
)
with gr.Blocks(title="Email Generator") as demo:
gr.Markdown("# Email Generator")
# Template selector at the beginning, blank default
template_choice = gr.Dropdown(
choices=["", "Standard out of stock", "Partial Out of Stock", "Order Status"],
label="Select Email Template",
value=""
)
# Inputs (initially hidden, will be shown by template selection)
with gr.Row():
customer_name_input = gr.Textbox(
label="Customer Name",
placeholder="Enter customer name",
visible=False,
)
with gr.Row():
out_of_stock_input = gr.Textbox(
label="Out of Stock Product",
placeholder="Enter out-of-stock product (one per row)",
lines=3,
visible=False,
)
with gr.Row():
dispatched_input = gr.Textbox(
label="Dispatched Product",
placeholder="Enter dispatched product (if any, one per row)",
lines=3,
visible=False,
)
with gr.Row():
tracking_number_input = gr.Textbox(
label="Tracking Number",
placeholder="Enter tracking number (single row)",
visible=False,
)
with gr.Row():
tracking_link_input = gr.Textbox(
label="Tracking Link",
placeholder="Enter tracking link (one per row)",
lines=3,
visible=False,
)
# Available Date (Standard & Partial – Sent Later)
with gr.Row():
available_date_input = gr.Textbox(
label="Available Date",
placeholder="e.g. late January 2026 / 25 Jan 2026",
visible=False,
)
# Solutions dropdown (Standard & Partial out of stock)
solutions_dropdown = gr.Dropdown(
choices=["Refund or exchange", "Refund", "Sent Later"],
value="Refund or exchange",
label="Solutions",
visible=False,
)
# Status dropdown (Order Status)
status_dropdown = gr.Dropdown(
choices=[
"Sent with tracking provided",
"Sent without tracking provided",
"To be sent",
],
value="Sent with tracking provided",
label="Status",
visible=False,
)
generate_button = gr.Button("Generate Email")
email_output = gr.Textbox(label="Generated Email", lines=15)
# Link template choice to visibility logic
template_choice.change(
fn=update_visibility,
inputs=template_choice,
outputs=[
customer_name_input,
out_of_stock_input,
dispatched_input,
tracking_number_input,
tracking_link_input,
available_date_input,
solutions_dropdown,
status_dropdown,
]
)
# Generate email
generate_button.click(
fn=generate_email,
inputs=[
customer_name_input,
out_of_stock_input,
dispatched_input,
tracking_number_input,
tracking_link_input,
template_choice,
available_date_input,
solutions_dropdown,
status_dropdown,
],
outputs=email_output
)
# Hyperlink block (unchanged)
gr.HTML(
"""
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
<h3>Shipping Tools</h3>
<a href="https://huggingface.co/spaces/leadingbridge/shipping-dhl-e-commerce">DHL</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-ec-ship">EC-Ship</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-fedex">Fedex</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-UPS">UPS</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-yunexpress">Yunexpress</a>
</div>
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
<h3>Administration Tools</h3>
<a href="https://huggingface.co/spaces/leadingbridge/email-template">Email Template</a> |
<a href="https://huggingface.co/spaces/leadingbridge/product-feeding">Google Merchant</a> |
<a href="https://huggingface.co/spaces/leadingbridge/tss">Order Processing</a>|
<a href="https://huggingface.co/spaces/leadingbridge/ammu-order">Ammu Order</a>|
<a href="https://huggingface.co/spaces/leadingbridge/tracking-upload">Tracking Upload</a>|
</div>
"""
)
demo.launch()