leadingbridge commited on
Commit
13eab12
·
verified ·
1 Parent(s): e7571d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py CHANGED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def generate_email(customer_name, out_of_stock, dispatched, template_choice):
4
+ # Define the two email templates with placeholders.
5
+ template1 = """Dear [variable: customer name],
6
+
7
+ 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:
8
+
9
+ [variable: out of stock product]
10
+
11
+ 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.
12
+
13
+ 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.
14
+
15
+ Sincerely,
16
+
17
+ Winson Lai
18
+ Trendy Sweet Shop"""
19
+
20
+ template2 = """Dear [variable: customer name],
21
+
22
+ Thanks for your order. We would like to update you your order status as following :
23
+
24
+ Out of Stock:
25
+ [variable: out of stock product]
26
+
27
+ Dispatched:
28
+ [variable: dispatched product]
29
+
30
+ Since we do not have an estimated time of when the outstanding item will be back in stock at this moment, a 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.
31
+
32
+ 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.
33
+
34
+ Sincerely,
35
+
36
+ Winson Lai
37
+ Trendy Sweet Shop"""
38
+
39
+ # Choose the email template based on the dropdown selection.
40
+ if template_choice == "Standard out of stock":
41
+ email_template = template1
42
+ elif template_choice == "Partial Out of Stock":
43
+ email_template = template2
44
+ else:
45
+ email_template = ""
46
+
47
+ # Replace the placeholders with the input values.
48
+ # If no value is provided, the placeholder will be replaced with an empty string.
49
+ email_template = email_template.replace("[variable: customer name]", customer_name if customer_name else "")
50
+ email_template = email_template.replace("[variable: out of stock product]", out_of_stock if out_of_stock else "")
51
+ email_template = email_template.replace("[variable: dispatched product]", dispatched if dispatched else "")
52
+
53
+ return email_template
54
+
55
+ with gr.Blocks(title="Email Generator") as demo:
56
+ gr.Markdown("# Email Generator")
57
+
58
+ # Input fields for the variables.
59
+ with gr.Row():
60
+ customer_name_input = gr.Textbox(label="Customer Name", placeholder="Enter customer name")
61
+ out_of_stock_input = gr.Textbox(label="Out of Stock Product", placeholder="Enter out-of-stock product")
62
+ dispatched_input = gr.Textbox(label="Dispatched Product", placeholder="Enter dispatched product (if any)")
63
+
64
+ # Dropdown for selecting the email template.
65
+ template_choice = gr.Dropdown(
66
+ choices=["Standard out of stock", "Partial Out of Stock"],
67
+ label="Select Email Template"
68
+ )
69
+
70
+ generate_button = gr.Button("Generate Email")
71
+ email_output = gr.Textbox(label="Generated Email", lines=15)
72
+
73
+ # When the button is clicked, generate_email() is called.
74
+ generate_button.click(
75
+ fn=generate_email,
76
+ inputs=[customer_name_input, out_of_stock_input, dispatched_input, template_choice],
77
+ outputs=email_output
78
+ )
79
+
80
+ # HTML block for the Copy Email button using JavaScript.
81
+ gr.HTML("""
82
+ <div style="margin-top: 10px;">
83
+ <button onclick="navigator.clipboard.writeText(document.querySelector('[aria-label=\\"Generated Email\\"] textarea').value)">
84
+ Copy Email
85
+ </button>
86
+ </div>
87
+ """)
88
+
89
+ demo.launch()