summarizer / app.py
anasmkh's picture
Create app.py
5ed8c5e verified
raw
history blame
1.81 kB
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import gradio as gr
# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
def summarize_text(text):
# Tokenize the input text
inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
# Generate summary
summary_ids = model.generate(
inputs["input_ids"],
num_beams=4,
max_length=200,
early_stopping=True
)
# Decode and return the summary
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
# Create Gradio interface
interface = gr.Interface(
fn=summarize_text,
inputs=gr.Textbox(lines=10, placeholder="Paste your text here...", label="Input Text"),
outputs=gr.Textbox(lines=5, label="Summary"),
title="Text Summarizer with BART-large-CNN",
description="Paste any long text and get a concise summary using Facebook's BART-large-CNN model.",
examples=[
["The Apollo program, also known as Project Apollo, was the third United States human spaceflight program carried out by the National Aeronautics and Space Administration (NASA), which succeeded in preparing and landing the first humans on the Moon from 1968 to 1972. It was first conceived during Dwight D. Eisenhower's administration as a three-person spacecraft to follow the one-person Project Mercury, which put the first Americans in space. Apollo was later dedicated to President John F. Kennedy's national goal of landing a man on the Moon and returning him safely to the Earth by the end of the 1960s, which he proposed in a May 25, 1961, address to Congress."]
]
)
# Launch the app
interface.launch()