Spaces:
Sleeping
Sleeping
File size: 15,497 Bytes
fc84fda fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 a0ac0fb fa5ed82 a0ac0fb fa5ed82 a0ac0fb fa5ed82 a0ac0fb fa5ed82 a0ac0fb fa5ed82 215b26b fa5ed82 215b26b fa5ed82 fc84fda fa5ed82 fc84fda a0ac0fb fc84fda a0ac0fb fc84fda a0ac0fb fc84fda fa5ed82 a0ac0fb fc84fda a0ac0fb fc84fda a0ac0fb 215b26b fa5ed82 a0ac0fb 215b26b fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 215b26b a0ac0fb fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 a0ac0fb fc84fda a0ac0fb fc84fda a0ac0fb fc84fda a0ac0fb fc84fda a0ac0fb fc84fda a0ac0fb fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 a0ac0fb fc84fda 935d87e fc84fda fa5ed82 fc84fda a0ac0fb fc84fda a0ac0fb fc84fda fa5ed82 fc84fda fa5ed82 a0ac0fb fc84fda a0ac0fb fc84fda fa5ed82 a0ac0fb fc84fda fa5ed82 fc84fda fa5ed82 935d87e a0ac0fb fa5ed82 fc84fda fa5ed82 fc84fda fa5ed82 fc84fda a0ac0fb fa5ed82 fc84fda |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
#!/usr/bin/env python3
"""
Gradio demo for T5 Email Summarizer with better preprocessing and separate fields
Deployed on HuggingFace Spaces with T4 GPU
"""
import gradio as gr
import torch
from transformers import T5ForConditionalGeneration, T5Tokenizer
import time
import re
# Load model and tokenizer
print("Loading T5 Email Summarizer model...")
model_name = "wordcab/t5-small-email-summarizer"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(
model_name,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
# Move to GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
model.eval()
print(f"Model loaded successfully on {device}!")
def normalize_titles(text):
"""
Normalize titles by removing periods to avoid tokenization issues.
This is a general solution that handles Mr. Ms. Dr. Prof. etc.
"""
# List of common titles that cause issues when followed by a period
titles_with_period = [
'Mr.', 'Ms.', 'Mrs.', 'Dr.', 'Prof.',
'Sr.', 'Jr.', 'Ph.D.', 'M.D.', 'B.A.', 'M.A.', 'B.S.', 'M.S.',
'Rev.', 'Hon.', 'Pres.', 'Gov.', 'Ofc.', 'Msgr.',
'Fr.', 'Br.', 'Sr.', 'Mx.'
]
normalized = text
for title in titles_with_period:
# Replace the title with period with the title without period
title_no_period = title.rstrip('.')
# Use word boundary to avoid replacing parts of words
normalized = re.sub(r'\b' + re.escape(title) + r'\b', title_no_period, normalized)
return normalized
def clean_unicode(text):
"""Clean up special unicode characters that can cause issues"""
# Normalize quotes
text = text.replace('"', '"').replace('"', '"')
text = text.replace(''', "'").replace(''', "'")
text = text.replace('β', '-').replace('β', '-')
text = text.replace('β¦', '...')
# Remove zero-width spaces and other invisible characters
text = re.sub(r'[\u200b\u200c\u200d\ufeff]', '', text)
return text
def preprocess_email(subject, body, mode="brief"):
"""
Preprocess email with general normalization
"""
# Clean unicode in both subject and body
if subject:
subject = clean_unicode(subject)
subject = normalize_titles(subject)
# For brief mode, simplify long subjects with names
# These confuse the model in brief mode
if mode == "brief" and len(subject) > 100:
# If it's a RE: or FW: with a long chain, try to simplify
if subject.startswith(('RE:', 'Re:', 'FW:', 'Fw:')):
# Extract key parts (hotel name, booking number, date)
parts = []
if 'Mia Saigon' in subject:
parts.append('Mia Saigon Hotel')
if 'birthday' in subject.lower() or 'Birthday' in subject:
parts.append('Birthday Celebration')
elif 'booking' in subject.lower():
parts.append('Booking')
# Extract date if present
import re
date_match = re.search(r'\d{1,2}\s+\w+\s+\d{4}', subject)
if date_match:
parts.append(date_match.group())
if parts:
subject = ' - '.join(parts)
else:
# Fallback: just take first 50 chars
subject = subject[:50] + '...'
if body:
body = clean_unicode(body)
body = normalize_titles(body)
# For brief mode, remove greeting lines that cause issues
if mode == "brief":
lines = body.strip().split('\n')
result_lines = []
skip_mode = False
for i, line in enumerate(lines):
line_stripped = line.strip()
# Check if this is a greeting line at the beginning
if i == 0 and line_stripped.lower().startswith(('dear', 'hi', 'hello', 'good morning', 'good afternoon', 'good evening')):
skip_mode = True
continue
# Skip empty lines right after greeting
if skip_mode and not line_stripped:
continue
# Once we hit real content, stop skipping
if line_stripped and skip_mode:
skip_mode = False
if not skip_mode:
result_lines.append(line)
if result_lines:
body = '\n'.join(result_lines).strip()
return subject, body
def summarize_email(subject, body, summary_type, temperature=0.7, max_length=150):
"""
Generate email summary based on selected type
"""
# Check if we have content
if not body and not subject:
return "Please enter email content (subject and/or body) to summarize.", 0, ""
# If only subject is provided
if subject and not body:
body = subject
subject = ""
start_time = time.time()
# Determine mode and parameters
if summary_type == "Brief (1-2 sentences)":
mode = "brief"
prefix = "summarize_brief:"
max_gen_length = 50
elif summary_type == "Full (detailed)":
mode = "full"
prefix = "summarize_full:"
max_gen_length = max_length
else: # Auto
# Use brief for short emails, full for longer ones
total_words = len((subject + " " + body).split())
if total_words < 100:
mode = "brief"
prefix = "summarize_brief:"
max_gen_length = 50
else:
mode = "full"
prefix = "summarize_full:"
max_gen_length = max_length
# Preprocess the email
original_subject = subject
original_body = body
processed_subject, processed_body = preprocess_email(subject, body, mode)
# Track what preprocessing was done
preprocessing_notes = []
if original_subject != processed_subject:
if len(original_subject) > 100 and len(processed_subject) < len(original_subject):
preprocessing_notes.append("Simplified long subject")
else:
preprocessing_notes.append("Normalized titles in subject")
if original_body != processed_body:
if original_body.lower().startswith(('dear', 'hi', 'hello')) and not processed_body.lower().startswith(('dear', 'hi', 'hello')):
preprocessing_notes.append("Removed greeting line")
else:
preprocessing_notes.append("Normalized titles in body")
# Format input for the model
if processed_subject:
input_text = f"{prefix} Subject: {processed_subject}. Body: {processed_body}"
else:
input_text = f"{prefix} Subject: Email. Body: {processed_body}"
# Tokenize
inputs = tokenizer(
input_text,
max_length=512,
truncation=True,
return_tensors="pt"
).to(device)
# Generate summary
with torch.no_grad():
outputs = model.generate(
**inputs,
max_length=max_gen_length,
min_length=10,
temperature=temperature,
do_sample=temperature > 0,
top_p=0.9,
num_beams=2 if temperature == 0 else 1,
early_stopping=True,
no_repeat_ngram_size=3
)
# Decode
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Calculate metrics
processing_time = time.time() - start_time
input_tokens = len(inputs['input_ids'][0])
output_tokens = len(outputs[0])
# Add metadata
metadata = f"\n\n---\nπ **Metrics:**\n"
metadata += f"- Processing time: {processing_time:.2f}s\n"
metadata += f"- Input tokens: {input_tokens}/512\n"
metadata += f"- Output tokens: {output_tokens}\n"
metadata += f"- Summary type: {mode.title()}\n"
# Note about preprocessing
if preprocessing_notes:
metadata += f"- Preprocessing: {', '.join(preprocessing_notes)}\n"
return summary, processing_time, metadata
# Example emails
examples = [
[
"Quarterly Budget Review Meeting",
"""Dear Team,
I hope this email finds you well. I wanted to remind everyone about our quarterly budget review meeting scheduled for next Tuesday, March 15th at 2:00 PM EST in Conference Room A.
Please come prepared with:
- Q1 expense reports
- Updated project timelines
- Resource allocation requests for Q2
We'll be discussing the 15% budget increase for digital marketing initiatives and the proposed headcount expansion for the engineering team.
If you cannot attend in person, please join via Zoom using the link in the calendar invite.
Best regards,
Sarah Johnson
Finance Director""",
"Auto-detect",
0.7,
150
],
[
"",
"""hey team,
quick update - cant make the meeting tmrw bc im stuck at the airport (flight delayed AGAIN ugh).
jim said we need to finalize teh proposal by friday or we'll miss the deadline... can someone take over? also dont forget to include the budget numbers from last months report.
btw has anyone seen my laptop charger? left it somewhere in the office yesterday lol
thx
mike""",
"Brief (1-2 sentences)",
0.7,
150
],
[
"Research Collaboration Opportunity",
"""Dear Dr. Williams,
I hope this message finds you well. I'm writing to follow up on our recent discussion about the research collaboration opportunity.
As we discussed, our lab has extensive experience in computational biology and we believe there could be significant synergies with your work in genomics. We have secured funding for a 3-year project and are looking for partners.
Would you be available for a call next week to discuss the details? I can share the full proposal and budget breakdown then.
Looking forward to your response.
Best regards,
Prof. Sarah Chen
Department of Computer Science""",
"Full (detailed)",
0.7,
150
]
]
# Create Gradio interface
with gr.Blocks(title="T5 Email Summarizer", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π§ T5 Email Summarizer - Brief & Full (v3)
This model can generate both **brief** (1-2 sentences) and **full** (detailed) summaries of emails.
It's robust to messy, informal text with typos and abbreviations.
**π§ v3 Updates:**
- Separate Subject/Body fields for better structure
- General title normalization (Mr. β Mr, Dr. β Dr, etc.)
- Improved unicode handling
- Better preprocessing for all edge cases
π€ **Model:** [wordcab/t5-small-email-summarizer](https://huggingface.co/wordcab/t5-small-email-summarizer)
| π **Dataset:** [argilla/FinePersonas-Conversations-Email-Summaries](https://huggingface.co/datasets/argilla/FinePersonas-Conversations-Email-Summaries)
| π **Running on:** CPU (Free tier)
""")
with gr.Row():
with gr.Column(scale=1):
subject_input = gr.Textbox(
label="π Subject Line (Optional)",
placeholder="e.g., Meeting Tomorrow, Project Update, etc.",
lines=1
)
body_input = gr.Textbox(
label="π Email Body",
placeholder="Paste or type your email content here...\n\nThe model handles formal/informal, clean/messy text equally well.",
lines=10
)
with gr.Row():
summary_type = gr.Radio(
choices=["Auto-detect", "Brief (1-2 sentences)", "Full (detailed)"],
value="Auto-detect",
label="π Summary Type"
)
with gr.Accordion("βοΈ Advanced Settings", open=False):
temperature = gr.Slider(
minimum=0,
maximum=1,
value=0.7,
step=0.1,
label="Temperature (0 = deterministic, 1 = creative)"
)
max_length = gr.Slider(
minimum=50,
maximum=200,
value=150,
step=10,
label="Max Length (for full summaries)"
)
summarize_btn = gr.Button("β¨ Generate Summary", variant="primary")
with gr.Column(scale=1):
output = gr.Textbox(
label="π Summary",
lines=8,
interactive=False
)
processing_time = gr.Number(
label="β±οΈ Processing Time (seconds)",
precision=2,
interactive=False,
visible=False
)
info_box = gr.Markdown(
label="π Processing Info",
value=""
)
gr.Markdown("### π‘ Try these examples:")
gr.Examples(
examples=examples,
inputs=[subject_input, body_input, summary_type, temperature, max_length],
outputs=[output, processing_time, info_box],
fn=summarize_email,
cache_examples=False
)
summarize_btn.click(
fn=summarize_email,
inputs=[subject_input, body_input, summary_type, temperature, max_length],
outputs=[output, processing_time, info_box]
)
gr.Markdown("""
---
### π How to use:
1. **Enter Subject** (optional) and **Email Body** separately for best results
2. **Select summary type** or use Auto-detect
3. **Click Generate Summary** to get your summary
### π― Features:
- **Dual-mode**: Get brief or detailed summaries on demand
- **Robust**: Handles typos, abbreviations, and informal language
- **Smart normalization**: Automatically handles titles (Mr., Dr., Prof., etc.)
- **Fast**: Processes emails quickly even on CPU
### π§ Preprocessing Features:
- **Title Normalization**: Converts "Mr." β "Mr", "Dr." β "Dr" to avoid tokenization issues
- **Unicode Cleaning**: Handles special quotes, dashes, and invisible characters
- **Smart Structure**: Separate subject/body fields for optimal processing
### π§ API Usage:
```python
from transformers import pipeline
summarizer = pipeline("summarization", model="wordcab/t5-small-email-summarizer")
# For production, normalize titles first:
import re
def normalize_titles(text):
titles = ['Mr.', 'Ms.', 'Dr.', 'Prof.']
for title in titles:
text = text.replace(title, title.rstrip('.'))
return text
email = normalize_titles(your_email)
# Brief summary
result = summarizer(f"summarize_brief: Subject: {subject}. Body: {body}")
# Full summary
result = summarizer(f"summarize_full: Subject: {subject}. Body: {body}")
```
### π Citation:
```bibtex
@misc{wordcab2025t5email,
title={T5 Email Summarizer - Brief & Full},
author={Wordcab Team},
year={2025},
publisher={HuggingFace}
}
```
""")
if __name__ == "__main__":
demo.launch() |