pdarleyjr's picture
Update Gradio to 4.44.1 and fix queue configuration
8fe7734
import gradio as gr
from transformers import pipeline
import torch
# Initialize the summarization pipeline with model caching
summarizer = pipeline(
"summarization",
model="facebook/bart-large-cnn",
device=-1 # Use CPU for free-tier compatibility
)
def summarize_text(text, max_length=130, min_length=30):
"""
Summarize the input text using BART model.
Args:
text (str): Input text to summarize
max_length (int): Maximum length of the summary
min_length (int): Minimum length of the summary
Returns:
str: Generated summary
"""
try:
# Input validation
if not text or len(text.strip()) < 50:
return "Please provide a longer text (at least 50 characters) for summarization."
# Generate summary
summary = summarizer(
text,
max_length=max_length,
min_length=min_length,
do_sample=False
)
return summary[0]['summary_text']
except Exception as e:
return f"Error during summarization: {str(e)}"
# Create Gradio interface
iface = gr.Interface(
fn=summarize_text,
inputs=[
gr.Textbox(
lines=10,
placeholder="Enter text to summarize (minimum 50 characters)",
label="Input Text"
),
gr.Slider(
minimum=30,
maximum=250,
value=130,
step=10,
label="Maximum Summary Length"
),
gr.Slider(
minimum=20,
maximum=100,
value=30,
step=5,
label="Minimum Summary Length"
)
],
outputs=gr.Textbox(label="Summary"),
title="Text Summarizer (BART-large-CNN)",
description="""
This Space uses the BART-large-CNN model to generate concise summaries of your text.
The model works best with news articles and formal content.
For optimal results:
- Provide clear, well-structured text
- Aim for at least 100 words of input
- Adjust the length parameters based on your needs
""",
examples=[
[
"""The city council met on Tuesday to discuss the new recycling initiative.
The proposed program would require all residents to separate their waste into
three categories: organic, recyclable, and general waste. The council members
debated the costs and benefits of implementing such a system, with some arguing
that it would significantly reduce landfill usage while others expressed concerns
about the financial burden on residents. After three hours of discussion,
the council voted 7-3 in favor of implementing the program, which will begin
next spring.""",
130,
30
]
],
allow_flagging="never"
)
# Launch with specific configurations for Spaces
iface.queue() # Enable queuing
iface.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)