Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "daniB2112/bart-news-summarizer"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
| 9 |
+
|
| 10 |
+
def summarize(text, max_length=150, min_length=40):
|
| 11 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
| 12 |
+
summary_ids = model.generate(
|
| 13 |
+
inputs["input_ids"],
|
| 14 |
+
max_length=max_length,
|
| 15 |
+
min_length=min_length,
|
| 16 |
+
length_penalty=2.0,
|
| 17 |
+
num_beams=4,
|
| 18 |
+
early_stopping=True
|
| 19 |
+
)
|
| 20 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=summarize,
|
| 24 |
+
inputs=[
|
| 25 |
+
gr.Textbox(lines=10, placeholder="Paste news article here...", label="Article"),
|
| 26 |
+
gr.Slider(50, 300, value=150, label="Max Summary Length"),
|
| 27 |
+
gr.Slider(10, 100, value=40, label="Min Summary Length"),
|
| 28 |
+
],
|
| 29 |
+
outputs=gr.Textbox(label="Summary"),
|
| 30 |
+
title="BART News Summarizer",
|
| 31 |
+
description="Summarize news articles using daniB2112/bart-news-summarizer"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
demo.launch()
|