Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +43 -0
- requirements.txt +15 -0
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
|
| 6 |
+
MODEL_ID = os.getenv("MODEL_ID", "mistralai/Mistral-7B-Instruct-v0.3")
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_ID,
|
| 12 |
+
token=HF_TOKEN,
|
| 13 |
+
torch_dtype="auto",
|
| 14 |
+
device_map="auto",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def draft_reply(subject, thread):
|
| 18 |
+
system = (
|
| 19 |
+
"You are an email assistant. Draft a reply email.\n"
|
| 20 |
+
"- Be clear and polite.\n"
|
| 21 |
+
"- Ask up to 2 clarifying questions if needed.\n"
|
| 22 |
+
"- Do not invent facts.\n"
|
| 23 |
+
"- Output ONLY the email body.\n"
|
| 24 |
+
)
|
| 25 |
+
user = f"Subject: {subject}\n\nEmail thread:\n{thread}\n\nWrite the reply now."
|
| 26 |
+
|
| 27 |
+
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
|
| 28 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 30 |
+
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model.generate(**inputs, max_new_tokens=250, temperature=0.4, do_sample=True)
|
| 33 |
+
|
| 34 |
+
return tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 35 |
+
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=draft_reply,
|
| 38 |
+
inputs=[gr.Textbox(label="Subject"), gr.Textbox(label="Email Thread", lines=10)],
|
| 39 |
+
outputs=gr.Textbox(label="Draft Reply", lines=12),
|
| 40 |
+
title="Email Reply Drafting Assistant",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# torch
|
| 2 |
+
# transformers>=4.45.0
|
| 3 |
+
# accelerate>=0.33.0
|
| 4 |
+
# safetensors>=0.4.3
|
| 5 |
+
# huggingface_hub>=0.24.0
|
| 6 |
+
# sentencepiece>=0.2.0
|
| 7 |
+
# python-dotenv>=1.0.1
|
| 8 |
+
|
| 9 |
+
gradio
|
| 10 |
+
torch
|
| 11 |
+
transformers>=4.45.0
|
| 12 |
+
accelerate>=0.33.0
|
| 13 |
+
safetensors>=0.4.3
|
| 14 |
+
huggingface_hub>=0.24.0
|
| 15 |
+
sentencepiece>=0.2.0
|