import openai import os import streamlit as st from streamlit import session_state import base64 import tempfile from pathlib import Path from langchain.document_loaders import WebBaseLoader, PyPDFLoader, TextLoader from langchain.indexes import VectorstoreIndexCreator from langchain.embeddings import HuggingFaceEmbeddings from langchain.docstore.document import Document openai.api_key = os.getenv("OPENAI_API_KEY") from langchain.document_loaders import PyPDFLoader from langchain.chat_models import ChatOpenAI st.title("Chat with data") model = ChatOpenAI(model = 'gpt-4', max_tokens = 100,temperature=0) uploaded_file = st.file_uploader("Choose a file") if uploaded_file is not None: # Make temp file path from uploaded file with tempfile.NamedTemporaryFile(delete=False) as tmp_file: fp = Path(tmp_file.name) fp.write_bytes(uploaded_file.getvalue()) print(tmp_file.name,"path") 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(ques): context = extract(tmp_file.name) 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(ques): session_state['answer']= lang(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("Submit", on_click=qna, args=[ques])