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