Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,104 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
MODEL_ID = 'lityops/Abstractive-Style-Summarizer'
|
| 5 |
-
|
| 6 |
-
summarizer = pipeline("summarization", model=MODEL_ID)
|
| 7 |
-
|
| 8 |
-
def generate_summary(text, style):
|
| 9 |
-
if not text or len(text.strip()) < 50:
|
| 10 |
-
return "Input must at least be 50 words long"
|
| 11 |
-
|
| 12 |
-
input_text = f"Summarize {style}: {text}"
|
| 13 |
-
input_words = len(text.split())
|
| 14 |
-
|
| 15 |
-
if style == 'Harsh':
|
| 16 |
-
max_len = int(input_words * 0.35)
|
| 17 |
-
min_len = 5
|
| 18 |
-
rep_penalty = 2.5
|
| 19 |
-
length_penalty = 1.5
|
| 20 |
-
beam_size = 4
|
| 21 |
-
max_cap = 120
|
| 22 |
-
elif style == 'Balanced':
|
| 23 |
-
max_len = int(input_words * 0.50)
|
| 24 |
-
min_len = 20
|
| 25 |
-
rep_penalty = 1.5
|
| 26 |
-
length_penalty = 1.2
|
| 27 |
-
beam_size = 4
|
| 28 |
-
max_cap = 180
|
| 29 |
-
else:
|
| 30 |
-
max_len = int(input_words * 0.70)
|
| 31 |
-
min_len = 50
|
| 32 |
-
rep_penalty = 1.2
|
| 33 |
-
length_penalty = 0.8
|
| 34 |
-
beam_size = 4
|
| 35 |
-
max_cap = 256
|
| 36 |
-
|
| 37 |
-
max_len = min(max_len, max_cap)
|
| 38 |
-
|
| 39 |
-
output = summarizer(
|
| 40 |
-
input_text,
|
| 41 |
-
max_length=max_len,
|
| 42 |
-
min_length=min_len,
|
| 43 |
-
num_beams=beam_size,
|
| 44 |
-
length_penalty=length_penalty,
|
| 45 |
-
repetition_penalty=rep_penalty,
|
| 46 |
-
no_repeat_ngram_size=3,
|
| 47 |
-
early_stopping=True
|
| 48 |
-
)
|
| 49 |
-
return output[0]["summary_text"]
|
| 50 |
-
|
| 51 |
-
custom_css = """
|
| 52 |
-
#header {text-align: center; margin-bottom: 25px;}
|
| 53 |
-
.gradio-container {max-width:
|
| 54 |
-
footer {display: none !important;}
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
with gr.Blocks(
|
| 58 |
-
with gr.Column(elem_id="header"):
|
| 59 |
-
gr.Markdown("# Style Summarizer")
|
| 60 |
-
gr.Markdown("Fine-tuned Flan-T5 model for multi-style document summarization.")
|
| 61 |
-
|
| 62 |
-
with gr.Row():
|
| 63 |
-
with gr.Column(scale=1):
|
| 64 |
-
input_box = gr.Textbox(
|
| 65 |
-
label="Input Text",
|
| 66 |
-
placeholder="Enter text to be summarized...",
|
| 67 |
-
lines=
|
| 68 |
-
)
|
| 69 |
-
style_radio = gr.Radio(
|
| 70 |
-
choices=["Harsh", "Balanced", "Detailed"],
|
| 71 |
-
label="Summary Type",
|
| 72 |
-
value="Balanced"
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
MODEL_ID = 'lityops/Abstractive-Style-Summarizer'
|
| 5 |
+
|
| 6 |
+
summarizer = pipeline("summarization", model=MODEL_ID)
|
| 7 |
+
|
| 8 |
+
def generate_summary(text, style):
|
| 9 |
+
if not text or len(text.strip()) < 50:
|
| 10 |
+
return "Input must at least be 50 words long"
|
| 11 |
+
|
| 12 |
+
input_text = f"Summarize {style}: {text}"
|
| 13 |
+
input_words = len(text.split())
|
| 14 |
+
|
| 15 |
+
if style == 'Harsh':
|
| 16 |
+
max_len = int(input_words * 0.35)
|
| 17 |
+
min_len = 5
|
| 18 |
+
rep_penalty = 2.5
|
| 19 |
+
length_penalty = 1.5
|
| 20 |
+
beam_size = 4
|
| 21 |
+
max_cap = 120
|
| 22 |
+
elif style == 'Balanced':
|
| 23 |
+
max_len = int(input_words * 0.50)
|
| 24 |
+
min_len = 20
|
| 25 |
+
rep_penalty = 1.5
|
| 26 |
+
length_penalty = 1.2
|
| 27 |
+
beam_size = 4
|
| 28 |
+
max_cap = 180
|
| 29 |
+
else:
|
| 30 |
+
max_len = int(input_words * 0.70)
|
| 31 |
+
min_len = 50
|
| 32 |
+
rep_penalty = 1.2
|
| 33 |
+
length_penalty = 0.8
|
| 34 |
+
beam_size = 4
|
| 35 |
+
max_cap = 256
|
| 36 |
+
|
| 37 |
+
max_len = min(max_len, max_cap)
|
| 38 |
+
|
| 39 |
+
output = summarizer(
|
| 40 |
+
input_text,
|
| 41 |
+
max_length=max_len,
|
| 42 |
+
min_length=min_len,
|
| 43 |
+
num_beams=beam_size,
|
| 44 |
+
length_penalty=length_penalty,
|
| 45 |
+
repetition_penalty=rep_penalty,
|
| 46 |
+
no_repeat_ngram_size=3,
|
| 47 |
+
early_stopping=True
|
| 48 |
+
)
|
| 49 |
+
return output[0]["summary_text"]
|
| 50 |
+
|
| 51 |
+
custom_css = """
|
| 52 |
+
#header {text-align: center; margin-bottom: 25px;}
|
| 53 |
+
.gradio-container {max-width: 95% !important;}
|
| 54 |
+
footer {display: none !important;}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
with gr.Column(elem_id="header"):
|
| 59 |
+
gr.Markdown("# Style Summarizer")
|
| 60 |
+
gr.Markdown("Fine-tuned Flan-T5 model for multi-style document summarization.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column(scale=1):
|
| 64 |
+
input_box = gr.Textbox(
|
| 65 |
+
label="Input Text",
|
| 66 |
+
placeholder="Enter text to be summarized...",
|
| 67 |
+
lines=15
|
| 68 |
+
)
|
| 69 |
+
style_radio = gr.Radio(
|
| 70 |
+
choices=["Harsh", "Balanced", "Detailed"],
|
| 71 |
+
label="Summary Type",
|
| 72 |
+
value="Balanced"
|
| 73 |
+
)
|
| 74 |
+
with gr.Row():
|
| 75 |
+
clear_btn = gr.Button("Clear Input")
|
| 76 |
+
submit_btn = gr.Button("Process Summary", variant="primary")
|
| 77 |
+
|
| 78 |
+
with gr.Column(scale=1):
|
| 79 |
+
output_box = gr.Textbox(
|
| 80 |
+
label="Output Summary",
|
| 81 |
+
lines=18,
|
| 82 |
+
interactive=False
|
| 83 |
+
)
|
| 84 |
+
copy_btn = gr.Button("Copy to Clipboard")
|
| 85 |
+
|
| 86 |
+
submit_btn.click(
|
| 87 |
+
fn=generate_summary,
|
| 88 |
+
inputs=[input_box, style_radio],
|
| 89 |
+
outputs=output_box
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
clear_btn.click(
|
| 93 |
+
fn=lambda: "",
|
| 94 |
+
inputs=None,
|
| 95 |
+
outputs=input_box
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
copy_btn.click(
|
| 99 |
+
fn=None,
|
| 100 |
+
inputs=[output_box],
|
| 101 |
+
js="(v) => { navigator.clipboard.writeText(v); }"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
demo.launch(css=custom_css, theme=gr.themes.Base())
|