Spaces:
Sleeping
Sleeping
Silence1412
commited on
Commit
•
585e73d
1
Parent(s):
ceb2aec
Update Chat_with_pdf_LLM.py
Browse files- Chat_with_pdf_LLM.py +26 -21
Chat_with_pdf_LLM.py
CHANGED
@@ -40,27 +40,32 @@ def LLM_pdf(model_name = 'google/flan-t5-large'):
|
|
40 |
if 'past' not in st.session_state:
|
41 |
st.session_state['past'] = []
|
42 |
# print(st.session_state['generated'],st.session_state['past'])
|
|
|
|
|
43 |
|
44 |
# show user input
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
st.session_state.past.append(user_question)
|
58 |
-
st.session_state.generated.append(response)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if 'past' not in st.session_state:
|
41 |
st.session_state['past'] = []
|
42 |
# print(st.session_state['generated'],st.session_state['past'])
|
43 |
+
|
44 |
+
chat_placeholder = st.empty()
|
45 |
|
46 |
# show user input
|
47 |
+
with st.container():
|
48 |
+
input_placeholder = st.empty()
|
49 |
+
user_question = input_placeholder.text_input("Ask a question about your PDF:")
|
50 |
+
if user_question:
|
51 |
+
docs = knowledge_base.similarity_search(user_question)
|
52 |
+
|
53 |
+
llm = HuggingFaceHub(repo_id=model_name, model_kwargs={"temperature":5,
|
54 |
+
"max_length":64})
|
55 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
56 |
+
response = chain.run(input_documents=docs,question=user_question)
|
57 |
+
|
58 |
+
#st.write(response)
|
|
|
|
|
59 |
|
60 |
+
# append user_input and output to state
|
61 |
+
st.session_state.past.append(user_question)
|
62 |
+
st.session_state.generated.append(response)
|
63 |
+
|
64 |
+
with chat_placeholder.container():
|
65 |
+
# If responses have been generated by the model
|
66 |
+
if st.session_state['generated']:
|
67 |
+
# Reverse iteration through the list
|
68 |
+
for i in range(len(st.session_state['generated'])-1, -1, -1):
|
69 |
+
# message from streamlit_chat
|
70 |
+
message(st.session_state['past'][::-1][i], is_user=True, key=str(i) + '_user')
|
71 |
+
message(st.session_state['generated'][::-1][i], key=str(i))
|