Dannyar608 commited on
Commit
33d9919
·
verified ·
1 Parent(s): 357e3ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -1,22 +1,40 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Initialize the summarizer pipeline
5
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
6
 
7
- # Define the summarization function
8
- def summarize_text(text):
9
- summary = summarizer(text, max_length=200, min_length=100, do_sample=False)
10
- return summary[0]['summary_text']
 
 
 
 
 
 
 
 
 
11
 
12
- # Create the Gradio interface
13
  demo = gr.Interface(
14
  fn=summarize_text,
15
- inputs=gr.Textbox(placeholder="Enter your text here", label="Input Text"),
16
- outputs=gr.Textbox(label="Summary"),
17
- title="Text Summarizer",
18
- description="This chatbot takes a long text as input and returns a summary."
 
 
 
 
 
 
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
+