Update app.py
Browse files
app.py
CHANGED
|
@@ -5,57 +5,57 @@ MODEL_ID = 'lityops/Style-Summarizer'
|
|
| 5 |
|
| 6 |
tokenizer = T5Tokenizer.from_pretrained(MODEL_ID)
|
| 7 |
summarizer = pipeline(
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
)
|
| 13 |
|
| 14 |
def generate_summary(text, style):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
demo = gr.Interface(
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
)
|
| 60 |
|
| 61 |
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
tokenizer = T5Tokenizer.from_pretrained(MODEL_ID)
|
| 7 |
summarizer = pipeline(
|
| 8 |
+
"summarization",
|
| 9 |
+
model=MODEL_ID,
|
| 10 |
+
tokenizer=tokenizer,
|
| 11 |
+
device=-1
|
| 12 |
)
|
| 13 |
|
| 14 |
def generate_summary(text, style):
|
| 15 |
+
if not text or len(text.strip()) < 50:
|
| 16 |
+
return "Input must at least be 50 words long"
|
| 17 |
+
|
| 18 |
+
input_text = f"summarize {style}: {text}"
|
| 19 |
+
input_words = len(text.split())
|
| 20 |
+
|
| 21 |
+
if style == 'harsh':
|
| 22 |
+
max_len = int(input_words * 0.35)
|
| 23 |
+
min_len = 5
|
| 24 |
+
rep_penalty = 2.5
|
| 25 |
+
beam_size = 4
|
| 26 |
+
elif style == 'standard':
|
| 27 |
+
max_len = int(input_words * 0.50)
|
| 28 |
+
min_len = 20
|
| 29 |
+
rep_penalty = 1.5
|
| 30 |
+
beam_size = 4
|
| 31 |
+
else:
|
| 32 |
+
max_len = int(input_words * 0.70)
|
| 33 |
+
min_len = 50
|
| 34 |
+
rep_penalty = 1.2
|
| 35 |
+
beam_size = 4
|
| 36 |
+
|
| 37 |
+
max_len = min(max_len, 256)
|
| 38 |
+
|
| 39 |
+
output = summarizer(
|
| 40 |
+
input_ids=input_text,
|
| 41 |
+
max_length=max_len,
|
| 42 |
+
min_length=min_len,
|
| 43 |
+
num_beams=beam_size,
|
| 44 |
+
repetition_penalty=rep_penalty,
|
| 45 |
+
no_repeat_ngram_size=3,
|
| 46 |
+
early_stopping=True
|
| 47 |
+
)
|
| 48 |
+
return output[0]["summary_text"]
|
| 49 |
|
| 50 |
demo = gr.Interface(
|
| 51 |
+
fn=generate_summary,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Textbox(label="Input Text", lines=10, placeholder="Paste text here..."),
|
| 54 |
+
gr.Radio(["harsh", "standard", "detailed"], label="Summary Style", value="standard")
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.Textbox(label="Summary"),
|
| 57 |
+
title="Style Summarizer",
|
| 58 |
+
description="A custom Flan-T5-Base model fine-tuned to generate summaries in different styles."
|
| 59 |
)
|
| 60 |
|
| 61 |
demo.launch()
|