# Import necessary libraries import gradio as gr from transformers import pipeline # Load a pre-trained summarization model from Hugging Face # 'sshleifer/distilbart-cnn-12-6' is a good general-purpose summarization model # This model is suitable for summarizing various types of text. summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") def summarize_text(input_text): """ Summarizes the input text using a pre-trained model. Args: input_text (str): The text to be summarized. Returns: str: The summarized text or an error message. """ if not input_text or len(input_text.strip()) == 0: return "Please provide some text to summarize." try: # Summarize the input text # The summarizer pipeline can handle texts up to a certain length (model dependent). # For very long texts, you might need to implement chunking and summarize each chunk. # max_length and min_length control the summary length. Adjust these as needed. summary = summarizer(input_text, max_length=200, min_length=50, do_sample=False) # The pipeline returns a list of dictionaries, we need the 'summary_text' from the first item. return summary[0]['summary_text'] except Exception as e: # Catch potential errors during summarization (e.g., text too long for the model) return f"Error summarizing text: {e}" # Create the Gradio interface # The interface takes a text input (for the text to be summarized) # and provides a text output (for the summarized text) iface = gr.Interface( fn=summarize_text, inputs=gr.Textbox(label="Enter Text to Summarize", lines=10), # Use multiple lines for text input outputs=gr.Textbox(label="Summarized Text"), title="General Text Summarizer", description="Enter any text to get a summary." ) # To deploy on Hugging Face Spaces, you need this file (e.g., app.py) # and a requirements.txt file. Hugging Face Spaces will automatically run # the Gradio app if it finds an interface defined. # Remove the iface.launch() call when deploying to Hugging Face Spaces. # The last line should be the interface object itself. # iface.launch(share=True, debug=True) # Use this line for local testing iface # Use this line for Hugging Face Spaces deployment