""" app.py - the main module for the gradio app for summarization Usage: app.py [-h] [--share] [-m MODEL] [-nb ADD_BEAM_OPTION] [-batch TOKEN_BATCH_OPTION] [-level {DEBUG,INFO,WARNING,ERROR}] Details: python app.py --help Environment Variables: USE_TORCH (str): whether to use torch (1) or not (0) TOKENIZERS_PARALLELISM (str): whether to use parallelism (true) or not (false) Optional Environment Variables: APP_MAX_WORDS (int): the maximum number of words to use for summarization APP_OCR_MAX_PAGES (int): the maximum number of pages to use for OCR """ import argparse import contextlib import gc import logging import os import pprint as pp import random import re import sys import time from pathlib import Path os.environ["USE_TORCH"] = "1" os.environ["TOKENIZERS_PARALLELISM"] = "false" logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s - %(message)s", datefmt="%Y-%b-%d %H:%M:%S", ) import gradio as gr import nltk import torch from cleantext import clean from doctr.models import ocr_predictor from aggregate import BatchAggregator from pdf2text import convert_PDF_to_Text from summarize import load_model_and_tokenizer, summarize_via_tokenbatches from utils import ( contraction_aware_tokenize, extract_batches, load_example_filenames, remove_stagnant_files, remove_stopwords, saves_summary, textlist2html, truncate_word_count, ) _here = Path(__file__).parent nltk.download("punkt", force=True, quiet=True) nltk.download("popular", force=True, quiet=True) # Constants & Globals MODEL_OPTIONS = [ "pszemraj/long-t5-tglobal-base-16384-book-summary", "pszemraj/long-t5-tglobal-base-sci-simplify", "pszemraj/long-t5-tglobal-base-sci-simplify-elife", "pszemraj/long-t5-tglobal-base-16384-booksci-summary-v1", "pszemraj/pegasus-x-large-book-summary", ] # models users can choose from BEAM_OPTIONS = [2, 3, 4] # beam sizes users can choose from TOKEN_BATCH_OPTIONS = [ 1024, 1536, 2048, 2560, 3072, ] # token batch sizes users can choose from SUMMARY_PLACEHOLDER = "
Output will appear below:
" AGGREGATE_MODEL = "MBZUAI/LaMini-Flan-T5-783M" # model to use for aggregation # if duplicating space: uncomment this line to adjust the max words # os.environ["APP_MAX_WORDS"] = str(2048) # set the max words to 2048 # os.environ["APP_OCR_MAX_PAGES"] = str(40) # set the max pages to 40 # os.environ["APP_AGG_FORCE_CPU"] = str(1) # force cpu for aggregation aggregator = BatchAggregator( AGGREGATE_MODEL, force_cpu=os.environ.get("APP_AGG_FORCE_CPU", False) ) def aggregate_text( summary_text: str, text_file: gr.inputs.File = None, ) -> str: """ Aggregate the text from the batches. NOTE: you should probably include the BatchAggregator object as a fn arg if using this code :param batches_html: The batches to aggregate, in html format :param text_file: The text file to append the aggregate summary to :return: The aggregate summary in html format """ if summary_text is None or summary_text == SUMMARY_PLACEHOLDER: logging.error("No text provided. Make sure a summary has been generated first.") return "Error: No text provided. Make sure a summary has been generated first." try: extracted_batches = extract_batches(summary_text) except Exception as e: logging.info(summary_text) logging.info(f"the batches html is: {type(summary_text)}") return f"Error: unable to extract batches - check input: {e}" if not extracted_batches: logging.error("unable to extract batches - check input") return "Error: unable to extract batches - check input" out_path = None if text_file is not None: out_path = text_file.name # assuming name attribute stores the file path content_batches = [batch["content"] for batch in extracted_batches] full_summary = aggregator.infer_aggregate(content_batches) # if a path that exists is provided, append the summary with markdown formatting if out_path: out_path = Path(out_path) try: with open(out_path, "a", encoding="utf-8") as f: f.write("\n\n## Aggregate Summary\n\n") f.write( "- This is an instruction-based LLM aggregation of the previous 'summary batches'.\n" ) f.write(f"- Aggregation model: {aggregator.model_name}\n\n") f.write(f"{full_summary}\n\n") logging.info(f"Updated {out_path} with aggregate summary") except Exception as e: logging.error(f"unable to update {out_path} with aggregate summary: {e}") full_summary_html = f"""{full_summary}
Input text was truncated to {max_input_length} words. That's about {100*max_input_length/input_wc:.2f}% of the original text.
Dropping stopwords is set to {predrop_stopwords}. If this is not what you intended, please validate the advanced settings.
Input text is too short to summarize. Detected {len(input_text)} characters. Please load text by selecting an example from the dropdown menu or by pasting text into the text box.
Runtime: {rt} minutes with model: {model_name}
" if msg is not None: html += msg html += "" settings["remove_stopwords"] = predrop_stopwords settings["model_name"] = model_name saved_file = saves_summary(summarize_output=_summaries, outpath=None, **settings) return html, full_summary, scores_out, saved_file def load_single_example_text( example_path: str or Path, max_pages: int = 20, ) -> str: """ load_single_example_text - loads a single example text file :param strorPath example_path: name of the example to load :param int max_pages: the maximum number of pages to load from a PDF :return str: the text of the example """ global name_to_path, ocr_model full_ex_path = name_to_path[example_path] full_ex_path = Path(full_ex_path) if full_ex_path.suffix in [".txt", ".md"]: with open(full_ex_path, "r", encoding="utf-8", errors="ignore") as f: raw_text = f.read() text = clean(raw_text, lower=False) elif full_ex_path.suffix == ".pdf": logging.info(f"Loading PDF file {full_ex_path}") max_pages = int(os.environ.get("APP_OCR_MAX_PAGES", max_pages)) logging.info(f"max_pages set to: {max_pages}") conversion_stats = convert_PDF_to_Text( full_ex_path, ocr_model=ocr_model, max_pages=max_pages, ) text = conversion_stats["converted_text"] else: logging.error(f"Unknown file type {full_ex_path.suffix}") text = "ERROR - check example path" return text def load_uploaded_file(file_obj, max_pages: int = 20, lower: bool = False) -> str: """ load_uploaded_file - loads a file uploaded by the user :param file_obj (POTENTIALLY list): Gradio file object inside a list :param int max_pages: the maximum number of pages to load from a PDF :param bool lower: whether to lowercase the text :return str: the text of the file """ global ocr_model logger = logging.getLogger(__name__) # check if mysterious file object is a list if isinstance(file_obj, list): file_obj = file_obj[0] file_path = Path(file_obj.name) try: logger.info(f"Loading file:\t{file_path}") if file_path.suffix in [".txt", ".md"]: with open(file_path, "r", encoding="utf-8", errors="ignore") as f: raw_text = f.read() text = clean(raw_text, lower=lower) elif file_path.suffix == ".pdf": logger.info(f"loading a PDF file: {file_path.name}") max_pages = int(os.environ.get("APP_OCR_MAX_PAGES", max_pages)) logger.info(f"max_pages is: {max_pages}. Starting conversion...") conversion_stats = convert_PDF_to_Text( file_path, ocr_model=ocr_model, max_pages=max_pages, ) text = conversion_stats["converted_text"] else: logger.error(f"Unknown file type:\t{file_path.suffix}") text = "ERROR - check file - unknown file type. PDF, TXT, and MD are supported." return text except Exception as e: logger.error(f"Trying to load file:\t{file_path},\nerror:\t{e}") return f"Error: Could not read file {file_path.name}. Make sure it is a PDF, TXT, or MD file." def parse_args(): """arguments for the command line interface""" parser = argparse.ArgumentParser( description="Document Summarization with Long-Document Transformers - Demo", formatter_class=argparse.ArgumentDefaultsHelpFormatter, epilog="Runs a local-only web UI to summarize documents. pass --share for a public link to share.", ) parser.add_argument( "--share", dest="share", action="store_true", help="Create a public link to share", ) parser.add_argument( "-m", "--model", type=str, default=None, help=f"Add a custom model to the list of models: {pp.pformat(MODEL_OPTIONS, compact=True)}", ) parser.add_argument( "-nb", "--add_beam_option", type=int, default=None, help=f"Add a beam search option to the demo UI options, default: {pp.pformat(BEAM_OPTIONS, compact=True)}", ) parser.add_argument( "-batch", "--token_batch_option", type=int, default=None, help=f"Add a token batch size to the demo UI options, default: {pp.pformat(TOKEN_BATCH_OPTIONS, compact=True)}", ) parser.add_argument( "-max_agg", "-2x", "--aggregator_beam_boost", dest="aggregator_beam_boost", action="store_true", help="Double the number of beams for the aggregator during beam search", ) parser.add_argument( "-level", "--log_level", type=str, default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR"], help="Set the logging level", ) return parser.parse_args() if __name__ == "__main__": """main - the main function of the app""" logger = logging.getLogger(__name__) args = parse_args() logger.setLevel(args.log_level) logger.info(f"args: {pp.pformat(args.__dict__, compact=True)}") # add any custom options if args.model is not None: logger.info(f"Adding model {args.model} to the list of models") MODEL_OPTIONS.append(args.model) if args.add_beam_option is not None: logger.info(f"Adding beam search option {args.add_beam_option} to the list") BEAM_OPTIONS.append(args.add_beam_option) if args.token_batch_option is not None: logger.info(f"Adding token batch option {args.token_batch_option} to the list") TOKEN_BATCH_OPTIONS.append(args.token_batch_option) if args.aggregator_beam_boost: logger.info("Doubling aggregator num_beams") _agg_cfg = aggregator.get_generation_config() _agg_cfg["num_beams"] = _agg_cfg["num_beams"] * 2 aggregator.update_generation_config(**_agg_cfg) logger.info("Loading OCR model") with contextlib.redirect_stdout(None): ocr_model = ocr_predictor( "db_resnet50", "crnn_mobilenet_v3_large", pretrained=True, assume_straight_pages=True, ) # load the examples name_to_path = load_example_filenames(_here / "examples") logger.info(f"Loaded {len(name_to_path)} examples") demo = gr.Blocks(title="Document Summarization with Long-Document Transformers") _examples = list(name_to_path.keys()) logger.info("Starting app instance") with demo: gr.Markdown("# Document Summarization with Long-Document Transformers") gr.Markdown( """An example use case for fine-tuned long document transformers. Model(s) are trained on [book summaries](https://hf.co/datasets/kmfoda/booksum). Architectures [in this demo](https://hf.co/spaces/pszemraj/document-summarization) are [LongT5-base](https://hf.co/pszemraj/long-t5-tglobal-base-16384-book-summary) and [Pegasus-X-Large](https://hf.co/pszemraj/pegasus-x-large-book-summary). **Want more performance? Run this demo from a free Google Colab GPU:**.Output will appear below:
") with gr.Column(): gr.Markdown("### Results & Scores") with gr.Row(): with gr.Column(variant="compact"): gr.Markdown( "Download the summary as a text file, with parameters and scores." ) text_file = gr.File( label="Download as Text File", file_count="single", type="file", interactive=False, ) with gr.Column(variant="compact"): gr.Markdown( "Scores **roughly** represent the summary quality as a measure of the model's 'confidence'. less-negative numbers (closer to 0) are better." ) summary_scores = gr.Textbox( label="Summary Scores", placeholder="Summary scores will appear here", ) with gr.Column(variant="panel"): gr.Markdown("### **Summary Output**") summary_text = gr.HTML( label="Summary", value="