Spaces:
Sleeping
Sleeping
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() | |