File size: 7,209 Bytes
8f6e6b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import subprocess
import sys
import pkg_resources
import warnings
warnings.filterwarnings("ignore")

def install_package(package, version=None):
    package_spec = f"{package}=={version}" if version else package
    print(f"Installing {package_spec}...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", package_spec])
    except subprocess.CalledProcessError as e:
        print(f"Failed to install {package_spec}: {e}")
        raise

# Required packages
required_packages = {
    "gradio": None,
    "torch": None,
    "transformers": None
}

installed_packages = {pkg.key for pkg in pkg_resources.working_set}
for package, version in required_packages.items():
    if package not in installed_packages:
        install_package(package, version)

# Import packages
import gradio as gr
import torch
from transformers import pipeline

# Load default summarization model
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)

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, max_length=130, min_length=30):
    if not text.strip():
        return "Please provide some text to summarize."

    try:
        global summarizer
        summarizer = pipeline("summarization", model=model_name)

        length_mapping = {
            "very_short": (30, 50),
            "short": (50, 70),
            "medium": (70, 100),
            "long": (100, 130)
        }
        min_length, max_length = length_mapping.get(summary_length, (min_length, max_length))

        summary = summarizer(text, max_length=int(max_length), min_length=int(min_length), num_beams=int(num_beams), do_sample=False)
        return summary[0]['summary_text']
    except Exception as e:
        return f"Error: {str(e)}"

def count_words(text):
    return len(text.split())

def on_input_change(text):
    return f"{count_words(text)} 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...",
                show_label=True,
                elem_id="text_input"
            )
            word_counter = gr.Markdown("0 words", elem_id="word_counter")
            text_input.change(on_input_change, 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..."
            )
            copy_button = gr.Button("πŸ“‹ Copy to Clipboard", variant="secondary")

    # Events
    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()