Spaces:
Sleeping
Sleeping
File size: 1,726 Bytes
384582b 33d9919 384582b 33d9919 384582b 33d9919 384582b 33d9919 384582b 33d9919 384582b ad7cdfc |
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 |
import gradio as gr
from transformers import pipeline
from nltk.tokenize import sent_tokenize
# Initialize the summarizer pipelines (abstractive and extractive)
summarizer_abstractive = pipeline("summarization", model="facebook/bart-large-cnn")
summarizer_extractive = pipeline("summarization", model="bert-extractive-summarizer")
# Function to summarize text using the selected model
def summarize_text(text, model_type="Abstractive", max_length=150, min_length=50):
# Tokenize the text into sentences
sentences = sent_tokenize(text)
if model_type == "Abstractive":
# Process using the abstractive model (BART)
summary = summarizer_abstractive(text, max_length=max_length, min_length=min_length, do_sample=False)
return summary[0]['summary_text']
elif model_type == "Extractive":
# Process using the extractive model (BERT)
summary = summarizer_extractive(text)
return ' '.join([sentence['sentence'] for sentence in summary])
# Create Gradio Interface
demo = gr.Interface(
fn=summarize_text,
inputs=[
gr.Textbox(placeholder="Enter your long text here", label="Input Text", lines=10),
gr.Radio(choices=["Abstractive", "Extractive"], label="Summarization Method", value="Abstractive"),
gr.Slider(minimum=50, maximum=500, step=10, label="Max Length", value=150),
gr.Slider(minimum=10, maximum=150, step=5, label="Min Length", value=50),
],
outputs="text",
title="Advanced Text Summarizer",
description="This tool provides both abstractive and extractive summarization options, allowing you to select the best method and adjust summary length.",
live=True,
)
# Launch the interface
demo.launch()
|