ogegadavis254 commited on
Commit
399202c
·
verified ·
1 Parent(s): 85720d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -36
app.py CHANGED
@@ -8,20 +8,17 @@ import io
8
  from langchain.text_splitter import CharacterTextSplitter
9
  from langchain.embeddings import HuggingFaceEmbeddings
10
  from langchain.vectorstores import FAISS
11
- from langchain.memory import ConversationBufferMemory
12
- from langchain.chains import ConversationalRetrievalChain
13
- from langchain.llms import HuggingFaceHub
14
 
15
  load_dotenv()
16
 
17
  # Initialize session state variables
18
- if "conversation" not in st.session_state:
19
- st.session_state.conversation = None
20
  if "chat_history" not in st.session_state:
21
  st.session_state.chat_history = []
22
 
23
  def reset_conversation():
24
- st.session_state.conversation = None
25
  st.session_state.chat_history = []
26
 
27
  def get_pdf_text(pdf_docs):
@@ -47,38 +44,59 @@ def get_vectorstore(text_chunks):
47
  vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
48
  return vectorstore
49
 
50
- def get_conversation_chain(vectorstore):
51
- llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
52
-
53
- memory = ConversationBufferMemory(
54
- memory_key='chat_history', return_messages=True)
55
- conversation_chain = ConversationalRetrievalChain.from_llm(
56
- llm=llm,
57
- retriever=vectorstore.as_retriever(),
58
- memory=memory
59
- )
60
- return conversation_chain
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def handle_userinput(user_question):
63
- response = st.session_state.conversation({'question': user_question})
64
- st.session_state.chat_history = response['chat_history']
65
-
66
- for i, message in enumerate(st.session_state.chat_history):
67
- if i % 2 == 0:
68
- st.write(user_template.replace(
69
- "{{MSG}}", message.content), unsafe_allow_html=True)
70
- else:
71
- st.write(bot_template.replace(
72
- "{{MSG}}", message.content), unsafe_allow_html=True)
 
73
 
74
  # Streamlit application
75
  st.set_page_config(page_title="Chat with your PDFs", page_icon=":books:")
76
 
77
  st.header("Chat with your PDFs :books:")
78
 
79
- user_template = '<div style="background-color: #e6f3ff; padding: 10px; border-radius: 5px; margin-bottom: 10px;"><strong>Human:</strong> {{MSG}}</div>'
80
- bot_template = '<div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px; margin-bottom: 10px;"><strong>AI:</strong> {{MSG}}</div>'
81
-
82
  # Sidebar
83
  with st.sidebar:
84
  st.subheader("Your documents")
@@ -92,17 +110,26 @@ with st.sidebar:
92
  text_chunks = get_text_chunks(raw_text)
93
 
94
  # Create vector store
95
- vectorstore = get_vectorstore(text_chunks)
96
-
97
- # Create conversation chain
98
- st.session_state.conversation = get_conversation_chain(vectorstore)
99
 
100
  st.button('Reset Chat', on_click=reset_conversation)
101
 
102
  # Main chat interface
103
- if st.session_state.conversation is None:
104
  st.write("Please upload PDF documents and click 'Process' to start chatting.")
105
  else:
106
  user_question = st.text_input("Ask a question about your documents:")
107
  if user_question:
108
- handle_userinput(user_question)
 
 
 
 
 
 
 
 
 
 
 
8
  from langchain.text_splitter import CharacterTextSplitter
9
  from langchain.embeddings import HuggingFaceEmbeddings
10
  from langchain.vectorstores import FAISS
 
 
 
11
 
12
  load_dotenv()
13
 
14
  # Initialize session state variables
15
+ if "vectorstore" not in st.session_state:
16
+ st.session_state.vectorstore = None
17
  if "chat_history" not in st.session_state:
18
  st.session_state.chat_history = []
19
 
20
  def reset_conversation():
21
+ st.session_state.vectorstore = None
22
  st.session_state.chat_history = []
23
 
24
  def get_pdf_text(pdf_docs):
 
44
  vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
45
  return vectorstore
46
 
47
+ def get_together_response(prompt, history):
48
+ url = "https://api.together.xyz/v1/chat/completions"
49
+ model_link = "NousResearch/Nous-Hermes-2-Yi-34B"
50
+
51
+ messages = [{"role": "system", "content": "You are an AI assistant that helps users understand the content of their PDFs. Provide concise and relevant answers based on the information in the documents."}]
52
+
53
+ for human, ai in history:
54
+ messages.append({"role": "user", "content": human})
55
+ messages.append({"role": "assistant", "content": ai})
56
+
57
+ messages.append({"role": "user", "content": prompt})
58
+
59
+ payload = {
60
+ "model": model_link,
61
+ "messages": messages,
62
+ "temperature": 0.7,
63
+ "top_p": 0.95,
64
+ "top_k": 50,
65
+ "repetition_penalty": 1,
66
+ "max_tokens": 1024
67
+ }
68
+
69
+ headers = {
70
+ "accept": "application/json",
71
+ "content-type": "application/json",
72
+ "Authorization": f"Bearer {os.getenv('TOGETHER_API_KEY')}"
73
+ }
74
+
75
+ try:
76
+ response = requests.post(url, json=payload, headers=headers)
77
+ response.raise_for_status()
78
+ return response.json()['choices'][0]['message']['content']
79
+ except requests.exceptions.RequestException as e:
80
+ return f"Error: {str(e)}"
81
 
82
  def handle_userinput(user_question):
83
+ if st.session_state.vectorstore:
84
+ docs = st.session_state.vectorstore.similarity_search(user_question)
85
+ context = "\n".join([doc.page_content for doc in docs])
86
+ prompt = f"Context from PDFs:\n{context}\n\nQuestion: {user_question}\nAnswer:"
87
+
88
+ response = get_together_response(prompt, st.session_state.chat_history)
89
+ st.session_state.chat_history.append((user_question, response))
90
+
91
+ return response
92
+ else:
93
+ return "Please upload and process PDF documents first."
94
 
95
  # Streamlit application
96
  st.set_page_config(page_title="Chat with your PDFs", page_icon=":books:")
97
 
98
  st.header("Chat with your PDFs :books:")
99
 
 
 
 
100
  # Sidebar
101
  with st.sidebar:
102
  st.subheader("Your documents")
 
110
  text_chunks = get_text_chunks(raw_text)
111
 
112
  # Create vector store
113
+ st.session_state.vectorstore = get_vectorstore(text_chunks)
114
+
115
+ st.success("PDFs processed successfully!")
 
116
 
117
  st.button('Reset Chat', on_click=reset_conversation)
118
 
119
  # Main chat interface
120
+ if st.session_state.vectorstore is None:
121
  st.write("Please upload PDF documents and click 'Process' to start chatting.")
122
  else:
123
  user_question = st.text_input("Ask a question about your documents:")
124
  if user_question:
125
+ response = handle_userinput(user_question)
126
+
127
+ st.write("Human: " + user_question)
128
+ st.write("AI: " + response)
129
+
130
+ # Display chat history
131
+ st.subheader("Chat History")
132
+ for human, ai in st.session_state.chat_history:
133
+ st.write("Human: " + human)
134
+ st.write("AI: " + ai)
135
+ st.write("---")