Spaces:
Runtime error
Runtime error
File size: 2,325 Bytes
d278afb | 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 | # 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
|