Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import T5ForConditionalGeneration, T5Tokenizer, PegasusForConditionalGeneration, PegasusTokenizer
|
|
|
|
| 3 |
import nltk
|
| 4 |
|
| 5 |
# Ensure that the NLTK sentence tokenizer is available
|
|
@@ -13,6 +14,9 @@ t5_tokenizer = T5Tokenizer.from_pretrained('t5-small')
|
|
| 13 |
pegasus_model = PegasusForConditionalGeneration.from_pretrained('google/pegasus-xsum')
|
| 14 |
pegasus_tokenizer = PegasusTokenizer.from_pretrained('google/pegasus-xsum')
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# Function to generate a summary using T5
|
| 17 |
def generate_t5_summary(text):
|
| 18 |
num_beams = 25 # Further increase beams for more diverse summaries
|
|
@@ -33,7 +37,7 @@ def generate_t5_summary(text):
|
|
| 33 |
|
| 34 |
# Function to generate a summary using PEGASUS
|
| 35 |
def generate_pegasus_summary(text):
|
| 36 |
-
num_beams =
|
| 37 |
length_penalty = 1.2
|
| 38 |
no_repeat_ngram_size = 2
|
| 39 |
max_length = 150
|
|
@@ -63,18 +67,19 @@ def generate_weighted_combined_summary(text, weight_t5=0.4, weight_pegasus=0.6):
|
|
| 63 |
combined_sentences.extend(t5_sentences[:int(len(t5_sentences) * weight_t5)])
|
| 64 |
combined_sentences.extend(pegasus_sentences[:int(len(pegasus_sentences) * weight_pegasus)])
|
| 65 |
|
| 66 |
-
#
|
| 67 |
combined_summary = " ".join(combined_sentences)
|
| 68 |
|
| 69 |
return combined_summary
|
| 70 |
|
| 71 |
# Define the Gradio interface
|
| 72 |
-
iface = gr.Interface(
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# Launch the
|
| 79 |
-
|
| 80 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import T5ForConditionalGeneration, T5Tokenizer, PegasusForConditionalGeneration, PegasusTokenizer
|
| 3 |
+
import evaluate
|
| 4 |
import nltk
|
| 5 |
|
| 6 |
# Ensure that the NLTK sentence tokenizer is available
|
|
|
|
| 14 |
pegasus_model = PegasusForConditionalGeneration.from_pretrained('google/pegasus-xsum')
|
| 15 |
pegasus_tokenizer = PegasusTokenizer.from_pretrained('google/pegasus-xsum')
|
| 16 |
|
| 17 |
+
# Load the ROUGE metric
|
| 18 |
+
rouge = evaluate.load("rouge")
|
| 19 |
+
|
| 20 |
# Function to generate a summary using T5
|
| 21 |
def generate_t5_summary(text):
|
| 22 |
num_beams = 25 # Further increase beams for more diverse summaries
|
|
|
|
| 37 |
|
| 38 |
# Function to generate a summary using PEGASUS
|
| 39 |
def generate_pegasus_summary(text):
|
| 40 |
+
num_beams = 20
|
| 41 |
length_penalty = 1.2
|
| 42 |
no_repeat_ngram_size = 2
|
| 43 |
max_length = 150
|
|
|
|
| 67 |
combined_sentences.extend(t5_sentences[:int(len(t5_sentences) * weight_t5)])
|
| 68 |
combined_sentences.extend(pegasus_sentences[:int(len(pegasus_sentences) * weight_pegasus)])
|
| 69 |
|
| 70 |
+
# Reorder sentences to maximize bigram overlap (use n-gram analysis if needed)
|
| 71 |
combined_summary = " ".join(combined_sentences)
|
| 72 |
|
| 73 |
return combined_summary
|
| 74 |
|
| 75 |
# Define the Gradio interface
|
| 76 |
+
iface = gr.Interface(
|
| 77 |
+
fn=generate_weighted_combined_summary,
|
| 78 |
+
inputs="textbox",
|
| 79 |
+
outputs="textbox",
|
| 80 |
+
title="Text Summarizer with T5 and PEGASUS",
|
| 81 |
+
description="Enter a text to generate its summary using a combined T5 and PEGASUS model."
|
| 82 |
+
)
|
| 83 |
|
| 84 |
+
# Launch the interface
|
| 85 |
+
iface.launch()
|
|
|