import gradio as gr from transformers import BartForConditionalGeneration, BartTokenizer from rouge_score import rouge_scorer import torch import time import spaces # load model print(" Loading BART model...") model_name = "facebook/bart-large-cnn" tokenizer = BartTokenizer.from_pretrained(model_name) model = BartForConditionalGeneration.from_pretrained(model_name) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) print(f" Model loaded on {device}") # mian summary function @spaces.GPU def summarize_text(article, max_length, min_length, num_beams, length_penalty, no_repeat_ngram): if not article or not article.strip(): return "Please enter some text to summarize!", "", "", "" start_time = time.time() inputs = tokenizer.encode( article, return_tensors="pt", max_length=1024, truncation=True, padding=True ).to(device) input_tokens = inputs.shape[1] word_count = len(article.split()) generate_kwargs = { "max_length": max_length, "min_length": min_length, "length_penalty": length_penalty, "num_beams": num_beams, "early_stopping": True, } if no_repeat_ngram > 0: generate_kwargs["no_repeat_ngram_size"] = no_repeat_ngram summary_ids = model.generate(inputs, **generate_kwargs) summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) generation_time = time.time() - start_time summary_words = len(summary.split()) compression = round((1 - summary_words / word_count) * 100, 1) if word_count > 0 else 0 stats = f""" 📊 **Generation Stats** - Input Words: {word_count} - Input Tokens: {input_tokens} - Summary Words: {summary_words} - Compression: {compression}% - Time: {generation_time:.2f}s - Device: {device} """ return summary, stats, "", "" # Rouge eval fn def evaluate_rouge(reference, generated_summary): if not reference or not reference.strip() or not generated_summary or not generated_summary.strip(): return "Enter both a reference summary and generate a summary first!" scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True) scores = scorer.score(reference, generated_summary) result = f""" **ROUGE Scores** | Metric | Score | Meaning | |--------|-------|---------| | **ROUGE-1** | {scores['rouge1'].fmeasure:.3f} | Unigram overlap | | **ROUGE-2** | {scores['rouge2'].fmeasure:.3f} | Bigram overlap (fluency) | | **ROUGE-L** | {scores['rougeL'].fmeasure:.3f} | Longest common subsequence | *Range: 0 to 1, where 1 = perfect match with reference* """ return result # sample args samples = [ ["""Artificial Intelligence (AI) has rapidly evolved from a theoretical concept to a transformative force across industries. Machine learning, a subset of AI, enables systems to learn from data without explicit programming. Deep learning, which uses neural networks with many layers, has achieved breakthroughs in image recognition, natural language processing, and autonomous driving. Companies like Google, OpenAI, and DeepMind are pushing the boundaries of what AI can accomplish. However, ethical concerns regarding bias, privacy, and job displacement remain significant challenges. Governments worldwide are beginning to implement regulations to ensure responsible AI development. The future of AI promises both unprecedented opportunities and complex societal implications that require careful navigation.""", 150, 40, 4, 2.0, 3], ["""Climate change represents one of the most pressing challenges of our time. Rising global temperatures, driven primarily by greenhouse gas emissions from human activities, are causing widespread environmental disruption. Glaciers are melting at unprecedented rates, sea levels are rising, and extreme weather events are becoming more frequent and severe. The Intergovernmental Panel on Climate Change (IPCC) has warned that without immediate and substantial reductions in carbon emissions, global temperatures could rise by more than 1.5 degrees Celsius above pre-industrial levels within decades. This would trigger catastrophic consequences including mass species extinction, food and water shortages, and the displacement of millions of people. Transitioning to renewable energy sources, improving energy efficiency, and implementing sustainable agricultural practices are critical steps toward mitigating these impacts. International cooperation through agreements like the Paris Climate Accord is essential for coordinated global action.""", 150, 40, 4, 2.0, 3], ["""Space exploration has entered a new era of innovation and ambition. NASA's Artemis program aims to return humans to the Moon by the mid-2020s, establishing a sustainable presence that will serve as a stepping stone for future Mars missions. Meanwhile, private companies like SpaceX have revolutionized the industry with reusable rocket technology, dramatically reducing launch costs. The James Webb Space Telescope has begun sending back breathtaking images of distant galaxies, providing unprecedented insights into the early universe. Scientists are also making progress in the search for extraterrestrial life, with missions to Europa and Enceladus—moons with subsurface oceans—planned for the coming decade. These advancements not only expand our understanding of the cosmos but also drive technological innovation that benefits life on Earth, from satellite communications to medical research.""", 150, 40, 4, 2.0, 3] ] # gradio part with gr.Blocks() as demo: gr.Markdown(""" # 📝 AI Text Summarizer ### Powered by **BART (facebook/bart-large-cnn)** — Abstractive Summarization This app generates **abstractive summaries** using Facebook's BART model fine-tuned on CNN/DailyMail. Unlike extractive methods that copy sentences, it generates novel text that captures the essence. """) with gr.Row(): with gr.Column(scale=2): article_input = gr.Textbox( label="📄 Input Article", placeholder="Paste a long article or blog post here...", lines=12 ) with gr.Row(): generate_btn = gr.Button("🚀 Generate Summary", variant="primary", scale=2) clear_btn = gr.Button("🗑️ Clear", scale=1) with gr.Column(scale=1): gr.Markdown(""" ### ⚙️ Model Settings **Model:** `facebook/bart-large-cnn` **Architecture:** Bidirectional Encoder + Autoregressive Decoder **Parameters:** 406M **Max Input:** 1024 tokens """) max_length = gr.Slider(50, 300, value=150, step=10, label="Max Summary Length") min_length = gr.Slider(20, 100, value=40, step=5, label="Min Summary Length") num_beams = gr.Slider(2, 10, value=4, step=1, label="Beam Search Width") length_penalty = gr.Slider(0.5, 3.0, value=2.0, step=0.1, label="Length Penalty") no_repeat_ngram = gr.Slider(0, 5, value=3, step=1, label="No-Repeat N-Gram Size") gr.Markdown("---") with gr.Row(): with gr.Column(): summary_output = gr.Textbox( label="✨ Generated Summary", lines=6, interactive=False ) with gr.Column(): stats_output = gr.Markdown() gr.Markdown("---") with gr.Row(): with gr.Column(): gr.Markdown("### 📈 ROUGE Evaluation") gr.Markdown("Enter a reference summary to compute ROUGE scores against the generated summary.") reference_input = gr.Textbox( label="Reference Summary (Ground Truth)", placeholder="Paste a human-written reference summary here...", lines=4 ) evaluate_btn = gr.Button("📊 Calculate ROUGE Scores", variant="secondary") with gr.Column(): rouge_output = gr.Markdown() gr.Markdown("---") gr.Markdown("""