File size: 4,602 Bytes
6cbffa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import tempfile
import csv

from translator import translate_english_text, enforce_formal_german
from templates import TEMPLATES


# ---------------- SINGLE EMAIL ----------------
def generate_and_translate(template_name, tone, name, sender, target_language):
    template_data = TEMPLATES[template_name][tone]

    subject_en = template_data["subject"]
    body_en = template_data["body"].format(
        name=name or "there",
        sender=sender or "Your Name"
    )

    subject_tr = translate_english_text(subject_en, target_language)
    body_tr = translate_english_text(body_en, target_language)

    if target_language == "German" and tone == "Formal":
        body_tr = enforce_formal_german(body_tr)

    final_email = f"Subject: {subject_tr}\n\n{body_tr}"

    tmp = tempfile.NamedTemporaryFile(
        delete=False, suffix=".txt", mode="w", encoding="utf-8"
    )
    tmp.write(final_email)
    tmp.close()

    return subject_tr, body_tr, final_email, tmp.name


# ---------------- BATCH CSV ----------------
def batch_translate_csv(csv_file):
    if csv_file is None:
        return None

    df = pd.read_csv(csv_file.name)

    rows = []
    for _, row in df.iterrows():
        template = row["template"].strip().replace(" ", "-")
        tone = row["tone"].strip()
        name = row["name"]
        sender = row["sender"]
        language = row["language"]

        tpl = TEMPLATES[template][tone]

        subject_tr = translate_english_text(tpl["subject"], language)
        body_tr = translate_english_text(
            tpl["body"].format(name=name, sender=sender),
            language
        )

        if language == "German" and tone == "Formal":
            body_tr = enforce_formal_german(body_tr)

        rows.append({
            "template": template,
            "tone": tone,
            "name": name,
            "language": language,
            "subject": subject_tr,
            "email_body": body_tr
        })

    out_df = pd.DataFrame(rows)

    tmp = tempfile.NamedTemporaryFile(
        delete=False, suffix=".csv", mode="w", encoding="utf-8", newline=""
    )
    out_df.to_csv(
        tmp.name,
        index=False,
        encoding="utf-8-sig",
        quoting=csv.QUOTE_ALL
    )
    tmp.close()

    return tmp.name


# ---------------- UI ----------------
with gr.Blocks(
    theme=gr.themes.Soft(primary_hue="orange"),
    css=".container {max-width:1200px; margin:auto}"
) as demo:

    gr.Markdown(
        """
        # 🌍 Cross-Border Sales Email Generator
        ### Tone-aware, multilingual, production-ready
        """
    )

    with gr.Tabs():

        # ---------- SINGLE EMAIL TAB ----------
        with gr.Tab("Single Email"):
            with gr.Row():

                with gr.Column(scale=1):
                    template = gr.Dropdown(
                        choices=list(TEMPLATES.keys()),
                        label="Email Template"
                    )
                    tone = gr.Radio(
                        ["Formal", "Casual"], label="Tone", value="Formal"
                    )
                    name = gr.Textbox(label="Recipient Name")
                    sender = gr.Textbox(label="Sender Name")
                    language = gr.Dropdown(
                        choices={
                            "🇩🇪 German": "German",
                            "🇷🇺 Russian": "Russian"
                        },
                        label="Customer Language"
                    )
                    submit_btn = gr.Button("Generate & Translate", variant="primary")

                with gr.Column(scale=1):
                    subject_out = gr.Textbox(label="Translated Subject")
                    body_out = gr.Textbox(label="Translated Email Body", lines=8)
                    final_out = gr.Textbox(label="Final Email", lines=10)
                    file_out = gr.File(label="Download Email (.txt)")

            submit_btn.click(
                fn=generate_and_translate,
                inputs=[template, tone, name, sender, language],
                outputs=[subject_out, body_out, final_out, file_out]
            )

        # ---------- BATCH CSV TAB ----------
        with gr.Tab("Batch CSV"):
            csv_in = gr.File(label="Upload CSV File")
            csv_out = gr.File(label="Download Translated CSV")
            batch_btn = gr.Button("Process CSV", variant="primary")

            batch_btn.click(
                fn=batch_translate_csv,
                inputs=csv_in,
                outputs=csv_out
            )

demo.launch()