johnmuchiri commited on
Commit
1404580
1 Parent(s): d2bc30a

Add application file

Browse files
Files changed (2) hide show
  1. app.py +137 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from langchain.chains import RetrievalQA
4
+ from langchain.vectorstores import Chroma, Pinecone
5
+ from langchain.llms import OpenAI
6
+ from langchain.document_loaders import TextLoader
7
+ from langchain.document_loaders import PyPDFLoader
8
+ from langchain.indexes import VectorstoreIndexCreator
9
+ from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
10
+ from langchain.embeddings import OpenAIEmbeddings
11
+ from langchain.vectorstores import Chroma
12
+ from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader
13
+ import pinecone
14
+
15
+ # Set the path where you want to save the uploaded PDF file
16
+ SAVE_DIR = "pdfs"
17
+
18
+
19
+ st.header('Question Answering with your PDF file')
20
+ st.write("Are you interested in chatting with your own documents, whether it is a text file, a PDF, or a website? LangChain makes it easy for you to do question answering with your documents.")
21
+ def qa(file, query, chain_type, k,api_key_pinecode,index_name,environment_pinecode):
22
+ # load document
23
+ loader = PyPDFLoader(file)
24
+ #loader = UnstructuredPDFLoader(file)
25
+ #loader = OnlinePDFLoader("https://wolfpaulus.com/wp-content/uploads/2017/05/field-guide-to-data-science.pdf")
26
+ documents = loader.load()
27
+ #print("doccs",documents)
28
+ # split the documents into chunks
29
+ # text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
30
+ # texts = text_splitter.split_documents(documents)
31
+
32
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
33
+ texts = text_splitter.split_documents(documents)
34
+ # select which embeddings we want to use
35
+ embeddings = OpenAIEmbeddings()
36
+ # create the vectorestore to use as the index
37
+ # initialize pinecone
38
+ pinecone.init(
39
+ api_key=api_key_pinecode, #"8e9ae07c-dc96-466e-b1a1-519e2ccb4705", # find at app.pinecone.io
40
+ environment=environment_pinecode #"northamerica-northeast1-gcp" # next to api key in console
41
+ )
42
+
43
+ #index_name = "openaiindex"
44
+ index_name = index_name
45
+ #db = Chroma.from_documents(texts, embeddings)
46
+ #db = Pinecone.from_texts(texts, embeddings)
47
+ db = Pinecone.from_texts([t.page_content for t in texts], embeddings, index_name=index_name)
48
+ # expose this index in a retriever interface
49
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
50
+ # create a chain to answer questions
51
+ qa = RetrievalQA.from_chain_type(
52
+ llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
53
+ result = qa({"query": query})
54
+ print(result['result'])
55
+ return result
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+ with st.sidebar:
66
+ st.header('Configurations')
67
+ st.write("Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account).")
68
+ apikey = st.text_input("Enter your OpenAI API Key here")
69
+ os.environ["OPENAI_API_KEY"] = apikey
70
+
71
+ st.write("Enter Pinecode API key. [Pinecode](https://www.pinecone.io/).")
72
+
73
+ apikey2 = st.text_input("Enter your Pinecone Key here")
74
+
75
+ enviroment_pinecode = st.text_input("Enter your Pinecone your environment Key")
76
+
77
+ index_name = st.text_input("enter index-name")
78
+
79
+
80
+
81
+
82
+
83
+ left_column, right_column = st.columns(2)
84
+ # You can use a column just like st.sidebar:
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+ with left_column:
94
+
95
+ # Add a file uploader to the app
96
+ uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
97
+
98
+ # Check if a file has been uploaded
99
+ if uploaded_file is not None:
100
+ # Save the uploaded file to the specified directory
101
+ file_path = os.path.join(SAVE_DIR, uploaded_file.name)
102
+ # with open(file_path, "wb") as f:
103
+ # f.write(uploaded_file.getbuffer())
104
+ st.success(f"File path {file_path}")
105
+ query = st.text_input("enter your question")
106
+ chain_type = st.selectbox(
107
+ 'chain type',
108
+ ('stuff', 'map_reduce', "refine", "map_rerank"))
109
+ k = st.slider('Number of relevant chunks', 1, 5)
110
+
111
+ if st.button('Loading'):
112
+ # Or even better, call Streamlit functions inside a "with" block:
113
+ result=qa(file_path, query, chain_type, k, apikey2, index_name, enviroment_pinecode)
114
+
115
+
116
+
117
+
118
+ with right_column:
119
+
120
+ st.write("Output of your question")
121
+
122
+ #st.write(result)
123
+
124
+ #st.write(result['result'])
125
+ st.subheader("Result")
126
+ st.write(result['result'])
127
+
128
+ st.subheader("source_documents")
129
+ st.write(result['source_documents'][0])
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
requirements.txt ADDED
Binary file (3.88 kB). View file