| import os |
| import subprocess |
| import sys |
| import warnings |
| warnings.filterwarnings("ignore") |
|
|
|
|
| def run_pip(*args): |
| """Run a pip install command and raise on failure.""" |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir"] + list(args)) |
|
|
|
|
| |
|
|
| print("=== Installing gradio (if needed) ===") |
| try: |
| import gradio |
| print("gradio already installed.") |
| except ImportError: |
| run_pip("gradio") |
|
|
| print("=== Installing torch (CPU-only, ~190 MB) ===") |
| try: |
| import torch |
| print("torch already installed.") |
| except ImportError: |
| run_pip("torch", "--index-url", "https://download.pytorch.org/whl/cpu") |
|
|
| print("=== Installing transformers 4.46.3 ===") |
| |
| try: |
| import transformers as _tf |
| if _tf.__version__ != "4.46.3": |
| raise ImportError("wrong version") |
| print("transformers 4.46.3 already installed.") |
| except (ImportError, AttributeError): |
| run_pip("transformers==4.46.3") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import transformers.utils.hub as _t_hub |
| import requests as _requests |
|
|
| _t_hub.get_session = lambda: _requests.Session() |
| print("Patched transformers.utils.hub.get_session β requests.Session()") |
|
|
| |
|
|
| import gradio as gr |
| import torch |
| from transformers import pipeline |
|
|
| |
|
|
| DEFAULT_MODEL = "sshleifer/distilbart-cnn-6-6" |
|
|
| AVAILABLE_MODELS = { |
| "sshleifer/distilbart-cnn-6-6": "Fast & light, good for general summarization", |
| "facebook/bart-large-cnn": "Larger BART model, better detail retention", |
| "google/pegasus-cnn_dailymail": "Pegasus model for high-quality summarization", |
| "allenai/led-base-16384": "Handles longer scientific documents", |
| } |
|
|
| print(f"Loading default model: {DEFAULT_MODEL}") |
| summarizer = pipeline("summarization", model=DEFAULT_MODEL, device=-1) |
|
|
| EXAMPLE_TEXTS = { |
| "news_article": ( |
| "In a historic move, global leaders have agreed to phase out fossil fuels over the next two " |
| "decades. This landmark decision came after weeks of intense negotiations during the international " |
| "climate summit. Experts believe this will drastically cut carbon emissions and pave the way for " |
| "sustainable energy sources worldwide. Countries will now be held accountable through annual " |
| "environmental reviews." |
| ), |
| "scientific_abstract": ( |
| "The rise of antibiotic-resistant bacteria poses one of the most significant threats to global " |
| "health in the 21st century. Recent studies have shown that the overuse and misuse of antibiotics " |
| "in both human medicine and agriculture have accelerated the evolution of resistant strains. In " |
| "this review, we summarize current research on bacterial resistance mechanisms, including horizontal " |
| "gene transfer and biofilm formation. We also explore novel approaches to combating resistance, " |
| "such as bacteriophage therapy, antimicrobial peptides, and CRISPR-based gene editing technologies. " |
| "The paper further outlines a strategic framework for integrating surveillance, policy reforms, and " |
| "public health initiatives to curb the spread of resistance. While scientific innovation holds " |
| "promise, global cooperation and responsible antibiotic stewardship remain essential to preventing " |
| "a post-antibiotic era where common infections could once again become deadly." |
| ), |
| "business_report": ( |
| "The company reported a 32% increase in quarterly revenue, largely driven by the success of its " |
| "latest AI-powered product line. International markets, particularly in Asia and Europe, showed " |
| "strong adoption rates. Leadership announced plans to reinvest earnings into R&D and global " |
| "expansion, while shareholders reacted positively with a 15% spike in stock prices." |
| ), |
| } |
|
|
|
|
| def summarize_text(text, model_name, summary_length, num_beams): |
| if not text.strip(): |
| return "Please provide some text to summarize." |
| try: |
| global summarizer |
| summarizer = pipeline("summarization", model=model_name, device=-1) |
| length_mapping = { |
| "very_short": (30, 50), |
| "short": (50, 70), |
| "medium": (70, 100), |
| "long": (100, 130), |
| } |
| min_len, max_len = length_mapping.get(summary_length, (70, 100)) |
| result = summarizer( |
| text, |
| max_length=int(max_len), |
| min_length=int(min_len), |
| num_beams=int(num_beams), |
| do_sample=False, |
| ) |
| return result[0]["summary_text"] |
| except Exception as exc: |
| return f"Error: {exc}" |
|
|
|
|
| def count_words(text): |
| return f"{len(text.split())} words" |
|
|
|
|
| def paste_example(example_type): |
| return EXAMPLE_TEXTS.get(example_type, "") |
|
|
|
|
| |
|
|
| with gr.Blocks(title="Multimodel Summarization App", theme=gr.themes.Soft()) as demo: |
| gr.Markdown("# π Multimodel Text Summarization") |
| gr.Markdown( |
| "Summarize news, reports, or scientific content using various models like BART, Pegasus, or LED." |
| ) |
|
|
| with gr.Row(): |
| with gr.Column(scale=3): |
| text_input = gr.Textbox( |
| lines=12, |
| label="Text to Summarize", |
| placeholder="Paste or type your text here...", |
| elem_id="text_input", |
| ) |
| word_counter = gr.Markdown("0 words") |
| text_input.change(count_words, inputs=[text_input], outputs=[word_counter]) |
|
|
| with gr.Row(): |
| example_dropdown = gr.Dropdown( |
| choices=list(EXAMPLE_TEXTS.keys()), |
| value=None, |
| label="Load Example Text", |
| ) |
| example_load_btn = gr.Button("Load Example") |
|
|
| with gr.Row(): |
| model_choice = gr.Dropdown( |
| choices=list(AVAILABLE_MODELS.keys()), |
| value=DEFAULT_MODEL, |
| label="Select Summarization Model", |
| ) |
| model_info = gr.Markdown(f"**Model info:** {AVAILABLE_MODELS[DEFAULT_MODEL]}") |
|
|
| with gr.Row(): |
| summary_length = gr.Radio( |
| choices=["very_short", "short", "medium", "long"], |
| value="medium", |
| label="Summary Length", |
| ) |
| num_beams = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="Beam Size") |
|
|
| summarize_button = gr.Button("Generate Summary", variant="primary", size="lg") |
|
|
| with gr.Column(scale=2): |
| gr.Markdown("### Summary Result") |
| summary_output = gr.Textbox( |
| label="Generated Summary", |
| lines=12, |
| placeholder="Your summary will appear here...", |
| ) |
|
|
| model_choice.change( |
| fn=lambda x: f"**Model info:** {AVAILABLE_MODELS.get(x, 'Custom model')}", |
| inputs=[model_choice], |
| outputs=[model_info], |
| ) |
| example_load_btn.click(fn=paste_example, inputs=[example_dropdown], outputs=[text_input]) |
| summarize_button.click( |
| fn=summarize_text, |
| inputs=[text_input, model_choice, summary_length, num_beams], |
| outputs=[summary_output], |
| ) |
|
|
| gr.Markdown(""" |
| --- |
| β
Choose from different summarization models |
| β
Works great for academic, news, or business content |
| β
Customize summary length and beam search for better quality |
| Built using Gradio and Hugging Face Transformers |
| """) |
|
|
| if __name__ == "__main__": |
| demo.launch() |