atingupta2005 commited on
Commit
7988cd8
·
verified ·
1 Parent(s): 86b8a99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -22
app.py CHANGED
@@ -2,19 +2,25 @@ import gradio as gr
2
  import torch
3
  from transformers import pipeline
4
 
5
- # 1. LOCAL MODEL LOADING
6
- # SmolLM2 is high-performance and fits in the 16GB RAM of a free Space.
 
 
7
  MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
8
 
9
- print("Loading model into Space memory...")
10
- # We use torch.float32 for CPU stability
11
  pipe = pipeline(
12
- "text-generation",
13
- model=MODEL_ID,
14
- torch_dtype=torch.float32,
15
- device_map="cpu"
16
  )
17
 
 
 
 
 
18
  def rewrite_email(user_text):
19
  if not user_text.strip():
20
  return "Please enter an email draft."
@@ -23,12 +29,18 @@ def rewrite_email(user_text):
23
  {
24
  "role": "system",
25
  "content": (
26
- "You are an Indian corporate professional skilled in writing senior-level business emails. "
27
- "Rewrite the email to be professional, clear, concise, and non-promotional. "
28
- "Maintain a polite, understated tone."
 
 
 
29
  )
30
  },
31
- {"role": "user", "content": user_text}
 
 
 
32
  ]
33
 
34
  prompt = pipe.tokenizer.apply_chat_template(
@@ -39,26 +51,39 @@ def rewrite_email(user_text):
39
 
40
  outputs = pipe(
41
  prompt,
42
- max_new_tokens=700,
43
- do_sample=True,
44
- temperature=0.6,
45
  top_p=0.9,
46
  return_full_text=False
47
  )
48
 
49
  return outputs[0]["generated_text"].strip()
50
 
51
- # 2. GRADIO UI
 
 
 
52
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
53
  gr.Markdown("# 📧 Professional Email Rewriter")
54
- gr.Markdown("This app runs locally on Hugging Face Infrastructure for maximum reliability.")
55
-
 
 
56
  with gr.Row():
57
- input_text = gr.Textbox(label="Your Draft", placeholder="Paste email here...", lines=20)
58
- output_text = gr.Textbox(label="Professional Version", interactive=False, lines=20)
59
-
 
 
 
 
 
 
 
 
60
  btn = gr.Button("Rewrite Now", variant="primary")
61
  btn.click(fn=rewrite_email, inputs=input_text, outputs=output_text)
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
2
  import torch
3
  from transformers import pipeline
4
 
5
+ # ===============================
6
+ # MODEL CONFIGURATION
7
+ # ===============================
8
+
9
  MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
10
 
11
+ print("Loading large instruction model into Space memory...")
12
+
13
  pipe = pipeline(
14
+ "text-generation",
15
+ model=MODEL_ID,
16
+ torch_dtype=torch.float16, # important for memory
17
+ device_map="auto", # let HF handle placement
18
  )
19
 
20
+ # ===============================
21
+ # EMAIL REWRITE FUNCTION
22
+ # ===============================
23
+
24
  def rewrite_email(user_text):
25
  if not user_text.strip():
26
  return "Please enter an email draft."
 
29
  {
30
  "role": "system",
31
  "content": (
32
+ "You are a senior Indian consulting professional writing formal business emails. "
33
+ "Rewrite the input into a structured, senior-level corporate email. "
34
+ "Follow instructions exactly. "
35
+ "Do not add headings, emojis, or marketing language. "
36
+ "Use bullet points where appropriate. "
37
+ "Maintain a confident, business-owned tone."
38
  )
39
  },
40
+ {
41
+ "role": "user",
42
+ "content": user_text
43
+ }
44
  ]
45
 
46
  prompt = pipe.tokenizer.apply_chat_template(
 
51
 
52
  outputs = pipe(
53
  prompt,
54
+ max_new_tokens=900,
55
+ do_sample=False, # IMPORTANT: deterministic output
56
+ temperature=0.2, # low temp = better rule following
57
  top_p=0.9,
58
  return_full_text=False
59
  )
60
 
61
  return outputs[0]["generated_text"].strip()
62
 
63
+ # ===============================
64
+ # GRADIO UI
65
+ # ===============================
66
+
67
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
68
  gr.Markdown("# 📧 Professional Email Rewriter")
69
+ gr.Markdown(
70
+ "This app uses a large instruction-tuned model for reliable, senior-level business emails."
71
+ )
72
+
73
  with gr.Row():
74
+ input_text = gr.Textbox(
75
+ label="Your Draft",
76
+ placeholder="Paste email here...",
77
+ lines=20
78
+ )
79
+ output_text = gr.Textbox(
80
+ label="Professional Version",
81
+ interactive=False,
82
+ lines=20
83
+ )
84
+
85
  btn = gr.Button("Rewrite Now", variant="primary")
86
  btn.click(fn=rewrite_email, inputs=input_text, outputs=output_text)
87
 
88
  if __name__ == "__main__":
89
+ demo.launch()