import openai import os import streamlit as st from streamlit import session_state openai.api_key = os.getenv("OPENAI_API_KEY") from langchain.document_loaders import PyPDFLoader st.title("Chat with data") uploaded_file = st.file_uploader("Choose a file") uploaded_file = uploaded_file.name def extract(uploaded_file): res = [] loader = PyPDFLoader(uploaded_file) pages = loader.load() for i in pages: res.append(i.page_content.replace('\n','')) a = " ".join(res) return a def lang(uploaded_file,ques): context = extract(uploaded_file) docs = Document(page_content=context) index2 = VectorstoreIndexCreator().from_documents([docs]) answer = index2.query(llm = model, question = ques) index2.vectorstore.delete_collection() return answer def qna(uploaded_file,ques): session_state['answer']= lang(uploaded_file,ques) if 'answer' not in session_state: session_state['answer']= "" ques= st.text_area(label= "Please enter the Question that you wanna ask.", placeholder="Question") st.text_area("result", value=session_state['answer']) st.button("Get answer dictionary", on_click=qna, args=[uploaded_file,ques])