Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,112 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from langchain_community.document_loaders import PyPDFLoader
|
3 |
-
from langchain_core.messages import HumanMessage, AIMessageChunk, AIMessage
|
4 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
-
from langchain_core.vectorstores import InMemoryVectorStore
|
7 |
-
import os
|
8 |
-
from langchain_core.chat_history import InMemoryChatMessageHistory, BaseChatMessageHistory
|
9 |
-
import time
|
10 |
-
from graph import get_graph
|
11 |
-
|
12 |
-
if 'read_file' not in st.session_state:
|
13 |
-
st.session_state.read_file = False
|
14 |
-
st.session_state.retriever = None
|
15 |
-
|
16 |
-
if 'chat_history' not in st.session_state:
|
17 |
-
st.session_state.chat_history = {}
|
18 |
-
st.session_state.first_msg = True
|
19 |
-
|
20 |
-
def get_session_by_id(session_id: str) -> BaseChatMessageHistory:
|
21 |
-
if session_id not in st.session_state.chat_history:
|
22 |
-
st.session_state.chat_history[session_id] = InMemoryChatMessageHistory()
|
23 |
-
return st.session_state.chat_history[session_id]
|
24 |
-
return st.session_state.chat_history[session_id]
|
25 |
-
|
26 |
-
if not st.session_state.read_file:
|
27 |
-
st.title('π€ Upload your PDF to talk with it', anchor=False)
|
28 |
-
file = st.file_uploader('Upload a PDF file', type='pdf')
|
29 |
-
if file:
|
30 |
-
with st.status('π€ Booting up the things!', expanded=True):
|
31 |
-
with st.spinner('π Uploading the PDF...', show_time=True):
|
32 |
-
with open('file.pdf', 'wb') as f:
|
33 |
-
f.write(file.read())
|
34 |
-
loader = PyPDFLoader('file.pdf')
|
35 |
-
documents = loader.load_and_split(RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200))
|
36 |
-
st.success('π File uploaded successfully!!!')
|
37 |
-
with st.spinner('π§ Reading the file...', show_time=True):
|
38 |
-
vstore = InMemoryVectorStore.from_documents(documents, HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2'))
|
39 |
-
st.session_state.retriever = vstore.as_retriever()
|
40 |
-
st.success('π§ File read successfully!!!')
|
41 |
-
os.remove('file.pdf')
|
42 |
-
with st.spinner('π΄ Waking up the LLM...', show_time=True):
|
43 |
-
st.session_state.graph = get_graph(st.session_state.retriever)
|
44 |
-
st.success('π LLM awakened!!!')
|
45 |
-
st.balloons()
|
46 |
-
placeholder = st.empty()
|
47 |
-
for _ in range(5, -1, -1):
|
48 |
-
placeholder.write(f'β³ Chat starting in 0{_} sec.')
|
49 |
-
time.sleep(1)
|
50 |
-
st.session_state.read_file = True
|
51 |
-
st.rerun()
|
52 |
-
|
53 |
-
if st.session_state.read_file:
|
54 |
-
|
55 |
-
st.title('π€ DocAI', anchor=False)
|
56 |
-
st.subheader('Chat with your document!', anchor=False)
|
57 |
-
|
58 |
-
if st.session_state.first_msg:
|
59 |
-
st.session_state.first_msg = False
|
60 |
-
get_session_by_id('chat42').add_message(AIMessage(content='Hello, how are you? How about we talk about the '
|
61 |
-
'document you sent me to read?'))
|
62 |
-
|
63 |
-
for msg in get_session_by_id('chat42').messages:
|
64 |
-
with st.chat_message(name='user' if isinstance(msg, HumanMessage) else 'ai'):
|
65 |
-
st.write(msg.content)
|
66 |
-
|
67 |
-
prompt = st.chat_input('Try to ask something about your file!')
|
68 |
-
if prompt:
|
69 |
-
with st.chat_message(name='user'):
|
70 |
-
st.write(prompt)
|
71 |
-
|
72 |
-
response = st.session_state.graph.stream(
|
73 |
-
{
|
74 |
-
'question': prompt,
|
75 |
-
'scratchpad': None,
|
76 |
-
'answer': None,
|
77 |
-
'next_node': None,
|
78 |
-
'history': get_session_by_id('chat42').messages,
|
79 |
-
},
|
80 |
-
stream_mode='messages'
|
81 |
-
)
|
82 |
-
|
83 |
-
get_session_by_id('chat42').add_message(HumanMessage(content=prompt))
|
84 |
-
|
85 |
-
def get_message():
|
86 |
-
for chunk, _ in response:
|
87 |
-
if chunk.content and isinstance(chunk, AIMessageChunk):
|
88 |
-
yield chunk.content
|
89 |
-
|
90 |
-
with st.chat_message(name='ai'):
|
91 |
-
full_response = ''
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
get_session_by_id('chat42').add_message(AIMessage(content=full_response))
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
3 |
+
from langchain_core.messages import HumanMessage, AIMessageChunk, AIMessage
|
4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
+
from langchain_core.vectorstores import InMemoryVectorStore
|
7 |
+
import os
|
8 |
+
from langchain_core.chat_history import InMemoryChatMessageHistory, BaseChatMessageHistory
|
9 |
+
import time
|
10 |
+
from graph import get_graph
|
11 |
+
|
12 |
+
if 'read_file' not in st.session_state:
|
13 |
+
st.session_state.read_file = False
|
14 |
+
st.session_state.retriever = None
|
15 |
+
|
16 |
+
if 'chat_history' not in st.session_state:
|
17 |
+
st.session_state.chat_history = {}
|
18 |
+
st.session_state.first_msg = True
|
19 |
+
|
20 |
+
def get_session_by_id(session_id: str) -> BaseChatMessageHistory:
|
21 |
+
if session_id not in st.session_state.chat_history:
|
22 |
+
st.session_state.chat_history[session_id] = InMemoryChatMessageHistory()
|
23 |
+
return st.session_state.chat_history[session_id]
|
24 |
+
return st.session_state.chat_history[session_id]
|
25 |
+
|
26 |
+
if not st.session_state.read_file:
|
27 |
+
st.title('π€ Upload your PDF to talk with it', anchor=False)
|
28 |
+
file = st.file_uploader('Upload a PDF file', type='pdf')
|
29 |
+
if file:
|
30 |
+
with st.status('π€ Booting up the things!', expanded=True):
|
31 |
+
with st.spinner('π Uploading the PDF...', show_time=True):
|
32 |
+
with open('file.pdf', 'wb') as f:
|
33 |
+
f.write(file.read())
|
34 |
+
loader = PyPDFLoader('file.pdf')
|
35 |
+
documents = loader.load_and_split(RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200))
|
36 |
+
st.success('π File uploaded successfully!!!')
|
37 |
+
with st.spinner('π§ Reading the file...', show_time=True):
|
38 |
+
vstore = InMemoryVectorStore.from_documents(documents, HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2'))
|
39 |
+
st.session_state.retriever = vstore.as_retriever()
|
40 |
+
st.success('π§ File read successfully!!!')
|
41 |
+
os.remove('file.pdf')
|
42 |
+
with st.spinner('π΄ Waking up the LLM...', show_time=True):
|
43 |
+
st.session_state.graph = get_graph(st.session_state.retriever)
|
44 |
+
st.success('π LLM awakened!!!')
|
45 |
+
st.balloons()
|
46 |
+
placeholder = st.empty()
|
47 |
+
for _ in range(5, -1, -1):
|
48 |
+
placeholder.write(f'β³ Chat starting in 0{_} sec.')
|
49 |
+
time.sleep(1)
|
50 |
+
st.session_state.read_file = True
|
51 |
+
st.rerun()
|
52 |
+
|
53 |
+
if st.session_state.read_file:
|
54 |
+
|
55 |
+
st.title('π€ DocAI', anchor=False)
|
56 |
+
st.subheader('Chat with your document!', anchor=False)
|
57 |
+
|
58 |
+
if st.session_state.first_msg:
|
59 |
+
st.session_state.first_msg = False
|
60 |
+
get_session_by_id('chat42').add_message(AIMessage(content='Hello, how are you? How about we talk about the '
|
61 |
+
'document you sent me to read?'))
|
62 |
+
|
63 |
+
for msg in get_session_by_id('chat42').messages:
|
64 |
+
with st.chat_message(name='user' if isinstance(msg, HumanMessage) else 'ai'):
|
65 |
+
st.write(msg.content)
|
66 |
+
|
67 |
+
prompt = st.chat_input('Try to ask something about your file!')
|
68 |
+
if prompt:
|
69 |
+
with st.chat_message(name='user'):
|
70 |
+
st.write(prompt)
|
71 |
+
|
72 |
+
response = st.session_state.graph.stream(
|
73 |
+
{
|
74 |
+
'question': prompt,
|
75 |
+
'scratchpad': None,
|
76 |
+
'answer': None,
|
77 |
+
'next_node': None,
|
78 |
+
'history': get_session_by_id('chat42').messages,
|
79 |
+
},
|
80 |
+
stream_mode='messages'
|
81 |
+
)
|
82 |
+
|
83 |
+
get_session_by_id('chat42').add_message(HumanMessage(content=prompt))
|
84 |
+
|
85 |
+
def get_message():
|
86 |
+
for chunk, _ in response:
|
87 |
+
if chunk.content and isinstance(chunk, AIMessageChunk):
|
88 |
+
yield chunk.content
|
89 |
+
|
90 |
+
with st.chat_message(name='ai'):
|
91 |
+
full_response = ''
|
92 |
+
tool_placeholder = st.empty()
|
93 |
+
placeholders = {}
|
94 |
+
prompt_message_placeholder = st.empty()
|
95 |
+
|
96 |
+
for msg in get_message():
|
97 |
+
full_response += msg
|
98 |
+
if '<tool>' in full_response:
|
99 |
+
with tool_placeholder.status('Reading document...', expanded=True):
|
100 |
+
if 'tool_message_placeholder' not in placeholders:
|
101 |
+
placeholders['tool_message_placeholder'] = st.empty()
|
102 |
+
placeholders['tool_message_placeholder'].write(full_response
|
103 |
+
.replace('<tool>', '')
|
104 |
+
.replace('</tool>', '')
|
105 |
+
.replace('retriever', 'Retrieving document'))
|
106 |
+
prompt_message_placeholder.empty()
|
107 |
+
if '</tool>' in full_response:
|
108 |
+
full_response = ''
|
109 |
+
continue
|
110 |
+
else:
|
111 |
+
prompt_message_placeholder.write(full_response)
|
112 |
get_session_by_id('chat42').add_message(AIMessage(content=full_response))
|