Spaces:
Sleeping
Sleeping
File size: 1,939 Bytes
ff2aa7c 9b079ce ff2aa7c 9b079ce ff2aa7c 9b079ce ff2aa7c 9b079ce ff2aa7c 9b079ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import gradio as gr
from transformers import pipeline
# 1. Use a lighter model and GPU if available
summarizer = pipeline(
"summarization",
model="sshleifer/distilbart-cnn-12-6",
device=0 # set to -1 for CPU-only
)
def chunked_summary(text, chunk_size=800):
tokens = text.split()
chunks = [" ".join(tokens[i:i+chunk_size]) for i in range(0, len(tokens), chunk_size)]
summaries = [
summarizer(c, max_length=80, min_length=20, do_sample=False)[0]["summary_text"]
for c in chunks
]
return " ".join(summaries)
def classify_action(email_text):
email_lower = email_text.lower()
if "meeting" in email_lower or "schedule" in email_lower:
return "Schedule a meeting"
elif "question" in email_lower or "reply" in email_lower or "can you" in email_lower:
return "Reply"
elif "unsubscribe" in email_lower or "spam" in email_lower:
return "Delete or Mark as Spam"
else:
return "Read and Archive"
def summarize_and_recommend(email_text):
if not email_text.strip():
return "No content provided.", "No action"
# 2. Decide whether to chunk
word_count = len(email_text.split())
if word_count > 800:
summary = chunked_summary(email_text)
else:
summary = summarizer(
email_text,
max_length=80,
min_length=20,
do_sample=False
)[0]['summary_text']
action = classify_action(email_text)
return summary, action
iface = gr.Interface(
fn=summarize_and_recommend,
inputs=gr.Textbox(lines=15, placeholder="Paste your email here..."),
outputs=[
gr.Textbox(label="Summary"),
gr.Textbox(label="Suggested Action")
],
title="📩 Smart Email Summarizer & Action Recommender",
description="Faster summarization with a distilled model and length controls.",
)
iface.launch(server_name="0.0.0.0", server_port=7860)
|