File size: 3,526 Bytes
2d427f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import gradio as gr
from transformers import pipeline
import torch

# Initialize the summarization pipeline with facebook/bart-large-cnn
summarizer = pipeline(
    "summarization",
    model="facebook/bart-large-cnn",
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    device=0 if torch.cuda.is_available() else -1
)

def summarize_text(text, max_length=150, min_length=30):
    """
    Summarize the input text using BART model
    """
    if not text.strip():
        return "Please provide some text to summarize."
    
    try:
        # Generate summary
        summary = summarizer(
            text,
            max_length=max_length,
            min_length=min_length,
            do_sample=False
        )
        return summary[0]['summary_text']
    except Exception as e:
        return f"Error occurred during summarization: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="Text Summarizer", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸ“ Text Summarizer")
    gr.Markdown("Powered by Facebook's BART-Large-CNN model from Hugging Face")
    
    with gr.Row():
        with gr.Column(scale=1):
            input_text = gr.Textbox(
                label="Input Text",
                placeholder="Enter the text you want to summarize...",
                lines=10,
                max_lines=15
            )
            
            with gr.Row():
                min_length_slider = gr.Slider(
                    minimum=10,
                    maximum=100,
                    value=30,
                    step=5,
                    label="Minimum Summary Length"
                )
                max_length_slider = gr.Slider(
                    minimum=50,
                    maximum=500,
                    value=150,
                    step=10,
                    label="Maximum Summary Length"
                )
            
            summarize_btn = gr.Button("Summarize Text", variant="primary")
            
        with gr.Column(scale=1):
            output_text = gr.Textbox(
                label="Summary",
                lines=8,
                max_lines=10,
                interactive=False
            )
    
    # Event handlers
    summarize_btn.click(
        fn=summarize_text,
        inputs=[input_text, max_length_slider, min_length_slider],
        outputs=output_text
    )
    
    # Example inputs
    gr.Examples(
        examples=[
            ["The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930."],
            ["Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence (AI) based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention. Machine learning algorithms build a model based on sample data, known as training data, in order to make predictions or decisions without being explicitly programmed to do so."]
        ],
        inputs=input_text,
        label="Example Texts"
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )