Silence1412 commited on
Commit
9b3a839
1 Parent(s): fce8396

Update Chat_with_pdf_OpenAI

Browse files
Files changed (1) hide show
  1. Chat_with_pdf_OpenAI +50 -49
Chat_with_pdf_OpenAI CHANGED
@@ -10,54 +10,55 @@ import os
10
  import openai
11
  from streamlit_chat import message
12
 
13
- OPENAI_API_KEY = st.text_input("Input your OpenAI API key", "")
14
- os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
15
- # st.header("Ask your PDF 💬")
16
-
17
- # upload file
18
- pdf = st.file_uploader("Upload your PDF", type="pdf")
19
-
20
- # extract the text
21
- if pdf is not None:
22
- pdf_reader = PdfReader(pdf)
23
- text = ""
24
- for page in pdf_reader.pages:
25
- text += page.extract_text()
26
-
27
- # split into chunks
28
- text_splitter = CharacterTextSplitter(
29
- separator="\n",
30
- chunk_size=1000,
31
- chunk_overlap=200,
32
- length_function=len
33
- )
34
- chunks = text_splitter.split_text(text)
35
-
36
- # create embeddings
37
- embeddings = OpenAIEmbeddings()
38
- knowledge_base = FAISS.from_texts(chunks, embeddings)
39
-
40
- if 'generated' not in st.session_state:
41
- st.session_state['generated'] = []
42
- if 'past' not in st.session_state:
43
- st.session_state['past'] = []
44
-
45
- # show user input
46
- user_question = st.text_input("Ask a question about your PDF:")
47
- if user_question:
48
- docs = knowledge_base.similarity_search(user_question)
49
-
50
- llm = OpenAI()
51
- chain = load_qa_chain(llm, chain_type="stuff")
52
- with get_openai_callback() as cb:
53
- response = chain.run(input_documents=docs, question=user_question)
54
- print(cb)
55
 
56
- #st.write(response)
57
- st.session_state.past.append(user_question)
58
- st.session_state.generated.append(response)
59
 
60
- if st.session_state['generated']:
61
- for i in range(len(st.session_state['generated'])-1, -1, -1):
62
- message(st.session_state["generated"][i], key=str(i))
63
- message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  import openai
11
  from streamlit_chat import message
12
 
13
+ def openai_pdf():
14
+ OPENAI_API_KEY = st.text_input("Input your OpenAI API key", "")
15
+ os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
16
+ # st.header("Ask your PDF 💬")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # upload file
19
+ pdf = st.file_uploader("Upload your PDF", type="pdf")
 
20
 
21
+ # extract the text
22
+ if pdf is not None:
23
+ pdf_reader = PdfReader(pdf)
24
+ text = ""
25
+ for page in pdf_reader.pages:
26
+ text += page.extract_text()
27
+
28
+ # split into chunks
29
+ text_splitter = CharacterTextSplitter(
30
+ separator="\n",
31
+ chunk_size=1000,
32
+ chunk_overlap=200,
33
+ length_function=len
34
+ )
35
+ chunks = text_splitter.split_text(text)
36
+
37
+ # create embeddings
38
+ embeddings = OpenAIEmbeddings()
39
+ knowledge_base = FAISS.from_texts(chunks, embeddings)
40
+
41
+ if 'generated' not in st.session_state:
42
+ st.session_state['generated'] = []
43
+ if 'past' not in st.session_state:
44
+ st.session_state['past'] = []
45
+
46
+ # show user input
47
+ user_question = st.text_input("Ask a question about your PDF:")
48
+ if user_question:
49
+ docs = knowledge_base.similarity_search(user_question)
50
+
51
+ llm = OpenAI()
52
+ chain = load_qa_chain(llm, chain_type="stuff")
53
+ with get_openai_callback() as cb:
54
+ response = chain.run(input_documents=docs, question=user_question)
55
+ print(cb)
56
+
57
+ #st.write(response)
58
+ st.session_state.past.append(user_question)
59
+ st.session_state.generated.append(response)
60
+
61
+ if st.session_state['generated']:
62
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
63
+ message(st.session_state["generated"][i], key=str(i))
64
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')