atingupta2005 commited on
Commit
a2c18d3
·
verified ·
1 Parent(s): f301a33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -2,51 +2,50 @@ import gradio as gr
2
  import torch
3
  from transformers import pipeline
4
 
5
- # 1. INITIALIZE MODEL LOCALLY
6
- # This ensures 0% chance of 404/401 errors.
7
- # SmolLM2 is highly optimized for 2026 hardware/software.
8
  MODEL_ID = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
9
 
10
- # Using pipeline for simplicity and stability
 
11
  pipe = pipeline(
12
  "text-generation",
13
  model=MODEL_ID,
14
- device_map="auto",
15
- torch_dtype=torch.bfloat16
16
  )
17
 
18
  def rewrite_email(user_text):
19
  if not user_text.strip():
20
- return "Please enter some text."
21
 
22
- # Format the prompt using the model's chat template
23
  messages = [
24
  {
25
  "role": "system",
26
- "content": "You are a professional business editor. Rewrite the email to be professional and remove all spammy language. Keep it concise."
27
  },
28
  {"role": "user", "content": user_text}
29
  ]
30
 
31
- # Generate response
32
  prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
- outputs = pipe(prompt, max_new_tokens=500, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
34
 
35
- # Extract only the newly generated text
36
- full_output = outputs[0]["generated_text"]
37
- return full_output.split("<|im_start|>assistant")[-1].strip()
38
 
39
- # 2. GRADIO INTERFACE
40
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
41
- gr.Markdown("# 📧 Local Professional Email Rewriter")
42
- gr.Markdown("Running locally in your Space — No API dependencies, No 404s.")
43
 
44
  with gr.Row():
45
- input_text = gr.Textbox(label="Draft Email", placeholder="Paste your draft...", lines=8)
46
- output_text = gr.Textbox(label="Professional Version", interactive=False, lines=8)
47
 
48
- rewrite_btn = gr.Button("Rewrite Email", variant="primary")
49
- rewrite_btn.click(fn=rewrite_email, inputs=input_text, outputs=output_text)
50
 
51
  if __name__ == "__main__":
52
  demo.launch()
 
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 = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
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."
21
 
22
+ # Professional System Prompt
23
  messages = [
24
  {
25
  "role": "system",
26
+ "content": "You are a professional editor. Rewrite the email to be professional, clear, and non-spammy. Maintain a polite tone."
27
  },
28
  {"role": "user", "content": user_text}
29
  ]
30
 
31
+ # Generate the text
32
  prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
34
 
35
+ # Extract response
36
+ return outputs[0]["generated_text"].split("<|im_start|>assistant")[-1].strip()
 
37
 
38
+ # 2. GRADIO UI
39
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
40
+ gr.Markdown("# 📧 Professional Email Rewriter")
41
+ gr.Markdown("This app runs locally on Hugging Face Infrastructure for maximum reliability.")
42
 
43
  with gr.Row():
44
+ input_text = gr.Textbox(label="Your Draft", placeholder="Paste email here...", lines=6)
45
+ output_text = gr.Textbox(label="Professional Version", interactive=False, lines=6)
46
 
47
+ btn = gr.Button("Rewrite Now", variant="primary")
48
+ btn.click(fn=rewrite_email, inputs=input_text, outputs=output_text)
49
 
50
  if __name__ == "__main__":
51
  demo.launch()