zahraanaji commited on
Commit
5c6a202
·
verified ·
1 Parent(s): b0c936d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -47
app.py CHANGED
@@ -2,20 +2,17 @@ from langchain_openai import ChatOpenAI
2
  from langchain_community.embeddings import HuggingFaceEmbeddings
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  from langchain_community.vectorstores import Chroma
5
- from langchain_community.document_loaders import PyPDFLoader
6
- from langchain_community.document_loaders import PDFPlumberLoader
7
  from langchain.chains import ConversationalRetrievalChain
8
  from langchain_community.chat_message_histories import ChatMessageHistory
9
  from langchain.memory import ConversationBufferMemory
10
  from langchain_core.prompts import PromptTemplate
 
 
11
  import streamlit as st
12
  import os
13
  from io import BytesIO
14
- from langchain_core.document_loaders import BaseLoader
15
- from langchain_core.documents import Document
16
  import pdfplumber
17
 
18
-
19
  class InMemoryPDFLoader(BaseLoader):
20
  def __init__(self, file_bytes: bytes):
21
  self.file_bytes = file_bytes
@@ -30,7 +27,6 @@ class InMemoryPDFLoader(BaseLoader):
30
 
31
  # Access the OpenAI API key from the environment
32
  open_ai_key = os.getenv("OPENAI_API_KEY")
33
-
34
  llm = ChatOpenAI(api_key=open_ai_key)
35
 
36
  template = """Use the following pieces of information to answer the user's question.
@@ -45,71 +41,51 @@ Helpful answer:
45
 
46
  prompt = PromptTemplate(template=template, input_variables=["context", "question"])
47
 
48
- with st.chat_message("user"):
49
- pdf_file = st.file_uploader("Upload your pdf",type="pdf")
50
-
51
-
52
- # upload PDF
53
- # pdf_file = st.file_uploader("Upload your pdf",type="pdf")
54
  question = st.chat_input("Ask your question")
55
 
56
-
57
  if pdf_file is not None:
58
- try:
59
  pdf_bytes = pdf_file.read()
60
  loader = InMemoryPDFLoader(file_bytes=pdf_bytes)
61
- # Load and process the PDF
62
- # loader = PDFPlumberLoader(pdf_file)
63
  pdf_data = loader.load()
64
-
65
  # Split the text into chunks
66
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
67
  docs = text_splitter.split_documents(pdf_data)
68
-
69
  # Create a Chroma vector store
70
  embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
71
  db = Chroma.from_documents(docs, embeddings)
72
-
73
  # Initialize message history for conversation
74
  message_history = ChatMessageHistory()
75
-
76
  # Memory for conversational context
77
  memory = ConversationBufferMemory(
78
- memory_key="chat_history",
79
- output_key="answer",
80
- chat_memory=message_history,
81
- return_messages=True,
82
- )
83
-
84
  # Create a chain that uses the Chroma vector store
85
  chain = ConversationalRetrievalChain.from_llm(
86
- llm=llm,
87
- chain_type="stuff",
88
- retriever=db.as_retriever(),
89
- memory=memory,
90
- return_source_documents=False,
91
- combine_docs_chain_kwargs={'prompt': prompt}
92
- )
93
- st.write("you can ask ")
94
  if question:
95
- with st.chat_message("user"):
96
-
97
  st.markdown(question)
98
-
99
-
100
  with st.chat_message("assistant"):
101
- # Process the question
102
  res = chain({"question": question})
103
  answer = res["answer"]
104
  st.write(f"{answer}")
105
-
106
- # Process the question
107
- # res = chain({"question": question})
108
- # answer = res["answer"]
109
- st.write_stream(chain({"question": question})["answer"])
110
- # st.markdoun(answer)
111
- # st.text(f"{answer}")
112
 
113
  except Exception as e:
114
  st.error(f"An error occurred: {e}")
115
-
 
2
  from langchain_community.embeddings import HuggingFaceEmbeddings
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  from langchain_community.vectorstores import Chroma
 
 
5
  from langchain.chains import ConversationalRetrievalChain
6
  from langchain_community.chat_message_histories import ChatMessageHistory
7
  from langchain.memory import ConversationBufferMemory
8
  from langchain_core.prompts import PromptTemplate
9
+ from langchain_core.document_loaders import BaseLoader
10
+ from langchain_core.documents import Document
11
  import streamlit as st
12
  import os
13
  from io import BytesIO
 
 
14
  import pdfplumber
15
 
 
16
  class InMemoryPDFLoader(BaseLoader):
17
  def __init__(self, file_bytes: bytes):
18
  self.file_bytes = file_bytes
 
27
 
28
  # Access the OpenAI API key from the environment
29
  open_ai_key = os.getenv("OPENAI_API_KEY")
 
30
  llm = ChatOpenAI(api_key=open_ai_key)
31
 
32
  template = """Use the following pieces of information to answer the user's question.
 
41
 
42
  prompt = PromptTemplate(template=template, input_variables=["context", "question"])
43
 
44
+ pdf_file = st.file_uploader("Upload your PDF", type="pdf")
 
 
 
 
 
45
  question = st.chat_input("Ask your question")
46
 
 
47
  if pdf_file is not None:
48
+ try:
49
  pdf_bytes = pdf_file.read()
50
  loader = InMemoryPDFLoader(file_bytes=pdf_bytes)
 
 
51
  pdf_data = loader.load()
52
+
53
  # Split the text into chunks
54
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
55
  docs = text_splitter.split_documents(pdf_data)
56
+
57
  # Create a Chroma vector store
58
  embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
59
  db = Chroma.from_documents(docs, embeddings)
60
+
61
  # Initialize message history for conversation
62
  message_history = ChatMessageHistory()
63
+
64
  # Memory for conversational context
65
  memory = ConversationBufferMemory(
66
+ memory_key="chat_history",
67
+ output_key="answer",
68
+ chat_memory=message_history,
69
+ return_messages=True,
70
+ )
71
+
72
  # Create a chain that uses the Chroma vector store
73
  chain = ConversationalRetrievalChain.from_llm(
74
+ llm=llm,
75
+ chain_type="stuff",
76
+ retriever=db.as_retriever(),
77
+ memory=memory,
78
+ return_source_documents=False,
79
+ combine_docs_chain_kwargs={'prompt': prompt}
80
+ )
81
+
82
  if question:
83
+ with st.chat_message("user"):
 
84
  st.markdown(question)
 
 
85
  with st.chat_message("assistant"):
 
86
  res = chain({"question": question})
87
  answer = res["answer"]
88
  st.write(f"{answer}")
 
 
 
 
 
 
 
89
 
90
  except Exception as e:
91
  st.error(f"An error occurred: {e}")