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