Update
Browse files
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 |
-
|
81 |
-
|
82 |
-
|
83 |
-
st.
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
with st.sidebar:
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
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__':
|