File size: 15,879 Bytes
13eab12
 
07184f5
 
 
 
 
 
bb8f7fc
 
 
 
 
 
 
 
 
c56b036
bb8f7fc
07184f5
 
 
b65aa70
9730fff
bb8f7fc
4c19f61
bb8f7fc
 
 
76f72e1
13eab12
1bc9da9
13eab12
1bc9da9
13eab12
76f72e1
13eab12
1bc9da9
13eab12
1bc9da9
13eab12
 
 
bb8f7fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c19f61
1bc9da9
4c19f61
 
 
 
1bc9da9
13eab12
 
1bc9da9
4c19f61
 
 
7e36be2
4c19f61
13eab12
1bc9da9
13eab12
1bc9da9
7e36be2
 
 
4c19f61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c56b036
337e19c
c56b036
 
 
337e19c
 
 
 
c56b036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337e19c
 
 
 
13eab12
 
 
bb8f7fc
 
 
13eab12
bb8f7fc
 
 
 
 
 
 
 
c56b036
13eab12
4c19f61
 
 
 
 
 
 
 
c56b036
337e19c
c56b036
 
 
 
 
 
 
 
13eab12
bb8f7fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13eab12
 
 
9730fff
 
 
 
 
 
bb8f7fc
9730fff
 
4c19f61
9730fff
 
c56b036
9730fff
 
 
 
 
 
 
 
bb8f7fc
 
c56b036
9730fff
 
 
 
 
 
 
 
4c19f61
 
c56b036
9730fff
 
 
 
 
 
 
 
bb8f7fc
 
c56b036
9730fff
 
95ec04c
9730fff
 
 
 
 
 
bb8f7fc
 
c56b036
9730fff
 
 
13eab12
 
9730fff
95ec04c
13eab12
95ec04c
 
 
13eab12
a9b40df
bb8f7fc
9730fff
 
 
 
bb8f7fc
9730fff
bb8f7fc
9730fff
 
 
 
 
bb8f7fc
9730fff
bb8f7fc
9730fff
 
 
 
 
bb8f7fc
9730fff
bb8f7fc
9730fff
 
49343d0
 
bb8f7fc
49343d0
bb8f7fc
49343d0
 
 
 
 
bb8f7fc
49343d0
 
4c19f61
bb8f7fc
 
 
 
 
 
 
4c19f61
bb8f7fc
 
 
 
 
 
 
c56b036
 
 
 
 
 
 
 
 
 
 
 
49343d0
 
 
 
 
 
 
 
 
 
 
 
 
bb8f7fc
 
c56b036
49343d0
 
 
 
 
 
 
 
 
 
 
 
bb8f7fc
 
 
c56b036
49343d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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()