Nazhar commited on
Commit
1034f78
·
verified ·
1 Parent(s): 3ca3a6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -2
app.py CHANGED
@@ -95,7 +95,8 @@ date_limit = pd.to_datetime(data['Date'].max()) # Set date limit for end date
95
  current_date = pd.to_datetime('2021-08-12')
96
 
97
  # Create Tabs
98
- market_analysis, news_analysis, final_recs, chat = st.tabs(["Market Analysis", "News Analysis", "GenAI Recommendations","Ask AI Advisor"])
 
99
 
100
  with market_analysis:
101
  st.header("Market Analysis", help = "This module provides market analysis for the following day based on the current date.")
@@ -863,4 +864,78 @@ with chat:
863
  st.write(f'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.')
864
 
865
  else:
866
- st.markdown(f"An Error Occured: \n {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  current_date = pd.to_datetime('2021-08-12')
96
 
97
  # Create Tabs
98
+ market_analysis, news_analysis, final_recs, chat, chat2 = st.tabs(["Market Analysis", "News Analysis", "GenAI Recommendations",
99
+ "Ask AI Advisor", "Ask AI Advisor 2"])
100
 
101
  with market_analysis:
102
  st.header("Market Analysis", help = "This module provides market analysis for the following day based on the current date.")
 
864
  st.write(f'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.')
865
 
866
  else:
867
+ st.markdown(f"An Error Occured: \n {e}")
868
+
869
+ with chat2:
870
+ from langchain_community.document_loaders import CSVLoader
871
+ from langchain_community.embeddings import HuggingFaceInstructEmbeddings
872
+ from langchain_community.vectorstores import FAISS
873
+ from langchain_community.llms import HuggingFaceHub
874
+ from langchain_core.prompts import PromptTemplate
875
+ from langchain.chains import RetrievalQA
876
+
877
+ st.header("Chat with AI Stock Advisor")
878
+
879
+ loader = CSVLoader("Events_SameDay.csv",encoding='iso-8859-1')
880
+ embeddings = HuggingFaceInstructEmbeddings()
881
+ persist_directory = 'FAISS_VectorStore'
882
+ db2 = FAISS.load_local(persist_directory, embeddings, allow_dangerous_deserialization=True)
883
+ repo_id = "mistralai/Mistral-7B-Instruct-v0.1"
884
+ llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature": 0.1, "max_new_tokens": 1024})
885
+
886
+ system_prompt = """Your task is to extract relevant information from the oil and gas sector data."""
887
+ B_INST, E_INST = "<s>[INST] ", " [/INST]"
888
+ template = (
889
+ B_INST
890
+ + system_prompt
891
+ + """
892
+ Context: {context}
893
+ User: {question}
894
+ """
895
+ + E_INST +
896
+ "\nHelpful Answer: \n"
897
+ )
898
+ sys_prompt = PromptTemplate(input_variables=["context", "question"], template=template)
899
+
900
+ chain = RetrievalQA.from_chain_type(
901
+ llm=llm,
902
+ chain_type="stuff",
903
+ retriever=db2.as_retriever(),
904
+ input_key="question",
905
+ chain_type_kwargs={"prompt": sys_prompt})
906
+
907
+ # Initialize chat history
908
+ if "messages" not in st.session_state:
909
+ st.session_state.messages = []
910
+
911
+ # Display chat messages from history on app rerun
912
+ for message in st.session_state.messages:
913
+ with st.chat_message(message["role"]):
914
+ st.markdown(message["content"])
915
+
916
+ # Accept user input
917
+ if prompt := st.chat_input("Enter your query here."):
918
+ # Add user message to chat history
919
+ st.session_state.messages.append({"role": "user", "content": prompt})
920
+ # Display user message in chat message container
921
+ with st.chat_message("user"):
922
+ st.markdown(prompt)
923
+
924
+ # question = query
925
+ print(f"User Question: {prompt}")
926
+ response = chain({"question": prompt})
927
+ print("Answer generated")
928
+ result = get_answer(response['result'])
929
+ print("helpful answer extracted")
930
+
931
+ # Display assistant response in chat message container
932
+ with st.chat_message("assistant"):
933
+ response = st.write_stream(response_generator(result))
934
+ # Add assistant response to chat history
935
+ st.session_state.messages.append({"role": "assistant", "content": response})
936
+
937
+
938
+
939
+
940
+
941
+