import gradio as gr from transformers import pipeline # Load the summarization pipeline # You can replace "sshleifer/distilbart-cnn-12-6" with another summarization model # from the Hugging Face Hub if you prefer. # A popular alternative is "bart-large-cnn". # See https://huggingface.co/models?pipeline_tag=summarization&sort=downloads for more options. try: summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") except Exception as e: print(f"Error loading model: {e}") print("Please ensure you have the 'torch' library installed (`pip install torch`) or try a different model.") summarizer = None # Set summarizer to None if model loading fails def summarize_text(text): """Summarizes the input text.""" if not summarizer: return "Model not loaded. Please check the console for errors." if not text: return "Please enter some text to summarize." try: # You can adjust max_length and min_length as needed summary = summarizer(text, max_length=150, min_length=40, do_sample=False)[0]['summary_text'] return summary except Exception as e: return f"An error occurred during summarization: {e}" # Create the Gradio interface if summarizer: interface = gr.Interface( fn=summarize_text, inputs=gr.Textbox(lines=10, label="Enter Text Here"), outputs=gr.Textbox(label="Summary"), title="Text Summarizer using Hugging Face and Gradio", description="Enter a long text and get a concise summary using a pre-trained model from Hugging Face." ) if __name__ == "__main__": interface.launch() else: print("Gradio interface not launched due to model loading error.")