Spaces:
Sleeping
Sleeping
File size: 3,322 Bytes
d83d604 5813702 73fdc8f 5813702 861709c 5813702 861709c 5813702 73fdc8f 5813702 0c65a3b 5813702 d83d604 5813702 d83d604 5813702 f96da32 5813702 |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Supported summarization models
model_choices = {
# 🥇 High Accuracy Models
"Pegasus (google/pegasus-xsum)": "google/pegasus-xsum",
"BigBird-Pegasus (google/bigbird-pegasus-large-arxiv)": "google/bigbird-pegasus-large-arxiv",
"LongT5 Large (google/long-t5-tglobal-large)": "google/long-t5-tglobal-large",
"BART Large CNN (facebook/bart-large-cnn)": "facebook/bart-large-cnn",
"ProphetNet (microsoft/prophetnet-large-uncased-cnndm)": "microsoft/prophetnet-large-uncased-cnndm",
"LED (allenai/led-base-16384)": "allenai/led-base-16384",
"T5 Large (t5-large)": "t5-large",
"Flan-T5 Large (google/flan-t5-large)": "google/flan-t5-large",
# ⚖️ Balanced (Speed vs Accuracy)
"DistilBART CNN (sshleifer/distilbart-cnn-12-6)": "sshleifer/distilbart-cnn-12-6",
"DistilBART XSum (mrm8488/distilbart-xsum-12-6)": "mrm8488/distilbart-xsum-12-6",
"T5 Base (t5-base)": "t5-base",
"Flan-T5 Base (google/flan-t5-base)": "google/flan-t5-base",
"BART CNN SamSum (philschmid/bart-large-cnn-samsum)": "philschmid/bart-large-cnn-samsum",
"T5 SamSum (knkarthick/pegasus-samsum)": "knkarthick/pegasus-samsum",
"LongT5 Base (google/long-t5-tglobal-base)": "google/long-t5-tglobal-base",
# ⚡ Lighter / Faster Models
"T5 Small (t5-small)": "t5-small",
"MBART (facebook/mbart-large-cc25)": "facebook/mbart-large-cc25",
"MarianMT (Helsinki-NLP/opus-mt-en-ro)": "Helsinki-NLP/opus-mt-en-ro", # not trained for summarization, just as placeholder
"Falcon Instruct (tiiuae/falcon-7b-instruct)": "tiiuae/falcon-7b-instruct", # general-purpose, not summarization-specific
"BART ELI5 (yjernite/bart_eli5)": "yjernite/bart_eli5" # trained for explain-like-I'm-5
}
# Cache for loaded models/tokenizers
model_cache = {}
def load_model(model_name):
if model_name not in model_cache:
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
model_cache[model_name] = (tokenizer, model)
return model_cache[model_name]
# Summarization function
def summarize_text(input_text, model_label):
if not input_text.strip():
return "Please enter some text."
model_name = model_choices[model_label]
tokenizer, model = load_model(model_name)
if "t5" in model_name.lower():
input_text = "summarize: " + input_text
inputs = tokenizer(input_text, return_tensors="pt", truncation=True)
summary_ids = model.generate(
inputs["input_ids"],
max_length=20, # Approximate for 65 characters
min_length=5,
do_sample=False
)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary[:65] # Ensure character limit
# Gradio UI
iface = gr.Interface(
fn=summarize_text,
inputs=[
gr.Textbox(lines=6, label="Enter text to summarize"),
gr.Dropdown(choices=list(model_choices.keys()), label="Choose summarization model", value="Pegasus (google/pegasus-xsum)")
],
outputs=gr.Textbox(lines=2, label="Summary (max 65 characters)"),
title="Short Text Summarizer",
description="Summarizes input text to under 65 characters using a selected model."
)
iface.launch()
|