File size: 2,718 Bytes
dbd2242
6f5226e
8c484ee
3d0a7ac
7988cd8
 
 
 
86b8a99
6f5226e
7988cd8
 
6f5226e
7988cd8
 
8c484ee
 
 
 
 
 
 
 
 
 
d3802b6
 
8c484ee
 
6f5226e
a36f65c
7988cd8
 
 
 
a36f65c
 
a2c18d3
c8b6d8d
a36f65c
 
c8b6d8d
 
7988cd8
 
 
 
 
 
c8b6d8d
a36f65c
7988cd8
 
 
 
a36f65c
c8b6d8d
 
 
 
 
 
 
 
 
8c484ee
c8b6d8d
 
 
 
a36f65c
7988cd8
 
 
 
a36f65c
a2c18d3
7988cd8
 
 
 
a36f65c
7988cd8
 
 
 
 
 
 
 
 
 
 
a2c18d3
 
a36f65c
 
534bda9
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
import gradio as gr
import torch
from transformers import pipeline, GenerationConfig

# ===============================
# MODEL CONFIGURATION
# ===============================

MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"

print("Loading large instruction model into Space memory...")

pipe = pipeline(
    "text-generation",
    model=MODEL_ID,
    torch_dtype=torch.float16,   # important for memory
    device_map="auto",           # let HF handle placement
)

# -------------------------------
# GENERATION CONFIG (SINGLE SOURCE OF TRUTH)
# -------------------------------

generation_config = GenerationConfig(
    max_new_tokens=900,
    do_sample=True,             # deterministic output
    temperature=0.4,             # low temperature = better rule following
    top_p=0.9,
    repetition_penalty=1.05,
)

# ===============================
# EMAIL REWRITE FUNCTION
# ===============================

def rewrite_email(user_text):
    if not user_text.strip():
        return "Please enter an email draft."

    messages = [
        {
            "role": "system",
            "content": (
                "You are a senior Indian consulting professional writing formal business emails. "
                "Rewrite the input into a structured, senior-level corporate email. "
                "Follow instructions exactly. "
                "Do not add headings, emojis, or marketing language. "
                "Use bullet points where appropriate. "
                "Maintain a confident, business-owned tone."
            )
        },
        {
            "role": "user",
            "content": user_text
        }
    ]

    prompt = pipe.tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )

    outputs = pipe(
        prompt,
        generation_config=generation_config,
        return_full_text=False
    )

    return outputs[0]["generated_text"].strip()

# ===============================
# GRADIO UI
# ===============================

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 📧 Professional Email Rewriter")
    gr.Markdown(
        "This app uses a large instruction-tuned model for reliable, senior-level business emails."
    )

    with gr.Row():
        input_text = gr.Textbox(
            label="Your Draft",
            placeholder="Paste email here...",
            lines=20
        )
        output_text = gr.Textbox(
            label="Professional Version",
            interactive=False,
            lines=20
        )

    btn = gr.Button("Rewrite Now", variant="primary")
    btn.click(fn=rewrite_email, inputs=input_text, outputs=output_text)

if __name__ == "__main__":
    demo.launch()