Building an AI Text Summarizer App Using Python and Hugging Face

Follow on LinkedIn

Text summarization is one of the most practical applications of Natural Language Processing (NLP). From summarizing long articles to extracting key points from documents, AI-powered text summarizers help save time and improve readability. In this article, we will build a Text Summarizer App using Python and Hugging Face, capable of summarizing plain text, uploaded files, and webpage URLs.

This project uses pre-trained transformer models, which means we do not train any machine learning model from scratch. Instead, we focus on building a real-world AI application and integrating powerful NLP models into a user-friendly interface.

🎥 Watch the full video tutorial on YouTube:
👉AI Text Summarizer App Tutorial

AI Text Summarizer App Using Python and Hugging Face
AI Text Summarizer App Using Python and Hugging Face

What This Text Summarizer App Can Do

The application supports multiple input types, making it flexible for real-world usage:

  • Summarize plain text entered by the user
  • Summarize uploaded .txt or .md files
  • Summarize content directly from a website URL

The app uses Hugging Face transformer models to generate concise summaries while preserving the original meaning of the content.

Technology Stack Used

This project is built using the following technologies:

  • Python – core programming language
  • Streamlit – for building the web interface
  • Hugging Face Transformers – for AI-based text summarization
  • BeautifulSoup – for extracting text from webpages
  • Requests – for fetching website content

These tools together allow us to build a production-style AI application with minimal complexity.

How the Application Works

The workflow of the text summarizer app is straightforward:

  1. The user selects an input type: Text, File, or URL
  2. The input text is cleaned and truncated to a safe length
  3. A pre-trained Hugging Face summarization model processes the text
  4. The summarized output is displayed alongside the original content

The app also allows users to adjust summary length and enable creative sampling if needed.

Installation and Setup

Before running the application, make sure Python 3.9 or later is installed on your system.

Step 1: Create a Virtual Environment (Optional but Recommended)

python -m venv venv
venv\Scripts\activate   # Windows

Step 2: Install Required Dependencies

pip install streamlit transformers requests beautifulsoup4 torch

Step 3: Run the Application

streamlit run app.py

Once started, the app will open automatically in your browser.

Complete Working Code

from typing import Optional
import streamlit as st
from transformers import pipeline
import requests
from bs4 import BeautifulSoup
import os


def fetch_text_from_url(url: str) -> str:
    response = requests.get(url, timeout=10)
    soup = BeautifulSoup(response.text, "html.parser")
    paragraphs = [p.get_text() for p in soup.find_all("p")]
    return "\n\n".join(paragraphs)


def clean_and_truncate(text: str, max_chars: int = 20000) -> str:
    cleaned = " ".join(text.strip().split())
    if len(cleaned) > max_chars:
        cleaned = cleaned[:max_chars] + "..."
    return cleaned


class Summarizer:

    def __init__(self, model_name: str = "sshleifer/distilbart-cnn-12-6"):
        self.model_name = model_name
        self.pipeline = pipeline("summarization", model=model_name)

    def summarize(
        self,
        text: str,
        max_length: int = 150,
        min_length: int = 40,
        do_sample: bool = False
    ) -> str:
        result = self.pipeline(
            text,
            max_length=max_length,
            min_length=min_length,
            do_sample=do_sample
        )
        return result[0].get("summary_text", "")


def main():
    st.set_page_config(page_title="Text Summarizer App", layout="wide")
    st.title("Text Summarizer App - Python + Hugging Face")
    st.markdown("Summarize text, files, and webpages")

    left, right = st.columns([1, 2])

    with left:
        input_mode = st.radio("Input Type:", ["Text", "File", "URL"])

        model_choice = st.selectbox(
            "Model:",
            [
                "sshleifer/distilbart-cnn-12-6",
                "facebook/bart-large-cnn",
                "t5-small"
            ]
        )

        min_length = st.number_input(
            "Minimum Summary Tokens:",
            min_value=5,
            max_value=100,
            value=40
        )

        max_length = st.number_input(
            "Maximum Summary Tokens:",
            min_value=10,
            max_value=2000,
            value=150
        )

        do_sample = st.checkbox(
            "Use Sampling (creative summaries)",
            value=False
        )

        raw_text: Optional[str] = None
        uploaded_file = None
        url_input: Optional[str] = None

        if input_mode == "Text":
            raw_text = st.text_area("Enter text here:", height=250)

        elif input_mode == "File":
            uploaded_file = st.file_uploader(
                "Upload .txt or .md file",
                type=["txt", "md"]
            )

        elif input_mode == "URL":
            url_input = st.text_input("Enter webpage URL")

        summarize_btn = st.button("Summarize")

    with right:
        st.subheader("Original Text")
        source_container = st.empty()

        st.subheader("Summary Output")
        summary_container = st.empty()

        if summarize_btn:
            user_input_text = ""

            if input_mode == "Text" and raw_text:
                user_input_text = raw_text

            elif input_mode == "File" and uploaded_file:
                uploaded_bytes = uploaded_file.read()
                try:
                    user_input_text = uploaded_bytes.decode("utf-8")
                except:
                    user_input_text = uploaded_bytes.decode("latin-1")

            elif input_mode == "URL" and url_input:
                try:
                    user_input_text = fetch_text_from_url(url_input)
                except Exception as e:
                    st.error(f"Failed to fetch URL: {e}")
                    return

            if not user_input_text.strip():
                st.warning("Please provide valid input.")
                return

            user_input_text = clean_and_truncate(user_input_text)
            source_container.code(user_input_text[:8000])

            if min_length >= max_length:
                st.error("Minimum tokens must be smaller than Maximum tokens.")
                return

            summarizer = Summarizer(model_name=model_choice)

            with st.spinner("Generating summary..."):
                try:
                    summary_text = summarizer.summarize(
                        user_input_text,
                        max_length=max_length,
                        min_length=min_length,
                        do_sample=do_sample
                    )
                except Exception as e:
                    st.error(f"Summarization failed: {e}")
                    return

                if not summary_text.strip():
                    st.warning(
                        "The model returned an empty summary. "
                        "Try reducing minimum tokens or adding more text."
                    )
                    return

                summary_container.write(summary_text)


if __name__ == "__main__":
    main()

Final Thoughts

This Text Summarizer App demonstrates how powerful AI models can be integrated into real applications using Python and Hugging Face. By supporting text, file, and URL inputs, the app becomes highly practical for everyday use. This project is ideal for learning NLP concepts, building AI-powered tools, and showcasing your skills in a portfolio.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

×