import gradio as gr from transformers import pipeline # Load once at startup summarizer = pipeline("summarization", model="facebook/bart-large-cnn") text = """BART is a transformer encoder-encoder (seq2seq) model with a bidirectional (BERT-like) encoder and an autoregressive (GPT-like) decoder. BART is pre-trained by (1) corrupting text with an arbitrary noising function, and (2) learning a model to reconstruct the original text. BART is particularly effective when fine-tuned for text generation (e.g. summarization, translation) but also works well for comprehension tasks (e.g. text classification, question answering). This particular checkpoint has been fine-tuned on CNN Daily Mail, a large collection of text-summary pairs.""" # Summarize the text. the length here is in tokens summary = summarizer(text, min_length=10, max_length=100) # Code 5 - define a function to summarize text def nlp(input_text): summary = summarizer( input_text, repetition_penalty=5.0, # Increase this to discourage repetition length_penalty=0.3, # Decrease this to generate longer summaries min_length=20, max_length=100 ) return summary[0]["summary_text"] # Code 6 - UI object ui = gr.Interface(nlp, inputs=gr.Textbox(label="Input Text"), outputs=gr.Textbox(label="Summary"), title="Text Summarizer", description="Summarize your text using the BART model.") # Code 7 - launch UI ui.launch(share=True)