email-template / app.py
leadingbridge's picture
Update app.py
a9b40df 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, customer_address, delivered_date, tracking_link, tracking_number, template_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 ""
customer_address = process_multiline(customer_address) if customer_address else ""
tracking_link = process_multiline(tracking_link) if tracking_link else ""
# delivered_date and tracking_number are single row variables, so we use them as is.
# Define the email templates with placeholders.
template1 = """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"""
template2 = """Dear [variable: customer name],
Thanks for your order. We would like to update you your order status as following:
Out of Stock:
[variable: out of stock product]
Dispatched:
[variable: dispatched 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"""
template3 = """Dear [variable: customer name],
Thank you for your recent order. According to tracking information from both DHL and USPS, your package was successfully delivered on [variable: delivered date]. You can verify the delivery status using the following tracking links:
[variable: tracking link]
The package was sent to the following address:
[variable: customer address]
Since the tracking details indicate a successful delivery, we recommend checking the usual delivery locations at your residence. If you're unable to locate the package, please contact USPS directly with the tracking number [variable: tracking number] for further assistance.
If you have any additional questions or need further support, feel free to reach out to us.
Best regards,
Winson Lai
Trendy Sweet Shop"""
# New "Order Status" template
template4 = """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]
Should you have any further question, please feel free to contact us again. Thanks.
Regards,
Winson Lai
Trendy Sweet Shop"""
# Choose the email template based on the dropdown selection.
if template_choice == "Standard out of stock":
email_template = template1
elif template_choice == "Partial Out of Stock":
email_template = template2
elif template_choice == "US not received":
email_template = template3
elif template_choice == "Order Status":
email_template = template4
else:
email_template = ""
# Replace the placeholders with the input values.
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: customer address]", customer_address)
email_template = email_template.replace("[variable: delivered date]", delivered_date if delivered_date else "")
email_template = email_template.replace("[variable: tracking link]", tracking_link)
email_template = email_template.replace("[variable: tracking number]", tracking_number if tracking_number else "")
return email_template
with gr.Blocks(title="Email Generator") as demo:
gr.Markdown("# Email Generator")
# Input fields for the variables.
with gr.Row():
customer_name_input = gr.Textbox(label="Customer Name", placeholder="Enter customer name")
out_of_stock_input = gr.Textbox(label="Out of Stock Product", placeholder="Enter out-of-stock product (one per row)", lines=3)
dispatched_input = gr.Textbox(label="Dispatched Product", placeholder="Enter dispatched product (if any, one per row)", lines=3)
# New input fields for customer address, delivered date, tracking link, and tracking number.
with gr.Row():
customer_address_input = gr.Textbox(label="Customer Address", placeholder="Enter customer address (one per row)", lines=3)
with gr.Row():
delivered_date_input = gr.Textbox(label="Delivered Date", placeholder="Enter delivered date (single row)")
tracking_number_input = gr.Textbox(label="Tracking Number", placeholder="Enter tracking number (single row)")
with gr.Row():
tracking_link_input = gr.Textbox(label="Tracking Link", placeholder="Enter tracking link (one per row)", lines=3)
# Dropdown for selecting the email template.
template_choice = gr.Dropdown(
choices=["Standard out of stock", "Partial Out of Stock", "US not received", "Order Status"],
label="Select Email Template"
)
generate_button = gr.Button("Generate Email")
email_output = gr.Textbox(label="Generated Email", lines=15)
# When the button is clicked, generate_email() is called.
generate_button.click(
fn=generate_email,
inputs=[
customer_name_input,
out_of_stock_input,
dispatched_input,
customer_address_input,
delivered_date_input,
tracking_link_input,
tracking_number_input,
template_choice
],
outputs=email_output
)
# Added hyperlink block with separate sections for Shipping Tools and Administration Tools.
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>
</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-feed">Google Merchant</a> |
<a href="https://huggingface.co/spaces/leadingbridge/tss-order">Order Processing</a>
</div>
"""
)
demo.launch()