Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# Initialize the summarizer
|
5 |
-
|
|
|
6 |
|
7 |
-
#
|
8 |
-
def summarize_text(text):
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
# Create
|
13 |
demo = gr.Interface(
|
14 |
fn=summarize_text,
|
15 |
-
inputs=
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
)
|
20 |
|
21 |
# Launch the interface
|
22 |
demo.launch()
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
from nltk.tokenize import sent_tokenize
|
4 |
|
5 |
+
# Initialize the summarizer pipelines (abstractive and extractive)
|
6 |
+
summarizer_abstractive = pipeline("summarization", model="facebook/bart-large-cnn")
|
7 |
+
summarizer_extractive = pipeline("summarization", model="bert-extractive-summarizer")
|
8 |
|
9 |
+
# Function to summarize text using the selected model
|
10 |
+
def summarize_text(text, model_type="Abstractive", max_length=150, min_length=50):
|
11 |
+
# Tokenize the text into sentences
|
12 |
+
sentences = sent_tokenize(text)
|
13 |
+
if model_type == "Abstractive":
|
14 |
+
# Process using the abstractive model (BART)
|
15 |
+
summary = summarizer_abstractive(text, max_length=max_length, min_length=min_length, do_sample=False)
|
16 |
+
return summary[0]['summary_text']
|
17 |
+
|
18 |
+
elif model_type == "Extractive":
|
19 |
+
# Process using the extractive model (BERT)
|
20 |
+
summary = summarizer_extractive(text)
|
21 |
+
return ' '.join([sentence['sentence'] for sentence in summary])
|
22 |
|
23 |
+
# Create Gradio Interface
|
24 |
demo = gr.Interface(
|
25 |
fn=summarize_text,
|
26 |
+
inputs=[
|
27 |
+
gr.Textbox(placeholder="Enter your long text here", label="Input Text", lines=10),
|
28 |
+
gr.Radio(choices=["Abstractive", "Extractive"], label="Summarization Method", value="Abstractive"),
|
29 |
+
gr.Slider(minimum=50, maximum=500, step=10, label="Max Length", value=150),
|
30 |
+
gr.Slider(minimum=10, maximum=150, step=5, label="Min Length", value=50),
|
31 |
+
],
|
32 |
+
outputs="text",
|
33 |
+
title="Advanced Text Summarizer",
|
34 |
+
description="This tool provides both abstractive and extractive summarization options, allowing you to select the best method and adjust summary length.",
|
35 |
+
live=True,
|
36 |
)
|
37 |
|
38 |
# Launch the interface
|
39 |
demo.launch()
|
40 |
+
|