File size: 1,763 Bytes
8fc65e7
564a3c1
6060e42
8dcb499
01f902f
 
 
18d1238
 
 
 
01f902f
 
 
6060e42
0ee5e57
45a6d26
01f902f
b51e5c7
01f902f
 
 
 
 
 
b51e5c7
 
d174b70
b51e5c7
 
 
 
 
0ee5e57
 
b51e5c7
 
 
 
 
0ee5e57
 
b51e5c7
94bb6ea
 
 
b51e5c7
 
3c56661
b51e5c7
3c56661
2b16e78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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])