import datetime import os import time import logging import nltk import validators import streamlit as st from summarizer import summarizer_init, summarizer_summarize from config import MODELS from warnings import filterwarnings filterwarnings("ignore") from utils import ( clean_text, fetch_article_text, preprocess_text_for_abstractive_summarization, read_text_from_file, ) # summarizer = None # from rouge import Rouge logger = logging.getLogger(__name__) def initialize_app(): nltk.download("punkt") SESSION_DEFAULTS = { "model_type": "local", "model_name": "Boardpac summarizer v1", "summarizer_type": "Map Reduce", "is_parameters_changed":False, # "user_question":'', 'openai_api_key':'', } for k, v in SESSION_DEFAULTS.items(): if k not in st.session_state: st.session_state[k] = v # init_summarizer(st.session_state.model_name,api_key=None) @st.cache_resource def init_summarizer(model_name,api_key=None): with st.spinner( text="initialising the summarizer. This might take a few seconds ..." ): model_type = "local" if model_name == "OpenAI": model_type = "openai" model_path = MODELS[model_name] if model_type == "openai": #validation logic api_key = st.session_state.openai_api_key tokenizer,base_summarizer = summarizer_init(model_path,model_type,api_key) else: logger.info(f"Model for summarization : {model_path}") tokenizer,base_summarizer = summarizer_init(model_path, model_type) alert = st.success("summarizer initialised") time.sleep(1) # Wait for 1 seconds alert.empty() # Clear the alert return model_type, tokenizer, base_summarizer def update_parameters_change(): st.session_state.is_parameters_changed = True def parameters_change_button(model_name, summarizer_type): st.session_state.model_name = model_name st.session_state.summarizer_type = summarizer_type st.session_state.is_parameters_changed = False # init_summarizer(model_name,api_key=None) alert = st.success("chat parameters updated") time.sleep(2) # Wait for 1 seconds alert.empty() # Clear the alert import re def is_valid_open_ai_api_key(secretKey): if re.search("^sk-[a-zA-Z0-9]{32,}$", secretKey ): return True else: return False def side_bar(): with st.sidebar: st.subheader("Model parameters") with st.form('param_form'): # st.info('Info: use openai chat model for best results') model_name = st.selectbox( "Summary model", MODELS, # options=["long-t5 v0", "long-t5 v1", "pegasus-x-large v1", "OpenAI"], key="Model Name", help="Select the LLM model for summarization", # on_change=update_parameters_change, ) summarizer_type = st.selectbox( "Summarizer Type for Long Text", # options=["Map Reduce", "Refine"] options=["Map Reduce"] ) submitted = st.form_submit_button( "Save Parameters", # on_click=update_parameters_change disabled = True ) # if submitted: # parameters_change_button(model_name, summarizer_type) st.markdown("\n") if st.session_state.model_name == 'openai': with st.form('openai api key'): api_key = st.text_input( "Enter openai api key", type="password", value=st.session_state.openai_api_key, help="enter an openai api key created from 'https://platform.openai.com/account/api-keys'", ) submit_key = st.form_submit_button( "Save key", # on_click=update_parameters_change ) if submit_key: st.session_state.openai_api_key = api_key # st.text(st.session_state.openai_api_key) alert = st.success("openai api key updated") time.sleep(1) # Wait for 3 seconds alert.empty() # Clear the alert st.markdown( "### How to use\n" "1. Select the Summarization model\n" # noqa: E501 # "1. If selected model asks for a api key enter a valid api key.\n" # noqa: E501 "1. Enter the text to get the summary." ) st.markdown("---") st.markdown(""" This app supports text in the following formats: - Raw text in text box - .txt, .pdf, .docx file formats """ # - URL of article/news to be summarized ) def load_app(): st.title("Text Summarizer 📝") # inp_text = st.text_input("Enter text or a url here") # inp_text = st.text_input( # "Enter text or a url here" # ) inp_text = st.text_area( "Enter text here" ) st.markdown( "