EinfachOlder commited on
Commit
c570125
0 Parent(s):

Duplicate from EinfachOlder/MultiPDF-QA-ChatGPT-Langchain

Browse files
Files changed (5) hide show
  1. .gitattributes +35 -0
  2. README.md +13 -0
  3. app.py +89 -0
  4. htmlTemplates.py +44 -0
  5. requirements.txt +14 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Ask Your Files
3
+ emoji: 📊
4
+ colorFrom: gray
5
+ colorTo: blue
6
+ sdk: streamlit
7
+ sdk_version: 1.21.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: EinfachOlder/MultiPDF-QA-ChatGPT-Langchain
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from PyPDF2 import PdfReader
5
+ from langchain.text_splitter import CharacterTextSplitter
6
+ from langchain.embeddings import OpenAIEmbeddings
7
+ from langchain.vectorstores import FAISS
8
+ from langchain.chat_models import ChatOpenAI
9
+ from langchain.memory import ConversationBufferMemory
10
+ from langchain.chains import ConversationalRetrievalChain
11
+ from htmlTemplates import css, bot_template, user_template
12
+
13
+ def extract_text_from_pdfs(pdf_docs):
14
+ text = ""
15
+ for pdf in pdf_docs:
16
+ pdf_reader = PdfReader(pdf)
17
+ for page in pdf_reader.pages:
18
+ text += page.extract_text()
19
+ return text
20
+
21
+ def split_text_into_chunks(text):
22
+ text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
23
+ return text_splitter.split_text(text)
24
+
25
+ def create_vector_store_from_text_chunks(text_chunks):
26
+ key = os.getenv('OPENAI_KEY')
27
+ embeddings = OpenAIEmbeddings(openai_api_key=key)
28
+ return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
29
+
30
+ def create_conversation_chain(vectorstore):
31
+ llm = ChatOpenAI()
32
+ memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
33
+ return ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(), memory=memory)
34
+
35
+ def process_user_input(user_question):
36
+ response = st.session_state.conversation({'question': user_question})
37
+ st.session_state.chat_history = response['chat_history']
38
+
39
+ for i, message in enumerate(st.session_state.chat_history):
40
+ template = user_template if i % 2 == 0 else bot_template
41
+ st.write(template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
42
+
43
+ def change_prompt():
44
+ if st.session_state['ability'] == "Communication":
45
+ 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"
46
+ elif st.session_state['ability'] == "Teamwork":
47
+ 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."
48
+ elif st.session_state['ability'] == "Leadership":
49
+ 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"
50
+ elif st.session_state['ability'] == "Conflict Resolution":
51
+ 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"
52
+ elif st.session_state['ability'] == "Decision-Making":
53
+ 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"
54
+ elif st.session_state['ability'] == "Emotional Intelligence":
55
+ 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."
56
+ return prompt
57
+
58
+ def main():
59
+ load_dotenv()
60
+ st.set_page_config(page_title="Ask to your files", page_icon=":books:")
61
+ st.write(css, unsafe_allow_html=True)
62
+
63
+ st.header("Ask to your files :books:")
64
+ user_question = st.text_input("",placeholder="Leave empty to use default Prompt")
65
+ if st.button("Ask"):
66
+ with st.spinner("Processing"):
67
+ if user_question == "":
68
+ prompt = change_prompt()
69
+ #st.write(prompt)
70
+ process_user_input(prompt)
71
+ else:
72
+ process_user_input(user_question)
73
+ #st.write(user_question)
74
+
75
+
76
+ with st.sidebar:
77
+ st.subheader("Settings")
78
+ ability = st.selectbox('Ability to assess',('Communication', 'Teamwork', 'Leadership', 'Conflict Resolution', 'Decision-Making','Emotional Intelligence' ), key="ability")
79
+ st.subheader("Your documents")
80
+ pdf_docs = st.file_uploader("Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
81
+ if st.button("Process"):
82
+ with st.spinner("Processing"):
83
+ raw_text = extract_text_from_pdfs(pdf_docs)
84
+ text_chunks = split_text_into_chunks(raw_text)
85
+ vectorstore = create_vector_store_from_text_chunks(text_chunks)
86
+ st.session_state.conversation = create_conversation_chain(vectorstore)
87
+
88
+ if __name__ == '__main__':
89
+ main()
htmlTemplates.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ css = '''
2
+ <style>
3
+ .chat-message {
4
+ padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
5
+ }
6
+ .chat-message.user {
7
+ background-color: #475063
8
+ }
9
+ .chat-message.bot {
10
+ background-color: #6EA0E1
11
+ }
12
+ .chat-message .avatar {
13
+ width: 15%;
14
+ }
15
+ .chat-message .avatar img {
16
+ max-width: 68px;
17
+ max-height: 68px;
18
+ border-radius: 50%;
19
+ object-fit: cover;
20
+ }
21
+ .chat-message .message {
22
+ width: 85%;
23
+ padding: 0 1.5rem;
24
+ color: #fff;
25
+ }
26
+ '''
27
+
28
+ bot_template = '''
29
+ <div class="chat-message bot">
30
+ <div class="avatar">
31
+ <img src="https://i.ibb.co/brCd5Pd/bot.png">
32
+ </div>
33
+ <div class="message">{{MSG}}</div>
34
+ </div>
35
+ '''
36
+
37
+ user_template = '''
38
+ <div class="chat-message user">
39
+ <div class="avatar">
40
+ <img src="https://i.ibb.co/6N3DLXK/Person.png">
41
+ </div>
42
+ <div class="message">{{MSG}}</div>
43
+ </div>
44
+ '''
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.0.184
2
+ PyPDF2==3.0.1
3
+ python-dotenv==1.0.0
4
+ streamlit==1.18.1
5
+ openai==0.27.6
6
+ faiss-cpu==1.7.4
7
+ tiktoken
8
+
9
+ # uncomment to use huggingface llms
10
+ # huggingface-hub==0.14.1
11
+
12
+ # uncomment to use instructor embeddings
13
+ # InstructorEmbedding==1.0.1
14
+ # sentence-transformers==2.2.2