Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,50 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# 1.
|
| 6 |
-
#
|
| 7 |
-
# SmolLM2 is highly optimized for 2026 hardware/software.
|
| 8 |
MODEL_ID = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
pipe = pipeline(
|
| 12 |
"text-generation",
|
| 13 |
model=MODEL_ID,
|
| 14 |
-
|
| 15 |
-
|
| 16 |
)
|
| 17 |
|
| 18 |
def rewrite_email(user_text):
|
| 19 |
if not user_text.strip():
|
| 20 |
-
return "Please enter
|
| 21 |
|
| 22 |
-
#
|
| 23 |
messages = [
|
| 24 |
{
|
| 25 |
"role": "system",
|
| 26 |
-
"content": "You are a professional
|
| 27 |
},
|
| 28 |
{"role": "user", "content": user_text}
|
| 29 |
]
|
| 30 |
|
| 31 |
-
# Generate
|
| 32 |
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 33 |
-
outputs = pipe(prompt, max_new_tokens=
|
| 34 |
|
| 35 |
-
# Extract
|
| 36 |
-
|
| 37 |
-
return full_output.split("<|im_start|>assistant")[-1].strip()
|
| 38 |
|
| 39 |
-
# 2. GRADIO
|
| 40 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 41 |
-
gr.Markdown("# 📧
|
| 42 |
-
gr.Markdown("
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
-
input_text = gr.Textbox(label="
|
| 46 |
-
output_text = gr.Textbox(label="Professional Version", interactive=False, lines=
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 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()
|