File size: 2,372 Bytes
0c9b99b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from level_1_text_summarization import get_pipeline, summarize_text

# --- 1. Load the model and pipeline at startup ---
# This ensures the model is loaded only once, not on every prediction.
print("Initializing the summarization pipeline for the Gradio app...")
summarizer_pipeline = get_pipeline()
print("Pipeline ready for the app.")

# --- 2. Define the prediction function ---
# This function will be called when the user interacts with the Gradio interface.
def generate_summary(input_text):
    """
    Takes raw text input and returns the summarized version.
    """
    if not summarizer_pipeline:
        return "Error: The summarization pipeline is not available."
    
    # Use the existing function to generate the summary
    summary = summarize_text(
        summarizer=summarizer_pipeline,
        text=input_text,
        max_length=150,  # Allow for longer summaries in the app
        min_length=30
    )
    
    if summary:
        return summary
    else:
        return "Error: Failed to generate summary. The input may be too short or an issue occurred."

# --- 3. Create and configure the Gradio interface ---
with gr.Blocks() as demo:
    gr.Markdown(
        """
        # 📝 Text Summarization with Hugging Face
        
        This demo uses the `sshleifer/distilbart-cnn-12-6` model to generate a concise summary of a long text document.
        
        **How to use:** Paste your text into the "Original Text" box and click "Summarize".
        """
    )
    
    with gr.Row():
        # Input component
        text_input = gr.Textbox(
            lines=15, 
            label="Original Text", 
            placeholder="Paste a long article or document here..."
        )
        # Output component
        summary_output = gr.Textbox(
            lines=15, 
            label="Summarized Text", 
            interactive=False
        )
        
    # Button to trigger the summarization
    summarize_button = gr.Button("Summarize")
    
    # Connect the button click to the prediction function
    summarize_button.click(
        fn=generate_summary,
        inputs=text_input,
        outputs=summary_output
    )

# --- 4. Launch the application ---
if __name__ == "__main__":
    print("Launching Gradio app...")
    # The app will be accessible at a local URL (e.g., http://127.0.0.1:7860)
    demo.launch()