ngmitam commited on
Commit
994f18f
1 Parent(s): 4c7135c
Files changed (1) hide show
  1. app.py +52 -26
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from datetime import datetime
2
  import os
3
  import streamlit as st
@@ -58,11 +59,11 @@ def get_conversation_chain(vectorstore):
58
 
59
  def user_input(user_question):
60
  # log user question with timestamp
61
- print(f"[{datetime.now()}]:{user_question}")
62
  with st.spinner("Thinking ..."):
63
  response = st.session_state.conversation({'question': user_question})
64
  # log bot answer with timestamp
65
- print(f"\n[{datetime.now()}]:{response['answer']}")
66
  st.session_state.chat_history = response['chat_history']
67
  for i, messages in enumerate(st.session_state.chat_history):
68
  if i % 2 == 0:
@@ -71,36 +72,61 @@ def user_input(user_question):
71
  message(messages.content)
72
 
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  def main():
75
  load_dotenv()
76
  if "ggml-gpt4all-j-v1.3-groovy.bin" not in os.listdir("/tmp"):
77
  hf_hub_download(repo_id="dnato/ggml-gpt4all-j-v1.3-groovy.bin",
78
  filename="ggml-gpt4all-j-v1.3-groovy.bin", local_dir="/tmp")
79
- st.set_page_config(page_title="Document Chatbot")
80
- if "conversation" not in st.session_state:
81
- st.session_state.conversation = None
82
- if "chat_history" not in st.session_state:
83
- st.session_state.chat_history = None
84
-
85
- st.header("Query your CO2 documents")
86
- user_question = st.text_input("Ask a question about your documents...")
87
- if user_question and st.session_state.conversation:
88
- user_input(user_question)
 
 
 
 
 
 
 
 
 
 
89
  with st.sidebar:
90
- st.subheader("Your CO2 documents")
91
- pdfs = st.file_uploader(
92
- "Upload here", accept_multiple_files=True, type=["pdf"],)
93
- if st.button("Study"):
94
- with st.spinner("Studying ..."):
95
- raw_text = get_pdf_text(pdfs)
96
- # print(raw_text)
97
- chunks = get_text_chunks(raw_text)
98
- # print(chunks)
99
- vectorstore = get_vectorstore(chunks)
100
- # print(vectorstore)
101
- st.session_state.conversation = get_conversation_chain(
102
- vectorstore)
103
- st.success("Done!")
104
 
105
 
106
  if __name__ == '__main__':
 
1
+ import base64
2
  from datetime import datetime
3
  import os
4
  import streamlit as st
 
59
 
60
  def user_input(user_question):
61
  # log user question with timestamp
62
+ print(f"[{datetime.now()}]ask:{user_question}")
63
  with st.spinner("Thinking ..."):
64
  response = st.session_state.conversation({'question': user_question})
65
  # log bot answer with timestamp
66
+ print(f"\n[{datetime.now()}]ans:{response['answer']}")
67
  st.session_state.chat_history = response['chat_history']
68
  for i, messages in enumerate(st.session_state.chat_history):
69
  if i % 2 == 0:
 
72
  message(messages.content)
73
 
74
 
75
+ def display_pdf(pdf):
76
+ with open("./docs/"+pdf, "rb") as f:
77
+ base64_pdf = base64.b64encode(f.read()).decode('utf-8')
78
+ pdf_display = F'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">'
79
+ st.markdown(pdf_display, unsafe_allow_html=True)
80
+
81
+
82
+ examples = [
83
+ "CO2 Emissions trend in 2022?",
84
+ "What is the average CO2 emissions in 2022?",
85
+ "CO2 Emissions of the United States in 2022?",
86
+ "Compare CO2 Emissions of the United States and China in 2022?",
87
+ ]
88
+
89
+
90
  def main():
91
  load_dotenv()
92
  if "ggml-gpt4all-j-v1.3-groovy.bin" not in os.listdir("/tmp"):
93
  hf_hub_download(repo_id="dnato/ggml-gpt4all-j-v1.3-groovy.bin",
94
  filename="ggml-gpt4all-j-v1.3-groovy.bin", local_dir="/tmp")
95
+ st.set_page_config(page_title="CO2 Emission Document Chatbot")
96
+
97
+ if "pdf" not in st.session_state:
98
+ st.session_state.pdf = None
99
+ st.header("Choose document")
100
+ for pdf in os.listdir("./docs"):
101
+ if st.button(pdf):
102
+ with st.spinner("Loading document..."):
103
+ st.session_state.pdf = pdf
104
+ pdf_text = get_pdf_text(["./docs/"+pdf])
105
+ text_chunks = get_text_chunks(pdf_text)
106
+ vectorstore = get_vectorstore(text_chunks)
107
+ conversation_chain = get_conversation_chain(vectorstore)
108
+ st.session_state.conversation = conversation_chain
109
+ st.session_state.chat_history = None
110
+
111
+ if st.session_state.pdf:
112
+ if st.success("<-- Chat about the document on the left sidebar"):
113
+ display_pdf(st.session_state.pdf)
114
+
115
  with st.sidebar:
116
+ if "conversation" not in st.session_state:
117
+ st.session_state.conversation = None
118
+ if "chat_history" not in st.session_state:
119
+ st.session_state.chat_history = None
120
+ st.header("Query your document")
121
+ user_question = st.text_input(
122
+ "Ask a question about the document...", disabled=not st.session_state.conversation)
123
+
124
+ if st.session_state.conversation:
125
+ for example in examples:
126
+ if st.button(example):
127
+ user_question = example
128
+ if user_question and st.session_state.conversation:
129
+ user_input(user_question)
130
 
131
 
132
  if __name__ == '__main__':