NickNYU commited on
Commit
02e7265
β€’
1 Parent(s): 874aed6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -1,17 +1,16 @@
1
  import streamlit as st
 
2
  import os
3
- # import pickle
4
  from PyPDF2 import PdfReader
5
- # from langchain import FAISS
6
  from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain.embeddings.openai import OpenAIEmbeddings
8
-
9
- from langchain.vectorstores import Pinecone
10
- import pinecone
11
  from langchain.llms import OpenAI
12
  from langchain.chains.question_answering import load_qa_chain
13
  from langchain.callbacks import get_openai_callback
14
 
 
15
  # Sidebar contents
16
  with st.sidebar:
17
  st.title('πŸ€—πŸ’¬ LLM Chat App')
@@ -51,28 +50,34 @@ def main():
51
  store_name = pdf.name[:-4]
52
  st.write(f'{store_name}')
53
 
54
- # if os.path.exists(f"{store_name}.pkl"):
55
- # with open(f"{store_name}.pkl", "rb") as f:
56
- # VectorStore = pickle.load(f)
57
- # st.write('Embeddings Loaded from the Disk')
58
- # else:
59
- # st.write('Embeddings calculate to the Pinecone')
60
- # embeddings = OpenAIEmbeddings()
61
- # VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
62
- # with open(f"{store_name}.pkl", "wb") as f:
63
- # pickle.dump(VectorStore, f)
64
-
65
- PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY', '894d5f1f-df46-4b01-8407-d9977eaee2eb')
66
- PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV',
67
- 'asia-southeast1-gcp-free') # You may need to switch with your env
68
- embeddings = OpenAIEmbeddings()
69
- # initialize pinecone
70
- pinecone.init(
71
- api_key=PINECONE_API_KEY, # find at app.pinecone.io
72
- environment=PINECONE_API_ENV # next to api key in console
73
- )
74
- index_name = "indexer" # put in the name of your pinecone index here
75
- VectorStore = Pinecone.from_texts(chunks, embeddings, index_name=index_name)
 
 
 
 
 
 
76
 
77
  # Accept user questions/query
78
  query = st.text_input("Ask questions about your PDF file:")
 
1
  import streamlit as st
2
+ # from streamlit_extras.add_vertical_space import add_vertical_space
3
  import os
4
+ import pickle
5
  from PyPDF2 import PdfReader
 
6
  from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain.embeddings.openai import OpenAIEmbeddings
8
+ from langchain.vectorstores import FAISS
 
 
9
  from langchain.llms import OpenAI
10
  from langchain.chains.question_answering import load_qa_chain
11
  from langchain.callbacks import get_openai_callback
12
 
13
+
14
  # Sidebar contents
15
  with st.sidebar:
16
  st.title('πŸ€—πŸ’¬ LLM Chat App')
 
50
  store_name = pdf.name[:-4]
51
  st.write(f'{store_name}')
52
 
53
+ if os.path.exists(f"{store_name}.pkl"):
54
+ with open(f"{store_name}.pkl", "rb") as f:
55
+ VectorStore = pickle.load(f)
56
+ st.write('Embeddings Loaded from the Disk')
57
+ else:
58
+ st.write('Embeddings calculate to the Pinecone')
59
+ embeddings = OpenAIEmbeddings()
60
+ VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
61
+ with open(f"{store_name}.pkl", "wb") as f:
62
+ pickle.dump(VectorStore, f)
63
+ st.download_button(
64
+ label="Download data as pkl",
65
+ data=f,
66
+ file_name='{store_name}.pkl',
67
+ mime='text/text',
68
+ )
69
+
70
+ # PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY', '894d5f1f-df46-4b01-8407-d9977eaee2eb')
71
+ # PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV',
72
+ # 'asia-southeast1-gcp-free') # You may need to switch with your env
73
+ # embeddings = OpenAIEmbeddings()
74
+ # # initialize pinecone
75
+ # pinecone.init(
76
+ # api_key=PINECONE_API_KEY, # find at app.pinecone.io
77
+ # environment=PINECONE_API_ENV # next to api key in console
78
+ # )
79
+ # index_name = "indexer" # put in the name of your pinecone index here
80
+ # VectorStore = Pinecone.from_texts(chunks, embeddings, index_name=index_name)
81
 
82
  # Accept user questions/query
83
  query = st.text_input("Ask questions about your PDF file:")