File size: 1,204 Bytes
8fc65e7
564a3c1
6060e42
8dcb499
8fc65e7
b51e5c7
6060e42
45a6d26
6060e42
b51e5c7
185d7c3
b51e5c7
 
d174b70
b51e5c7
 
 
 
 
45a6d26
 
b51e5c7
 
 
 
 
 
357a24d
b51e5c7
94bb6ea
 
 
b51e5c7
 
3c56661
b51e5c7
3c56661
45a6d26
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
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])