Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -58,17 +58,16 @@ def handle_query(query):
|
|
58 |
)
|
59 |
]
|
60 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
return "Sorry, I couldn't find an answer."
|
72 |
|
73 |
|
74 |
# Streamlit app initialization
|
@@ -94,9 +93,16 @@ with st.sidebar:
|
|
94 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
95 |
if user_prompt:
|
96 |
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
|
|
|
|
|
|
97 |
response = handle_query(user_prompt)
|
|
|
|
|
|
|
|
|
98 |
st.session_state.messages.append({'role': 'assistant', "content": response})
|
99 |
|
100 |
-
for message in st.session_state.messages:
|
101 |
-
|
102 |
-
|
|
|
58 |
)
|
59 |
]
|
60 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
61 |
+
|
62 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
63 |
+
answer = query_engine.query(query)
|
64 |
+
|
65 |
+
if hasattr(answer, 'response'):
|
66 |
+
return answer.response
|
67 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
68 |
+
return answer['response']
|
69 |
+
else:
|
70 |
+
return "Sorry, I couldn't find an answer."
|
|
|
71 |
|
72 |
|
73 |
# Streamlit app initialization
|
|
|
93 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
94 |
if user_prompt:
|
95 |
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
96 |
+
for message in st.session_state.messages:
|
97 |
+
with st.chat_message(message['role']):
|
98 |
+
st.write(message['content'])
|
99 |
response = handle_query(user_prompt)
|
100 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
101 |
+
with st.chat_message("assistant"):
|
102 |
+
with st.spinner("Thinking..."):
|
103 |
+
st.write(response)
|
104 |
st.session_state.messages.append({'role': 'assistant', "content": response})
|
105 |
|
106 |
+
# for message in st.session_state.messages:
|
107 |
+
# with st.chat_message(message['role']):
|
108 |
+
# st.write(message['content'])
|