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()