Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,46 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if __name__ == "__main__":
|
| 54 |
-
app()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("Armandoliv/t5-small-summarizer-scitldr")
|
| 6 |
+
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Armandoliv/t5-small-summarizer-scitldr")
|
| 8 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
model = model.to(device)
|
| 10 |
+
|
| 11 |
+
def main_summarizer(text):
|
| 12 |
+
max_input_length = 1024
|
| 13 |
+
preprocess_text = text.strip().replace("\n"," ").replace("’", "'").strip()
|
| 14 |
+
tokenized_text = tokenizer.encode(preprocess_text, return_tensors="pt", truncation=True, max_length=max_input_length,).to(device)
|
| 15 |
+
|
| 16 |
+
summary_ids = model.generate(
|
| 17 |
+
tokenized_text,
|
| 18 |
+
max_length=256,
|
| 19 |
+
num_beams=8,
|
| 20 |
+
repetition_penalty=3.0,
|
| 21 |
+
length_penalty=2.5,
|
| 22 |
+
early_stopping=False
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
output = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
return output
|
| 28 |
+
|
| 29 |
+
inputs = [gr.Textbox(lines=10, placeholder="Text Here...", label="Input")]
|
| 30 |
+
outputs = gr.Text( label="Summary")
|
| 31 |
+
title="Text summarisation app"
|
| 32 |
+
description = "This demo uses AI Models to summarize long text.\nIt focus on scientific texts."
|
| 33 |
+
|
| 34 |
+
io = gr.Interface(fn=main_summarizer, inputs=inputs, outputs=outputs, title=title, description = description,
|
| 35 |
+
|
| 36 |
+
css= """.gr-button-primary { background: -webkit-linear-gradient(
|
| 37 |
+
90deg, #355764 0%, #55a8a1 100% ) !important; background: #355764;
|
| 38 |
+
background: linear-gradient(
|
| 39 |
+
90deg, #355764 0%, #55a8a1 100% ) !important;
|
| 40 |
+
background: -moz-linear-gradient( 90deg, #355764 0%, #55a8a1 100% ) !important;
|
| 41 |
+
background: -webkit-linear-gradient(
|
| 42 |
+
90deg, #355764 0%, #55a8a1 100% ) !important;
|
| 43 |
+
color:white !important}"""
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
io.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|