# This code sets up the necessary components, interacts with the LangChain tool and ChatOpenAI model to perform text summarization, 
# and provides a user interface for input and output.

from langchain.document_loaders import UnstructuredFileLoader  # Importing necessary modules
from langchain.document_loaders import UnstructuredPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain.prompts import PromptTemplate  # Importing PromptTemplate for prompts
import markdown
from html2docx import html2docx
import os
import openai
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader
#import langchain
#langchain.debug = True
def open_file(filepath):
    with open(filepath, "r", encoding="utf-8") as infile:
         sadrzaj = infile.read()
         infile.close()
         return sadrzaj
# Creating a list of messages that includes a system message and an AI message
def main():

    st.title('Large Text Summarizer with Input for .pdf, .txt and .docx')  # Setting the title for Streamlit application
    uploaded_file = st.file_uploader("Choose a file")

    openai.api_key = os.environ.get('OPENAI_API_KEY')  # Reading OpenAI API key from file
    llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key=openai.api_key)  # Initializing ChatOpenAI model

    placeholder = st.empty()
    st.session_state['question'] = ''
    # document.add_heading('Suma velikog dokumenta', level=1)
    dld="blank"
    buf = html2docx("nothing", title="Summary")
    # summarize chosen file
    if uploaded_file is not None:
    
        with placeholder.form(key='my_form', clear_on_submit=True): 
               # st.write(uploaded_file.name)
               with open(uploaded_file.name, "wb") as file:
                        file.write(uploaded_file.getbuffer())
               if ".pdf" in uploaded_file.name:
                    loader = UnstructuredPDFLoader(uploaded_file.name, encoding="utf-8")
               else:
                    loader = UnstructuredFileLoader(uploaded_file.name, encoding="utf-8")  # Creating a file loader object

               result = loader.load()  # Loading text from the file

               text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)  # Creating a text splitter object
               texts = text_splitter.split_documents(result)  # Splitting the loaded text into smaller chunks
               prompt_initial=open_file("prompt_initial.txt")
               prompt_final=open_file("prompt_final.txt")
               prompt_opsirni= open_file("prompt_opsirni.txt")
               opis1 = st.text_input(f"Ovo je postojeci inicijalni prompt :  {prompt_initial}  Dodajte potrebne detalje koji ce zauzeti mesto polja opis1 detalje koji nedostaju: ")
               opis2 = st.text_input(f"Ovo je postojeci finalni prompt : {prompt_final} Dodajte potrebne detalje koji ce zauzeti mesto polja opis2 detalje koji nedostaju: ")
               st.write(f"Ovo je postojeci prompt za opsirni deo teksta : {prompt_opsirni} ")
               submit_button = st.form_submit_button(label='Submit')
               # Creating a list of messages that includes a system message and an AI message
               opp = PromptTemplate(template=prompt_opsirni, input_variables=["text"]) 
               initial= PromptTemplate(template=prompt_initial, input_variables=["text" , "opis1"])  # Creating a prompt template object
               final = PromptTemplate(template=prompt_final, input_variables=["text", "opis2" ])  # Creating a prompt template object
              

               
           
               if submit_button:
                    with st.spinner("Sacekajte trenutak..."):
                        chain = load_summarize_chain(llm, chain_type="map_reduce", verbose=False, map_prompt=initial, combine_prompt=final)
                        # Load the summarization chain with verbose mode
                        chain2 = load_summarize_chain(llm, chain_type="map_reduce", verbose=False, return_intermediate_steps=True, map_prompt=opp, combine_prompt=opp)
                        prosireno = chain2({"input_documents": texts}, return_only_outputs=True)
                        samo_text = prosireno['intermediate_steps']

                        output_string = ""
                        # Process each element of the list
                        for i, step in enumerate(samo_text, start=1):
                            # Create a variable dynamically for each element
                            var_name = f"Poglavlje {i}"
                            globals()[var_name] = step
                            output_string += f" **{var_name}:**  {step}\n\n"
                        
                        st.markdown("# Opsirnije" + "\n\n")
                        st.markdown(output_string)
                        st.markdown("\n\n" + "# Ukratko" + "\n\n")
                        suma = AIMessage(content=chain.run(input_documents=texts, opis1=opis1, opis2=opis2))
                        st.markdown(suma.content)  # Displaying the summary
                        dld =  "# Executive Summary" + "\n\n" +suma.content + "\n\n" +  "## Opsirnije" + "\n\n" + output_string 
                        html = markdown.markdown(dld)
                        buf = html2docx(html, title="Summary")
                        
        st.download_button(
                label="Click here to download",
                data=buf.getvalue(),
                file_name="Suma.docx",
                mime="docx"
                )   

st.set_page_config(
page_title="Positive summarizer",
page_icon="📖",
layout="wide",
initial_sidebar_state="collapsed",
)



hide_streamlit_style = """
        <style>
        #MainMenu {visibility: hidden;}
        footer {visibility: hidden;}
        </style>
        """
st.markdown(hide_streamlit_style, unsafe_allow_html=True) 


with open('config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    config['preauthorized']
)

name, authentication_status, username = authenticator.login('Login to use the service', 'main')

if st.session_state["authentication_status"]:
    authenticator.logout('Logout', 'main', key='unique_key')
    # if login success run the program
    main()
elif st.session_state["authentication_status"] is False:
    st.error('Username/password is incorrect')
elif st.session_state["authentication_status"] is None:
    st.warning('Please enter your username and password')
 
   


langchain.chains.summarize.load_summarize_chain langchain.document_loaders.UnstructuredFileLoader langchain.text_splitter.RecursiveCharacterTextSplitter langchain.chat_models.ChatOpenAI langchain.document_loaders.UnstructuredPDFLoader langchain.prompts.PromptTemplate
[((5769, 5891), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Positive summarizer"""', 'page_icon': '"""📖"""', 'layout': '"""wide"""', 'initial_sidebar_state': '"""collapsed"""'}), "(page_title='Positive summarizer', page_icon='📖', layout=\n    'wide', initial_sidebar_state='collapsed')\n", (5787, 5891), True, 'import streamlit as st\n'), ((6042, 6099), 'streamlit.markdown', 'st.markdown', (['hide_streamlit_style'], {'unsafe_allow_html': '(True)'}), '(hide_streamlit_style, unsafe_allow_html=True)\n', (6053, 6099), True, 'import streamlit as st\n'), ((6202, 6363), 'streamlit_authenticator.Authenticate', 'stauth.Authenticate', (["config['credentials']", "config['cookie']['name']", "config['cookie']['key']", "config['cookie']['expiry_days']", "config['preauthorized']"], {}), "(config['credentials'], config['cookie']['name'], config\n    ['cookie']['key'], config['cookie']['expiry_days'], config['preauthorized']\n    )\n", (6221, 6363), True, 'import streamlit_authenticator as stauth\n'), ((1152, 1221), 'streamlit.title', 'st.title', (['"""Large Text Summarizer with Input for .pdf, .txt and .docx"""'], {}), "('Large Text Summarizer with Input for .pdf, .txt and .docx')\n", (1160, 1221), True, 'import streamlit as st\n'), ((1289, 1322), 'streamlit.file_uploader', 'st.file_uploader', (['"""Choose a file"""'], {}), "('Choose a file')\n", (1305, 1322), True, 'import streamlit as st\n'), ((1345, 1377), 'os.environ.get', 'os.environ.get', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (1359, 1377), False, 'import os\n'), ((1424, 1513), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'model_name': '"""gpt-3.5-turbo"""', 'temperature': '(0)', 'openai_api_key': 'openai.api_key'}), "(model_name='gpt-3.5-turbo', temperature=0, openai_api_key=openai\n    .api_key)\n", (1434, 1513), False, 'from langchain.chat_models import ChatOpenAI\n'), ((1561, 1571), 'streamlit.empty', 'st.empty', ([], {}), '()\n', (1569, 1571), True, 'import streamlit as st\n'), ((1698, 1735), 'html2docx.html2docx', 'html2docx', (['"""nothing"""'], {'title': '"""Summary"""'}), "('nothing', title='Summary')\n", (1707, 1735), False, 'from html2docx import html2docx\n'), ((6150, 6184), 'yaml.load', 'yaml.load', (['file'], {'Loader': 'SafeLoader'}), '(file, Loader=SafeLoader)\n', (6159, 6184), False, 'import yaml\n'), ((6692, 6734), 'streamlit.error', 'st.error', (['"""Username/password is incorrect"""'], {}), "('Username/password is incorrect')\n", (6700, 6734), True, 'import streamlit as st\n'), ((2422, 2486), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'chunk_size': '(2000)', 'chunk_overlap': '(0)'}), '(chunk_size=2000, chunk_overlap=0)\n', (2452, 2486), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n'), ((2838, 3005), 'streamlit.text_input', 'st.text_input', (['f"""Ovo je postojeci inicijalni prompt :  {prompt_initial}  Dodajte potrebne detalje koji ce zauzeti mesto polja opis1 detalje koji nedostaju: """'], {}), "(\n    f'Ovo je postojeci inicijalni prompt :  {prompt_initial}  Dodajte potrebne detalje koji ce zauzeti mesto polja opis1 detalje koji nedostaju: '\n    )\n", (2851, 3005), True, 'import streamlit as st\n'), ((3019, 3179), 'streamlit.text_input', 'st.text_input', (['f"""Ovo je postojeci finalni prompt : {prompt_final} Dodajte potrebne detalje koji ce zauzeti mesto polja opis2 detalje koji nedostaju: """'], {}), "(\n    f'Ovo je postojeci finalni prompt : {prompt_final} Dodajte potrebne detalje koji ce zauzeti mesto polja opis2 detalje koji nedostaju: '\n    )\n", (3032, 3179), True, 'import streamlit as st\n'), ((3185, 3263), 'streamlit.write', 'st.write', (['f"""Ovo je postojeci prompt za opsirni deo teksta : {prompt_opsirni} """'], {}), "(f'Ovo je postojeci prompt za opsirni deo teksta : {prompt_opsirni} ')\n", (3193, 3263), True, 'import streamlit as st\n'), ((3295, 3332), 'streamlit.form_submit_button', 'st.form_submit_button', ([], {'label': '"""Submit"""'}), "(label='Submit')\n", (3316, 3332), True, 'import streamlit as st\n'), ((3448, 3513), 'langchain.prompts.PromptTemplate', 'PromptTemplate', ([], {'template': 'prompt_opsirni', 'input_variables': "['text']"}), "(template=prompt_opsirni, input_variables=['text'])\n", (3462, 3513), False, 'from langchain.prompts import PromptTemplate\n'), ((3539, 3613), 'langchain.prompts.PromptTemplate', 'PromptTemplate', ([], {'template': 'prompt_initial', 'input_variables': "['text', 'opis1']"}), "(template=prompt_initial, input_variables=['text', 'opis1'])\n", (3553, 3613), False, 'from langchain.prompts import PromptTemplate\n'), ((3675, 3747), 'langchain.prompts.PromptTemplate', 'PromptTemplate', ([], {'template': 'prompt_final', 'input_variables': "['text', 'opis2']"}), "(template=prompt_final, input_variables=['text', 'opis2'])\n", (3689, 3747), False, 'from langchain.prompts import PromptTemplate\n'), ((6795, 6848), 'streamlit.warning', 'st.warning', (['"""Please enter your username and password"""'], {}), "('Please enter your username and password')\n", (6805, 6848), True, 'import streamlit as st\n'), ((2117, 2176), 'langchain.document_loaders.UnstructuredPDFLoader', 'UnstructuredPDFLoader', (['uploaded_file.name'], {'encoding': '"""utf-8"""'}), "(uploaded_file.name, encoding='utf-8')\n", (2138, 2176), False, 'from langchain.document_loaders import UnstructuredPDFLoader\n'), ((2227, 2287), 'langchain.document_loaders.UnstructuredFileLoader', 'UnstructuredFileLoader', (['uploaded_file.name'], {'encoding': '"""utf-8"""'}), "(uploaded_file.name, encoding='utf-8')\n", (2249, 2287), False, 'from langchain.document_loaders import UnstructuredFileLoader\n'), ((3888, 3923), 'streamlit.spinner', 'st.spinner', (['"""Sacekajte trenutak..."""'], {}), "('Sacekajte trenutak...')\n", (3898, 3923), True, 'import streamlit as st\n'), ((3957, 4068), 'langchain.chains.summarize.load_summarize_chain', 'load_summarize_chain', (['llm'], {'chain_type': '"""map_reduce"""', 'verbose': '(False)', 'map_prompt': 'initial', 'combine_prompt': 'final'}), "(llm, chain_type='map_reduce', verbose=False,\n    map_prompt=initial, combine_prompt=final)\n", (3977, 4068), False, 'from langchain.chains.summarize import load_summarize_chain\n'), ((4171, 4308), 'langchain.chains.summarize.load_summarize_chain', 'load_summarize_chain', (['llm'], {'chain_type': '"""map_reduce"""', 'verbose': '(False)', 'return_intermediate_steps': '(True)', 'map_prompt': 'opp', 'combine_prompt': 'opp'}), "(llm, chain_type='map_reduce', verbose=False,\n    return_intermediate_steps=True, map_prompt=opp, combine_prompt=opp)\n", (4191, 4308), False, 'from langchain.chains.summarize import load_summarize_chain\n'), ((4957, 4992), 'streamlit.markdown', 'st.markdown', (["('# Opsirnije' + '\\n\\n')"], {}), "('# Opsirnije' + '\\n\\n')\n", (4968, 4992), True, 'import streamlit as st\n'), ((5017, 5043), 'streamlit.markdown', 'st.markdown', (['output_string'], {}), '(output_string)\n', (5028, 5043), True, 'import streamlit as st\n'), ((5068, 5110), 'streamlit.markdown', 'st.markdown', (["('\\n\\n' + '# Ukratko' + '\\n\\n')"], {}), "('\\n\\n' + '# Ukratko' + '\\n\\n')\n", (5079, 5110), True, 'import streamlit as st\n'), ((5244, 5269), 'streamlit.markdown', 'st.markdown', (['suma.content'], {}), '(suma.content)\n', (5255, 5269), True, 'import streamlit as st\n'), ((5456, 5478), 'markdown.markdown', 'markdown.markdown', (['dld'], {}), '(dld)\n', (5473, 5478), False, 'import markdown\n'), ((5509, 5541), 'html2docx.html2docx', 'html2docx', (['html'], {'title': '"""Summary"""'}), "(html, title='Summary')\n", (5518, 5541), False, 'from html2docx import html2docx\n')]