acodi commited on
Commit
907a182
1 Parent(s): 870b873

Switch to use EU LAW GPT in .env

Browse files
Files changed (5) hide show
  1. .env.local +11 -0
  2. .gitignore +3 -0
  3. app.py +21 -27
  4. htmlTemplates.py +5 -2
  5. requirements.txt +2 -3
.env.local ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is used to store the environment variables that are used in the application
2
+ # Please make sure to create a .env file in the root directory of the project and add the following variables
3
+
4
+ # OPEN AI API KEY
5
+ OPENAI_API_KEY=
6
+
7
+ # LAW GPT MODEL URL, please look at https://huggingface.co/codimind for the model url
8
+ LAW_GPT_MODEL_URL=
9
+
10
+ # Huggingface hub api token
11
+ HUGGINGFACEHUB_API_TOKEN=
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env
2
+ __pycache__/
3
+ .DS_Store
app.py CHANGED
@@ -48,11 +48,15 @@ def get_vector_store(text_chunks):
48
  return vectorstore
49
 
50
  def get_conversation_chain(vectorstore):
51
- llm = ChatOpenAI(openai_api_key=os.getenv('OPENAI_API_KEY'))
52
- # llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
53
- # llm = HuggingFaceHub(repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct", model_kwargs={"temperature":0.5, "max_length":512})
54
- # llm = HuggingFaceHub(repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct")
55
 
 
 
 
 
 
 
56
  memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
57
  conversation_chain = ConversationalRetrievalChain.from_llm(
58
  llm=llm,
@@ -76,12 +80,12 @@ def handle_user_input(user_question):
76
  st.write(bot_template.replace(
77
  "{{MSG}}", message.content), unsafe_allow_html=True)
78
  else:
79
- st.write("Please upload PDFs and click process")
80
 
81
  def main():
82
  load_dotenv()
83
 
84
- st.set_page_config(page_title="Onki AI Assistant - Chat with multiple PDFs", page_icon=":books:")
85
  st.write(css, unsafe_allow_html=True)
86
 
87
  #load knowledge data PDF
@@ -97,31 +101,21 @@ def main():
97
  if "chat_history" not in st.session_state:
98
  st.session_state.chat_history = None
99
 
100
- st.header("Chat with multiple PDFs :books:")
101
- user_question = st.text_input("Ask a question about your documents:")
102
-
103
- if user_question:
104
- handle_user_input(user_question)
105
-
106
- with st.sidebar:
107
-
108
- st.subheader("Your documents")
109
- pdf_docs = st.file_uploader("Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
110
- if st.button("Process"):
111
- with st.spinner("Processing"):
112
- # get pdf text
113
- raw_text = get_pdf_text(pdf_docs)
114
 
115
- # get the text chunks
116
- text_chunks = get_text_chunks(raw_text)
 
117
 
118
- # create vector store
119
- vectorstore = get_vector_store(text_chunks)
120
 
121
- vectorstore.merge_from(vectorstore_knowledge)
 
122
 
123
- #create conversation chain
124
- st.session_state.conversation = get_conversation_chain(vectorstore)
125
 
126
  if __name__ == '__main__':
127
  main()
 
48
  return vectorstore
49
 
50
  def get_conversation_chain(vectorstore):
51
+ if not os.getenv('OPENAI_API_KEY') and not os.getenv('LAW_GPT_MODEL_URL'):
52
+ raise ValueError("Please provide either OPENAI_API_KEY or LAW_GPT_MODEL_URL in the .env file")
 
 
53
 
54
+ # Use LAW GPT model if LAW_GPT_MODEL_URL is provided
55
+ if os.getenv('LAW_GPT_MODEL_URL'):
56
+ llm = HuggingFaceHub(repo_id=os.getenv('LAW_GPT_MODEL_URL'))
57
+ else:
58
+ llm = ChatOpenAI(openai_api_key=os.getenv('OPENAI_API_KEY'))
59
+
60
  memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
61
  conversation_chain = ConversationalRetrievalChain.from_llm(
62
  llm=llm,
 
80
  st.write(bot_template.replace(
81
  "{{MSG}}", message.content), unsafe_allow_html=True)
82
  else:
83
+ st.write("No data is loaded for RAG. Please upload a PDFs files to the data/ directory.")
84
 
85
  def main():
86
  load_dotenv()
87
 
88
+ st.set_page_config(page_title="EULawGPT - LLM model that can understand and reason about EU public domain data", page_icon=":books:")
89
  st.write(css, unsafe_allow_html=True)
90
 
91
  #load knowledge data PDF
 
101
  if "chat_history" not in st.session_state:
102
  st.session_state.chat_history = None
103
 
104
+ st.title("EU Law GPT")
105
+ st.write("EU Law GPT is a LLM model that can understand and reason about EU public domain data")
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
+ st.subheader('Popular questions:')
108
+ if st.button("What is happening in Equador?"):
109
+ handle_user_input("What is happening in Equador?")
110
 
111
+ if st.button("What EU will do with Ecuador crisis?"):
112
+ handle_user_input("What EU will do with Ecuador crisis?")
113
 
114
+ st.subheader('Ask anything:')
115
+ user_question = st.text_input("Ask a question about EU Law and Parlament work")
116
 
117
+ if user_question:
118
+ handle_user_input(user_question)
119
 
120
  if __name__ == '__main__':
121
  main()
htmlTemplates.py CHANGED
@@ -28,7 +28,8 @@ css = '''
28
  bot_template = '''
29
  <div class="chat-message bot">
30
  <div class="avatar">
31
- <img src="https://i.ibb.co/cN0nmSj/Screenshot-2023-05-28-at-02-37-21.png" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
 
32
  </div>
33
  <div class="message">{{MSG}}</div>
34
  </div>
@@ -37,8 +38,10 @@ bot_template = '''
37
  user_template = '''
38
  <div class="chat-message user">
39
  <div class="avatar">
40
- <img src="https://i.ibb.co/rdZC7LZ/Photo-logo-1.png">
 
41
  </div>
42
  <div class="message">{{MSG}}</div>
43
  </div>
 
44
  '''
 
28
  bot_template = '''
29
  <div class="chat-message bot">
30
  <div class="avatar">
31
+ <img style='display:block; width:100px;height:100px;' id='base64imageBot'
32
+ src='data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICA8IS0tIEJhY2tncm91bmQgQ2lyY2xlIC0tPgogIDxjaXJjbGUgY3g9IjEwMCIgY3k9IjEwMCIgcj0iMTAwIiBmaWxsPSIjRkZENzAwIi8+CiAgCiAgPCEtLSBIZWFkIC0tPgogIDxjaXJjbGUgY3g9IjEwMCIgY3k9IjgwIiByPSI1MCIgZmlsbD0iI0ZGRTBCRCIvPgogIAogIDwhLS0gRXllcyAtLT4KICA8Y2lyY2xlIGN4PSI4MCIgY3k9IjcwIiByPSI4IiBmaWxsPSIjMDAwMDAwIi8+CiAgPGNpcmNsZSBjeD0iMTIwIiBjeT0iNzAiIHI9IjgiIGZpbGw9IiMwMDAwMDAiLz4KICAKICA8IS0tIE1vdXRoIC0tPgogIDxwYXRoIGQ9Ik0gNzAgOTAgUSAxMDAgMTIwIDEzMCA5MCIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjUiIGZpbGw9Im5vbmUiLz4KICAKICA8IS0tIEJvZHkgLS0+CiAgPHJlY3QgeD0iNTAiIHk9IjEzMCIgd2lkdGg9IjEwMCIgaGVpZ2h0PSI1MCIgZmlsbD0iIzQ2ODJCNCIvPgogIAogIDwhLS0gQXJtcyAtLT4KICA8cmVjdCB4PSIyMCIgeT0iMTMwIiB3aWR0aD0iMzAiIGhlaWdodD0iMTAiIGZpbGw9IiNGRkUwQkQiLz4KICA8cmVjdCB4PSIxNTAiIHk9IjEzMCIgd2lkdGg9IjMwIiBoZWlnaHQ9IjEwIiBmaWxsPSIjRkZFMEJEIi8+Cjwvc3ZnPg==' />
33
  </div>
34
  <div class="message">{{MSG}}</div>
35
  </div>
 
38
  user_template = '''
39
  <div class="chat-message user">
40
  <div class="avatar">
41
+ <img style="display: block; width: 100px; height: 100px;" id="base64imageUser"
42
+ src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICA8Y2lyY2xlIGN4PSIxMDAiIGN5PSIxMDAiIHI9IjEwMCIgZmlsbD0iI0ZGNjM0NyIvPgogIDxjaXJjbGUgY3g9IjEwMCIgY3k9IjgwIiByPSI1MCI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t CHANGED
@@ -1,9 +1,8 @@
1
- #unstructured[pdf]
2
- #python-magic
3
  streamlit
4
  python-dotenv
5
  PyPDF2
6
  langchain
7
  langchain_openai
8
  langchain_community
9
- faiss-cpu
 
 
 
 
1
  streamlit
2
  python-dotenv
3
  PyPDF2
4
  langchain
5
  langchain_openai
6
  langchain_community
7
+ faiss-cpu
8
+ huggingface_hub