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