EinfachOlder's picture
Duplicate from EinfachOlder/MultiPDF-QA-ChatGPT-Langchain
c570125
import os
import streamlit as st
from dotenv import load_dotenv
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from htmlTemplates import css, bot_template, user_template
def extract_text_from_pdfs(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def split_text_into_chunks(text):
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
return text_splitter.split_text(text)
def create_vector_store_from_text_chunks(text_chunks):
key = os.getenv('OPENAI_KEY')
embeddings = OpenAIEmbeddings(openai_api_key=key)
return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
def create_conversation_chain(vectorstore):
llm = ChatOpenAI()
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
return ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(), memory=memory)
def process_user_input(user_question):
response = st.session_state.conversation({'question': user_question})
st.session_state.chat_history = response['chat_history']
for i, message in enumerate(st.session_state.chat_history):
template = user_template if i % 2 == 0 else bot_template
st.write(template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
def change_prompt():
if st.session_state['ability'] == "Communication":
prompt = "Can you analyze the conversation files and provide insights on the communication skills exhibited within that conversation? Specifically, identify instances or patterns that demonstrate effective communication, such as clear articulation of ideas, active listening, asking relevant questions, and fostering meaningful dialogue. Additionally, highlight any areas where communication could be improved and suggest strategies for enhancing communication effectiveness within the conversation"
elif st.session_state['ability'] == "Teamwork":
prompt = "Based on the conversation files, could you analyze and provide insights on the role and effectiveness of teamwork within that conversation? Specifically, highlight any instances or patterns that showcase teamwork, collaborative problem-solving, or the impact of effective team dynamics."
elif st.session_state['ability'] == "Leadership":
prompt = "Based on the conversation files, could you analyze and provide insights regarding leadership dynamics within that conversation? Specifically, highlight any instances or patterns that demonstrate leadership qualities, such as taking initiative, guiding the conversation, motivating others, making informed decisions, or resolving conflicts effectively. Additionally, please identify any individuals who exhibited strong leadership skills and describe the impact of their leadership on the overall conversation"
elif st.session_state['ability'] == "Conflict Resolution":
prompt = "Could you analyze the conversation files and provide insights on conflict resolution within that conversation? Specifically, identify instances or patterns that showcase how conflicts were addressed, resolved, or managed by the individuals involved. Highlight any effective strategies or approaches used to navigate conflicts, foster understanding, and reach mutually agreeable solutions. Additionally, if there were any unresolved conflicts or challenges, please provide suggestions for how the conversation participants could have better approached or resolved those conflicts"
elif st.session_state['ability'] == "Decision-Making":
prompt = "Based on the conversation files, could you analyze and provide insights on the decision-making processes within that conversation? Specifically, identify instances or patterns where decisions were made, factors considered, and the overall effectiveness of the decision-making approach. Highlight any instances of informed decision-making, consensus-building, or consideration of various perspectives. Additionally, if there were any missed opportunities or areas for improvement in the decision-making process, please provide suggestions or alternative approaches that could have enhanced the decision-making outcomes"
elif st.session_state['ability'] == "Emotional Intelligence":
prompt = "Can you analyze the conversation files and provide insights on the display of emotional intelligence within that conversation? Specifically, identify instances or patterns where individuals demonstrated self-awareness, empathy, emotional regulation, or effective handling of emotions. Highlight any examples of individuals understanding and responding to others' emotions, demonstrating empathy, or navigating challenging situations with emotional intelligence. Additionally, if there were any missed opportunities or areas for improvement in terms of emotional intelligence, please provide suggestions or strategies for enhancing emotional intelligence within the conversation."
return prompt
def main():
load_dotenv()
st.set_page_config(page_title="Ask to your files", page_icon=":books:")
st.write(css, unsafe_allow_html=True)
st.header("Ask to your files :books:")
user_question = st.text_input("",placeholder="Leave empty to use default Prompt")
if st.button("Ask"):
with st.spinner("Processing"):
if user_question == "":
prompt = change_prompt()
#st.write(prompt)
process_user_input(prompt)
else:
process_user_input(user_question)
#st.write(user_question)
with st.sidebar:
st.subheader("Settings")
ability = st.selectbox('Ability to assess',('Communication', 'Teamwork', 'Leadership', 'Conflict Resolution', 'Decision-Making','Emotional Intelligence' ), key="ability")
st.subheader("Your documents")
pdf_docs = st.file_uploader("Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
if st.button("Process"):
with st.spinner("Processing"):
raw_text = extract_text_from_pdfs(pdf_docs)
text_chunks = split_text_into_chunks(raw_text)
vectorstore = create_vector_store_from_text_chunks(text_chunks)
st.session_state.conversation = create_conversation_chain(vectorstore)
if __name__ == '__main__':
main()