Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 "
|
19 |
-
st.session_state.
|
20 |
if "chat_history" not in st.session_state:
|
21 |
st.session_state.chat_history = []
|
22 |
|
23 |
def reset_conversation():
|
24 |
-
st.session_state.
|
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
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
def handle_userinput(user_question):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
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 |
-
|
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.
|
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("---")
|