from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.question_answering import load_qa_chain
from langchain.embeddings.openai import OpenAIEmbeddings
from streamlit_option_menu import option_menu
from deep_translator import GoogleTranslator
from langchain.vectorstores import Pinecone
import streamlit_authenticator as stauth
from yaml.loader import SafeLoader
from langchain.llms import OpenAI
from dotenv import load_dotenv
from langchain import OpenAI
from PyPDF2 import PdfReader
import streamlit as st
import langchain
load_dotenv()
import pinecone
import openai
import time
import yaml
import os

# Initialization
pinecone.init(api_key="db6b2a8c-d59e-48e1-8d5c-4c2704622937",environment="gcp-starter")
llm=OpenAI(model_name="gpt-3.5-turbo-instruct")
chain=load_qa_chain(llm,chain_type="stuff")
index_name="langchainvector"


# Home Page
def home():
    st.title("This is my Home page")


# Login Page
def login():
    st.title("Login page")
    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']
    )
    authenticator.login('Login', location = 'main')
    if st.session_state["authentication_status"]:
        st.title(f'Welcome *{st.session_state["name"]}*')
        st.subheader('Click on the Chat to upload document and access AI chatbot')
        user_name = st.session_state["name"]
        parent = os.getcwd()
        path = os.path.join(parent, user_name)
        if not os.path.exists(path):
            os.mkdir(path)
        with st.sidebar:
               authenticator.logout("Logout", "sidebar")
    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')


# Register Page
def register():
    st.title("Register page")
    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']
    )
    if authenticator.register_user('Register user', preauthorization=False):
        st.success('User registration successfully')
    with open('./config.yaml', 'a') as file:
        yaml.dump(config, file, default_flow_style=False)


def forgot_pass():
    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']
    )
    username_forgot_pw, email, random_password = authenticator.forgot_password('Forgot password')
    if username_forgot_pw:
        st.success(f'New random password is : {random_password}.. Change it in next login')
    elif username_forgot_pw == False:
        st.error('Username not found')
    with open('./config.yaml', 'w') as file:
        yaml.dump(config, file, default_flow_style=False)


def change_pass():
    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']
    )
    if st.session_state["authentication_status"]:
        if authenticator.reset_password(st.session_state["username"], 'Reset password'):
            st.success('New password changed')
    if not st.session_state["authentication_status"]:
        st.subheader('You need to login to change the password')
    with open('./config.yaml', 'w') as file:
        yaml.dump(config, file, default_flow_style=False)


def update_profile():
    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']
    )
    if st.session_state["authentication_status"]:
        if authenticator.update_user_details(st.session_state["username"], 'Update user details'):
            st.success('Entries updated successfully')
    if not st.session_state["authentication_status"]:
        st.subheader('You need to login to update the profile')
    with open('./config.yaml', 'a') as file:
        yaml.dump(config, file, default_flow_style=False)


# Translatiton
def translate_text(text, source='auto', target='hi'):
    return GoogleTranslator(source=source, target=target).translate(text)


# Extract document and create embeddings
def process_text():
	text = ""
	if not os.path.exists(st.session_state.txt_path):
		os.mkdir(st.session_state.txt_path)
	if st.session_state.doc_type  == 'PDF':
		for file in st.session_state.upload_folder:
			pdfdata = PdfReader(file)
			for page in pdfdata.pages:
				text += page.extract_text()
	else:
		for file in pdf_folder:
			for line in file:
				text += str(line, encoding = 'utf-8')
	file = open(st.session_state.txt_path + '/' + 'raw_text.txt' , 'w')
	file.write(text)

	text_splitter = RecursiveCharacterTextSplitter(
		chunk_size=1000,
		chunk_overlap=100, 
		length_function=len
	)
	chunks = text_splitter.split_text(text)
	embeddings = OpenAIEmbeddings()
	st.info('Creating OpenAI embeddings with PINECONE.... Please wait', icon="ℹ️")
	st.session_state.vector_db = Pinecone.from_texts(chunks,embeddings,index_name=index_name)
	st.success('Embeddings generated... Start the conversations', icon="✅")



def query_answer(query):
	docs = st.session_state.vector_db.similarity_search(query, k=2)
	response = chain.run(input_documents=docs, question=query)
	return response


def chatbox():
	for message in st.session_state.messages:
		with st.chat_message(message['role']):
			st.markdown(message['content'])
	if prompt := st.chat_input('Ask question about PDF content'):
		st.session_state.messages.append({'role' : 'user', 'content' : prompt})
		with st.chat_message('user'):
			st.markdown(prompt)
		with st.chat_message('assistant'):
			message_placeholder = st.empty()
			raw_prompt = translate_text(prompt, 'auto', 'en')
			result = query_answer(prompt)
			result2 = ""
			for chunk in result.split():
				result2 += chunk + " "
				time.sleep(0.1)
				message_placeholder.markdown(result2 + "▌")
		st.session_state.messages.append({"role": "assistant", "content": result})


def about(key):
	selection = st.session_state[key]
	if selection == 'Home':
		home()
	if selection == 'Login':
		login()
	if selection == 'Register':
		register()
	if selection == 'Forgot Password':
		forgot_pass()

def tasks():
	st.write('Tasks')

def main():
	if 'vector_db' not in st.session_state:
		st.session_state.vector_db = None
	if 'txt_path' not in st.session_state:
		st.session_state.txt_path = None
	if 'doc_type' not in st.session_state:
		st.session_state.doc_type = None
	if 'upload_folder' not in st.session_state:
		st.session_state.upload_folder = None
	if 'messages' not in st.session_state:
		st.session_state.messages = []
	st.session_state.txt_path = os.path.join(os.getcwd(), 'extract_text')
	with st.sidebar:
		selected5 = option_menu(None, ["Home", "Login", "Register", 'Forgot Passoword'],
	                        icons=['house', 'login', "register", 'gear'],
	                        on_change=about, key='menu_5', orientation="vertical")
		st.session_state.doc_type = st.selectbox('Document type', ('None','PDF','TXT', 'RST','MD'))
		st.session_state.upload_folder = st.file_uploader('Upload files', type = ['pdf', 'txt', 'rst','md'], accept_multiple_files=True)
		submitBtn = st.button('Submit') 
		if submitBtn:
			process_text()
		
	chatbox()


if __name__ == '__main__':
	main()

langchain.chains.question_answering.load_qa_chain langchain.vectorstores.Pinecone.from_texts langchain.text_splitter.RecursiveCharacterTextSplitter langchain.OpenAI langchain.embeddings.openai.OpenAIEmbeddings
[((560, 573), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (571, 573), False, 'from dotenv import load_dotenv\n'), ((656, 749), 'pinecone.init', 'pinecone.init', ([], {'api_key': '"""db6b2a8c-d59e-48e1-8d5c-4c2704622937"""', 'environment': '"""gcp-starter"""'}), "(api_key='db6b2a8c-d59e-48e1-8d5c-4c2704622937', environment=\n    'gcp-starter')\n", (669, 749), False, 'import pinecone\n'), ((748, 791), 'langchain.OpenAI', 'OpenAI', ([], {'model_name': '"""gpt-3.5-turbo-instruct"""'}), "(model_name='gpt-3.5-turbo-instruct')\n", (754, 791), False, 'from langchain import OpenAI\n'), ((798, 836), 'langchain.chains.question_answering.load_qa_chain', 'load_qa_chain', (['llm'], {'chain_type': '"""stuff"""'}), "(llm, chain_type='stuff')\n", (811, 836), False, 'from langchain.chains.question_answering import load_qa_chain\n'), ((895, 927), 'streamlit.title', 'st.title', (['"""This is my Home page"""'], {}), "('This is my Home page')\n", (903, 927), True, 'import streamlit as st\n'), ((960, 982), 'streamlit.title', 'st.title', (['"""Login page"""'], {}), "('Login page')\n", (968, 982), True, 'import streamlit as st\n'), ((1096, 1257), '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", (1115, 1257), True, 'import streamlit_authenticator as stauth\n'), ((2076, 2101), 'streamlit.title', 'st.title', (['"""Register page"""'], {}), "('Register page')\n", (2084, 2101), True, 'import streamlit as st\n'), ((2215, 2376), '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", (2234, 2376), True, 'import streamlit_authenticator as stauth\n'), ((2780, 2941), '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", (2799, 2941), True, 'import streamlit_authenticator as stauth\n'), ((3509, 3670), '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", (3528, 3670), True, 'import streamlit_authenticator as stauth\n'), ((4252, 4413), '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", (4271, 4413), True, 'import streamlit_authenticator as stauth\n'), ((5562, 5653), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'chunk_size': '(1000)', 'chunk_overlap': '(100)', 'length_function': 'len'}), '(chunk_size=1000, chunk_overlap=100,\n    length_function=len)\n', (5592, 5653), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n'), ((5715, 5733), 'langchain.embeddings.openai.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {}), '()\n', (5731, 5733), False, 'from langchain.embeddings.openai import OpenAIEmbeddings\n'), ((5735, 5813), 'streamlit.info', 'st.info', (['"""Creating OpenAI embeddings with PINECONE.... Please wait"""'], {'icon': '"""ℹ️"""'}), "('Creating OpenAI embeddings with PINECONE.... Please wait', icon='ℹ️')\n", (5742, 5813), True, 'import streamlit as st\n'), ((5844, 5906), 'langchain.vectorstores.Pinecone.from_texts', 'Pinecone.from_texts', (['chunks', 'embeddings'], {'index_name': 'index_name'}), '(chunks, embeddings, index_name=index_name)\n', (5863, 5906), False, 'from langchain.vectorstores import Pinecone\n'), ((5906, 5977), 'streamlit.success', 'st.success', (['"""Embeddings generated... Start the conversations"""'], {'icon': '"""✅"""'}), "('Embeddings generated... Start the conversations', icon='✅')\n", (5916, 5977), True, 'import streamlit as st\n'), ((6014, 6070), 'streamlit.session_state.vector_db.similarity_search', 'st.session_state.vector_db.similarity_search', (['query'], {'k': '(2)'}), '(query, k=2)\n', (6058, 6070), True, 'import streamlit as st\n'), ((7087, 7104), 'streamlit.write', 'st.write', (['"""Tasks"""'], {}), "('Tasks')\n", (7095, 7104), True, 'import streamlit as st\n'), ((1040, 1074), 'yaml.load', 'yaml.load', (['file'], {'Loader': 'SafeLoader'}), '(file, Loader=SafeLoader)\n', (1049, 1074), False, 'import yaml\n'), ((1404, 1453), 'streamlit.title', 'st.title', (['f"""Welcome *{st.session_state[\'name\']}*"""'], {}), '(f"Welcome *{st.session_state[\'name\']}*")\n', (1412, 1453), True, 'import streamlit as st\n'), ((1462, 1536), 'streamlit.subheader', 'st.subheader', (['"""Click on the Chat to upload document and access AI chatbot"""'], {}), "('Click on the Chat to upload document and access AI chatbot')\n", (1474, 1536), True, 'import streamlit as st\n'), ((1599, 1610), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (1608, 1610), False, 'import os\n'), ((1626, 1657), 'os.path.join', 'os.path.join', (['parent', 'user_name'], {}), '(parent, user_name)\n', (1638, 1657), False, 'import os\n'), ((2159, 2193), 'yaml.load', 'yaml.load', (['file'], {'Loader': 'SafeLoader'}), '(file, Loader=SafeLoader)\n', (2168, 2193), False, 'import yaml\n'), ((2498, 2542), 'streamlit.success', 'st.success', (['"""User registration successfully"""'], {}), "('User registration successfully')\n", (2508, 2542), True, 'import streamlit as st\n'), ((2596, 2645), 'yaml.dump', 'yaml.dump', (['config', 'file'], {'default_flow_style': '(False)'}), '(config, file, default_flow_style=False)\n', (2605, 2645), False, 'import yaml\n'), ((2724, 2758), 'yaml.load', 'yaml.load', (['file'], {'Loader': 'SafeLoader'}), '(file, Loader=SafeLoader)\n', (2733, 2758), False, 'import yaml\n'), ((3111, 3199), 'streamlit.success', 'st.success', (['f"""New random password is : {random_password}.. Change it in next login"""'], {}), "(\n    f'New random password is : {random_password}.. Change it in next login')\n", (3121, 3199), True, 'import streamlit as st\n'), ((3325, 3374), 'yaml.dump', 'yaml.dump', (['config', 'file'], {'default_flow_style': '(False)'}), '(config, file, default_flow_style=False)\n', (3334, 3374), False, 'import yaml\n'), ((3453, 3487), 'yaml.load', 'yaml.load', (['file'], {'Loader': 'SafeLoader'}), '(file, Loader=SafeLoader)\n', (3462, 3487), False, 'import yaml\n'), ((3955, 4011), 'streamlit.subheader', 'st.subheader', (['"""You need to login to change the password"""'], {}), "('You need to login to change the password')\n", (3967, 4011), True, 'import streamlit as st\n'), ((4065, 4114), 'yaml.dump', 'yaml.dump', (['config', 'file'], {'default_flow_style': '(False)'}), '(config, file, default_flow_style=False)\n', (4074, 4114), False, 'import yaml\n'), ((4196, 4230), 'yaml.load', 'yaml.load', (['file'], {'Loader': 'SafeLoader'}), '(file, Loader=SafeLoader)\n', (4205, 4230), False, 'import yaml\n'), ((4716, 4771), 'streamlit.subheader', 'st.subheader', (['"""You need to login to update the profile"""'], {}), "('You need to login to update the profile')\n", (4728, 4771), True, 'import streamlit as st\n'), ((4825, 4874), 'yaml.dump', 'yaml.dump', (['config', 'file'], {'default_flow_style': '(False)'}), '(config, file, default_flow_style=False)\n', (4834, 4874), False, 'import yaml\n'), ((5102, 5143), 'os.path.exists', 'os.path.exists', (['st.session_state.txt_path'], {}), '(st.session_state.txt_path)\n', (5116, 5143), False, 'import os\n'), ((5147, 5182), 'os.mkdir', 'os.mkdir', (['st.session_state.txt_path'], {}), '(st.session_state.txt_path)\n', (5155, 5182), False, 'import os\n'), ((6298, 6345), 'streamlit.chat_input', 'st.chat_input', (['"""Ask question about PDF content"""'], {}), "('Ask question about PDF content')\n", (6311, 6345), True, 'import streamlit as st\n'), ((6349, 6418), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'user', 'content': prompt}"], {}), "({'role': 'user', 'content': prompt})\n", (6381, 6418), True, 'import streamlit as st\n'), ((6780, 6854), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'assistant', 'content': result}"], {}), "({'role': 'assistant', 'content': result})\n", (6812, 6854), True, 'import streamlit as st\n'), ((7545, 7556), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (7554, 7556), False, 'import os\n'), ((7606, 7784), 'streamlit_option_menu.option_menu', 'option_menu', (['None', "['Home', 'Login', 'Register', 'Forgot Passoword']"], {'icons': "['house', 'login', 'register', 'gear']", 'on_change': 'about', 'key': '"""menu_5"""', 'orientation': '"""vertical"""'}), "(None, ['Home', 'Login', 'Register', 'Forgot Passoword'], icons=\n    ['house', 'login', 'register', 'gear'], on_change=about, key='menu_5',\n    orientation='vertical')\n", (7617, 7784), False, 'from streamlit_option_menu import option_menu\n'), ((7856, 7922), 'streamlit.selectbox', 'st.selectbox', (['"""Document type"""', "('None', 'PDF', 'TXT', 'RST', 'MD')"], {}), "('Document type', ('None', 'PDF', 'TXT', 'RST', 'MD'))\n", (7868, 7922), True, 'import streamlit as st\n'), ((7955, 8053), 'streamlit.file_uploader', 'st.file_uploader', (['"""Upload files"""'], {'type': "['pdf', 'txt', 'rst', 'md']", 'accept_multiple_files': '(True)'}), "('Upload files', type=['pdf', 'txt', 'rst', 'md'],\n    accept_multiple_files=True)\n", (7971, 8053), True, 'import streamlit as st\n'), ((8065, 8084), 'streamlit.button', 'st.button', (['"""Submit"""'], {}), "('Submit')\n", (8074, 8084), True, 'import streamlit as st\n'), ((1673, 1693), 'os.path.exists', 'os.path.exists', (['path'], {}), '(path)\n', (1687, 1693), False, 'import os\n'), ((1707, 1721), 'os.mkdir', 'os.mkdir', (['path'], {}), '(path)\n', (1715, 1721), False, 'import os\n'), ((1873, 1915), 'streamlit.error', 'st.error', (['"""Username/password is incorrect"""'], {}), "('Username/password is incorrect')\n", (1881, 1915), True, 'import streamlit as st\n'), ((3241, 3271), 'streamlit.error', 'st.error', (['"""Username not found"""'], {}), "('Username not found')\n", (3249, 3271), True, 'import streamlit as st\n'), ((3858, 3892), 'streamlit.success', 'st.success', (['"""New password changed"""'], {}), "('New password changed')\n", (3868, 3892), True, 'import streamlit as st\n'), ((4611, 4653), 'streamlit.success', 'st.success', (['"""Entries updated successfully"""'], {}), "('Entries updated successfully')\n", (4621, 4653), True, 'import streamlit as st\n'), ((4957, 5003), 'deep_translator.GoogleTranslator', 'GoogleTranslator', ([], {'source': 'source', 'target': 'target'}), '(source=source, target=target)\n', (4973, 5003), False, 'from deep_translator import GoogleTranslator\n'), ((5283, 5298), 'PyPDF2.PdfReader', 'PdfReader', (['file'], {}), '(file)\n', (5292, 5298), False, 'from PyPDF2 import PdfReader\n'), ((6215, 6247), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (6230, 6247), True, 'import streamlit as st\n'), ((6252, 6283), 'streamlit.markdown', 'st.markdown', (["message['content']"], {}), "(message['content'])\n", (6263, 6283), True, 'import streamlit as st\n'), ((6428, 6451), 'streamlit.chat_message', 'st.chat_message', (['"""user"""'], {}), "('user')\n", (6443, 6451), True, 'import streamlit as st\n'), ((6456, 6475), 'streamlit.markdown', 'st.markdown', (['prompt'], {}), '(prompt)\n', (6467, 6475), True, 'import streamlit as st\n'), ((6483, 6511), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (6498, 6511), True, 'import streamlit as st\n'), ((6538, 6548), 'streamlit.empty', 'st.empty', ([], {}), '()\n', (6546, 6548), True, 'import streamlit as st\n'), ((1984, 2037), 'streamlit.warning', 'st.warning', (['"""Please enter your username and password"""'], {}), "('Please enter your username and password')\n", (1994, 2037), True, 'import streamlit as st\n'), ((6714, 6729), 'time.sleep', 'time.sleep', (['(0.1)'], {}), '(0.1)\n', (6724, 6729), False, 'import time\n')]