|
import streamlit as st |
|
from streamlit_chat import message |
|
from helper import get_qa_chain, create_vector_db |
|
|
|
st.set_page_config(layout="wide",page_title="Chat with PDF") |
|
|
|
|
|
def process_answer(instruction): |
|
response = '' |
|
instruction = instruction |
|
qa = get_qa_chain() |
|
generated_text = qa(instruction) |
|
answer = generated_text['result'] |
|
return answer |
|
|
|
|
|
def display_conversation(history): |
|
for i in range(len(history["generated"])): |
|
message(history["past"][i], is_user=True, key=str(i) + "_user") |
|
message(history["generated"][i],key=str(i)) |
|
|
|
def main(): |
|
st.header("Chat with your PDF") |
|
create_embeddings = st.button("Create Embeddings") |
|
|
|
if create_embeddings: |
|
with st.spinner('Embeddings are in process...'): |
|
create_vector_db() |
|
st.success('Embeddings are created successfully!') |
|
|
|
st.subheader("Chat Here") |
|
user_input = st.text_input("",key="input") |
|
|
|
|
|
if "generated" not in st.session_state: |
|
st.session_state["generated"] = ["I am an AI assitance how can I help?"] |
|
|
|
if "past" not in st.session_state: |
|
st.session_state["past"] = ["Hey there!"] |
|
|
|
if user_input: |
|
answer = process_answer({'query': user_input}) |
|
st.session_state["past"].append(user_input) |
|
response = answer |
|
st.session_state["generated"].append(response) |
|
|
|
|
|
if st.session_state["generated"]: |
|
display_conversation(st.session_state) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
main() |