Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
@@ -10,8 +10,8 @@ from langchain.document_loaders import PyPDFLoader
|
|
10 |
from langchain.document_loaders import TextLoader
|
11 |
from langchain.document_loaders import Docx2txtLoader
|
12 |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
13 |
-
from dotenv import load_dotenv # Add this line for loading environment variables
|
14 |
import os
|
|
|
15 |
import tempfile
|
16 |
|
17 |
load_dotenv()
|
@@ -36,22 +36,27 @@ def display_chat_history(chain):
|
|
36 |
container = st.container()
|
37 |
|
38 |
with container:
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
with st.
|
43 |
-
|
44 |
-
submit_button = st.form_submit_button(label='Send')
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
def create_conversational_chain(vector_store):
|
53 |
load_dotenv()
|
54 |
-
|
|
|
55 |
os.environ["REPLICATE_API_TOKEN"] = replicate_api_token
|
56 |
|
57 |
llm = Replicate(
|
@@ -98,8 +103,6 @@ def main():
|
|
98 |
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=100, length_function=len)
|
99 |
text_chunks = text_splitter.split_documents(text)
|
100 |
|
101 |
-
st.write("Text chunks lengths:", [len(chunk) for chunk in text_chunks])
|
102 |
-
|
103 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
104 |
model_kwargs={'device': 'cpu'})
|
105 |
vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)
|
|
|
10 |
from langchain.document_loaders import TextLoader
|
11 |
from langchain.document_loaders import Docx2txtLoader
|
12 |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
|
|
13 |
import os
|
14 |
+
from dotenv import load_dotenv
|
15 |
import tempfile
|
16 |
|
17 |
load_dotenv()
|
|
|
36 |
container = st.container()
|
37 |
|
38 |
with container:
|
39 |
+
with st.form(key='my_form', clear_on_submit=True):
|
40 |
+
user_input = st.text_input("Question:", placeholder="Ask about your Documents", key='input')
|
41 |
+
submit_button = st.form_submit_button(label='Send')
|
42 |
|
43 |
+
if submit_button and user_input:
|
44 |
+
with st.spinner('Generating response...'):
|
45 |
+
output = conversation_chat(user_input, chain, st.session_state['history'])
|
|
|
46 |
|
47 |
+
st.session_state['past'].append(user_input)
|
48 |
+
st.session_state['generated'].append(output)
|
49 |
+
|
50 |
+
if st.session_state['generated']:
|
51 |
+
with reply_container:
|
52 |
+
for i in range(len(st.session_state['generated'])):
|
53 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="thumbs")
|
54 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="fun-emoji")
|
55 |
|
56 |
def create_conversational_chain(vector_store):
|
57 |
load_dotenv()
|
58 |
+
|
59 |
+
replicate_api_token = "r8_AA3K1fhDykqLa5M74E5V0w5ss1z0P9S3foWJl"
|
60 |
os.environ["REPLICATE_API_TOKEN"] = replicate_api_token
|
61 |
|
62 |
llm = Replicate(
|
|
|
103 |
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=100, length_function=len)
|
104 |
text_chunks = text_splitter.split_documents(text)
|
105 |
|
|
|
|
|
106 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
107 |
model_kwargs={'device': 'cpu'})
|
108 |
vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)
|