satyam001 commited on
Commit
6aaa040
Β·
1 Parent(s): 008e424

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -63
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import streamlit as st
3
  # from dotenv import load_dotenv
4
  import pickle
@@ -16,85 +15,93 @@ from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddi
16
  from langchain.embeddings import HuggingFaceHubEmbeddings
17
  # Sidebar contents
18
  from langchain.llms import HuggingFaceHub
 
 
 
 
 
19
  with st.sidebar:
20
  st.title('πŸ€—πŸ’¬ LLM Chat App')
21
  st.markdown('''
22
  ## About
23
- This app is an LLM-powered chatbot built using:
24
- - [Streamlit](https://streamlit.io/)
25
- - [LangChain](https://python.langchain.com/)
26
- - [OpenAI](https://platform.openai.com/docs/models) LLM model
27
 
28
  ''')
29
  add_vertical_space(5)
30
- st.write(
31
- 'Made with ❀️ by [Prompt Engineer](https://youtube.com/@engineerprompt)')
 
 
32
 
33
  # load_dotenv()
34
 
35
- os.environ['OPEN_AI_APIKEY'] = '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'
36
 
37
 
38
  def main():
39
  st.header("Chat with PDF πŸ’¬")
40
-
41
- # upload a PDF file
42
  pdf = st.file_uploader("Upload your PDF", type='pdf')
43
 
44
- # st.write(pdf)
45
- if pdf is not None:
46
- pdf_reader = PdfReader(pdf)
47
-
48
- text = ""
49
- for page in pdf_reader.pages:
50
- text += page.extract_text()
51
-
52
- text_splitter = RecursiveCharacterTextSplitter(
53
- chunk_size=1000,
54
- chunk_overlap=200,
55
- length_function=len
56
- )
57
- chunks = text_splitter.split_text(text=text)
58
-
59
- # # embeddings
60
- store_name = pdf.name[:-4]
61
- st.write(f'{store_name}')
62
- # st.write(chunks)
63
-
64
- if os.path.exists(f"{store_name}.pkl"):
65
- with open(f"{store_name}.pkl", "rb") as f:
66
- VectorStore = pickle.load(f)
67
- # st.write('Embeddings Loaded from the Disk')s
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  else:
69
- # embeddings = OpenAIEmbeddings(
70
- # openai_api_key='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')
71
- # embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
72
- embeddings = HuggingFaceHubEmbeddings(
73
- huggingfacehub_api_token='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')
74
- VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
75
- # VectorStore=Chroma.from_documents(chunks, embeddings)
76
- with open(f"{store_name}.pkl", "wb") as f:
77
- pickle.dump(VectorStore, f)
78
-
79
- # embeddings = OpenAIEmbeddings()
80
- # VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
81
-
82
- # Accept user questions/query
83
- query = st.text_input("Ask questions about your PDF file:")
84
- # st.write(query)
85
-
86
- if query:
87
- docs = VectorStore.similarity_search(query=query, k=3)
88
- llm = HuggingFaceHub(repo_id='google/flan-ul2',
89
- huggingfacehub_api_token='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', model_kwargs={"temperature": 0.1, "max_new_tokens": 500})
90
- # llm = OpenAI()
91
- chain = load_qa_chain(llm=llm, chain_type="stuff")
92
- response = chain.run(input_documents=docs, question=query)
93
- # with get_openai_callback() as cb:
94
- # response = chain.run(input_documents=docs, question=query)
95
- # print(cb)
96
- st.write(response)
97
 
98
 
99
  if __name__ == '__main__':
100
- main()
 
 
1
  import streamlit as st
2
  # from dotenv import load_dotenv
3
  import pickle
 
15
  from langchain.embeddings import HuggingFaceHubEmbeddings
16
  # Sidebar contents
17
  from langchain.llms import HuggingFaceHub
18
+
19
+
20
+ if 'HuggingFace_API_Key' not in st.session_state:
21
+ st.session_state['HuggingFace_API_Key'] = ''
22
+
23
  with st.sidebar:
24
  st.title('πŸ€—πŸ’¬ LLM Chat App')
25
  st.markdown('''
26
  ## About
27
+ This app is an LLM-powered chatbot
28
+ PDF:Chatbot AI-powered chat assistant for PDFs
 
 
29
 
30
  ''')
31
  add_vertical_space(5)
32
+
33
+
34
+ st.session_state['HuggingFace_API_Key'] = st.sidebar.text_input(
35
+ "What's your HuggingFace API key?", type="password")
36
 
37
  # load_dotenv()
38
 
39
+ load_button = st.sidebar.button("Load data to Pinecone", key="load_button")
40
 
41
 
42
  def main():
43
  st.header("Chat with PDF πŸ’¬")
 
 
44
  pdf = st.file_uploader("Upload your PDF", type='pdf')
45
 
46
+ if st.session_state['HuggingFace_API_Key'] != "":
47
+ # upload a PDF file
48
+ # st.write(pdf)
49
+ if pdf is not None:
50
+ pdf_reader = PdfReader(pdf)
51
+
52
+ text = ""
53
+ for page in pdf_reader.pages:
54
+ text += page.extract_text()
55
+ # st.write(text)
56
+ text_splitter = RecursiveCharacterTextSplitter(
57
+ chunk_size=1000,
58
+ chunk_overlap=200,
59
+ length_function=len
60
+ )
61
+ chunks = text_splitter.split_text(text=text)
62
+
63
+ # # embeddings
64
+ store_name = pdf.name[:-4]
65
+ st.write(f'{store_name}')
66
+ # st.write(chunks)
67
+
68
+ if os.path.exists(f"{store_name}.pkl"):
69
+ with open(f"{store_name}.pkl", "rb") as f:
70
+ VectorStore = pickle.load(f)
71
+ # st.write('Embeddings Loaded from the Disk')s
72
+ else:
73
+ # embeddings = OpenAIEmbeddings(
74
+ # openai_api_key='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')
75
+ # embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
76
+ embeddings = HuggingFaceHubEmbeddings(
77
+ huggingfacehub_api_token=st.session_state['HuggingFace_API_Key'])
78
+ VectorStore = FAISS.from_texts(
79
+ chunks, embedding=embeddings)
80
+ # VectorStore=Chroma.from_documents(chunks, embeddings)
81
+ with open(f"{store_name}.pkl", "wb") as f:
82
+ pickle.dump(VectorStore, f)
83
+
84
+ # embeddings = OpenAIEmbeddings()
85
+ # VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
86
+
87
+ # Accept user questions/query
88
+ query = st.text_input("Ask questions about your PDF file:")
89
+ # st.write(query)
90
+
91
+ if query:
92
+ docs = VectorStore.similarity_search(query=query, k=3)
93
+ llm = HuggingFaceHub(repo_id='google/flan-ul2',
94
+ huggingfacehub_api_token=st.session_state['HuggingFace_API_Key'], model_kwargs={"temperature": 0.1, "max_new_tokens": 500})
95
+ # llm = OpenAI()
96
+ chain = load_qa_chain(llm=llm, chain_type="stuff")
97
+ response = chain.run(input_documents=docs, question=query)
98
+ # with get_openai_callback() as cb:
99
+ # response = chain.run(input_documents=docs, question=query)
100
+ # print(cb)
101
+ st.write(response)
102
  else:
103
+ st.sidebar.error("Ooopssss!!! Please upload pdf...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
 
106
  if __name__ == '__main__':
107
+ main()