Text_summarizer / app.py
MrSimple07's picture
Update app.py
a9c222c verified
import gradio as gr
from transformers import pipeline
# Initialize the BART summarization model from Hugging Face
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Define the function that will summarize the text
def summarize_text(input_text, max_length, min_length):
summary = summarizer(input_text, max_length=max_length, min_length=min_length, do_sample=False)
return summary[0]['summary_text']
# Example texts for users to try
example_texts = [
["The emergence of artificial intelligence (AI) has had a profound impact on many industries, including healthcare, finance, and transportation. AI has enabled automation of complex processes, improving efficiency and reducing costs. However, ethical considerations, such as privacy and job displacement, continue to be points of concern as the technology advances."],
["The Great Wall of China, a UNESCO World Heritage site, is one of the most iconic structures in the world. Originally built to protect against invasions, it spans thousands of miles across northern China. Today, the wall attracts millions of tourists each year who come to marvel at its history and grandeur."],
["Climate change is one of the most pressing issues of our time. Rising global temperatures, melting ice caps, and extreme weather events are just some of the impacts being observed. Scientists and policymakers are working together to find sustainable solutions to mitigate its effects and protect the planet for future generations."]
]
# Create a Gradio interface with the default theme
with gr.Blocks() as iface:
gr.Markdown("# πŸ“„ Text Summarization App")
gr.Markdown("This app generates a concise summary for long-form text (articles, reports, books, etc.). Adjust the sliders to control the length of the summary. Powered by Hugging Face's BART model.")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
lines=10,
label="Enter Text to Summarize",
placeholder="Type or paste your article here...",
elem_classes="input_textbox"
)
max_length = gr.Slider(
minimum=50,
maximum=200,
value=100,
step=10,
label="Max Length of Summary",
elem_classes="input_slider"
)
min_length = gr.Slider(
minimum=20,
maximum=100,
value=50,
step=10,
label="Min Length of Summary",
elem_classes="input_slider"
)
with gr.Column():
output_text = gr.Textbox(
label="Summarized Text",
elem_classes="output_textbox"
)
# Add examples
gr.Examples(
examples=example_texts,
inputs=[input_text],
examples_per_page=3
)
# Create the submit button
input_text.submit(
fn=summarize_text,
inputs=[input_text, max_length, min_length],
outputs=output_text
)
# Launch the app
iface.launch(
share=True,
server_port=7860,
server_name="0.0.0.0"
)