Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
import torch | |
# Initialize the summarization pipeline with facebook/bart-large-cnn | |
summarizer = pipeline( | |
"summarization", | |
model="facebook/bart-large-cnn", | |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, | |
device=0 if torch.cuda.is_available() else -1 | |
) | |
def summarize_text(text, max_length=150, min_length=30): | |
""" | |
Summarize the input text using BART model | |
""" | |
if not text.strip(): | |
return "Please provide some text to summarize." | |
try: | |
# 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 occurred during summarization: {str(e)}" | |
# Create Gradio interface | |
with gr.Blocks(title="Text Summarizer", theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# π Text Summarizer") | |
gr.Markdown("Powered by Facebook's BART-Large-CNN model from Hugging Face") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
input_text = gr.Textbox( | |
label="Input Text", | |
placeholder="Enter the text you want to summarize...", | |
lines=10, | |
max_lines=15 | |
) | |
with gr.Row(): | |
min_length_slider = gr.Slider( | |
minimum=10, | |
maximum=100, | |
value=30, | |
step=5, | |
label="Minimum Summary Length" | |
) | |
max_length_slider = gr.Slider( | |
minimum=50, | |
maximum=500, | |
value=150, | |
step=10, | |
label="Maximum Summary Length" | |
) | |
summarize_btn = gr.Button("Summarize Text", variant="primary") | |
with gr.Column(scale=1): | |
output_text = gr.Textbox( | |
label="Summary", | |
lines=8, | |
max_lines=10, | |
interactive=False | |
) | |
# Event handlers | |
summarize_btn.click( | |
fn=summarize_text, | |
inputs=[input_text, max_length_slider, min_length_slider], | |
outputs=output_text | |
) | |
# Example inputs | |
gr.Examples( | |
examples=[ | |
["The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930."], | |
["Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence (AI) based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention. Machine learning algorithms build a model based on sample data, known as training data, in order to make predictions or decisions without being explicitly programmed to do so."] | |
], | |
inputs=input_text, | |
label="Example Texts" | |
) | |
if __name__ == "__main__": | |
demo.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=False | |
) | |