Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,13 +61,18 @@ def handle_query(query):
|
|
| 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 |
-
|
| 67 |
elif isinstance(answer, dict) and 'response' in answer:
|
| 68 |
-
|
| 69 |
else:
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
# Streamlit app initialization
|
|
@@ -77,7 +82,12 @@ st.markdown("chat here👇")
|
|
| 77 |
|
| 78 |
if 'messages' not in st.session_state:
|
| 79 |
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
with st.sidebar:
|
| 82 |
st.title("Menu:")
|
| 83 |
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
|
|
@@ -93,9 +103,19 @@ with st.sidebar:
|
|
| 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 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
for message in st.session_state.messages:
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
| 63 |
answer = query_engine.query(query)
|
| 64 |
+
|
| 65 |
+
final_ans = []
|
| 66 |
if hasattr(answer, 'response'):
|
| 67 |
+
final_ans.append(answer.response)
|
| 68 |
elif isinstance(answer, dict) and 'response' in answer:
|
| 69 |
+
final_ans.append(answer['response'])
|
| 70 |
else:
|
| 71 |
+
final_ans.append("Sorry, I couldn't find an answer.")
|
| 72 |
+
|
| 73 |
+
ans = " ".join(final_ans)
|
| 74 |
+
for i in ans:
|
| 75 |
+
yiled str(i)
|
| 76 |
|
| 77 |
|
| 78 |
# Streamlit app initialization
|
|
|
|
| 82 |
|
| 83 |
if 'messages' not in st.session_state:
|
| 84 |
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
|
| 85 |
+
|
| 86 |
+
# Display or clear chat messages
|
| 87 |
+
for message in st.session_state.messages:
|
| 88 |
+
with st.chat_message(message["role"]):
|
| 89 |
+
st.write(message["content"])
|
| 90 |
+
|
| 91 |
with st.sidebar:
|
| 92 |
st.title("Menu:")
|
| 93 |
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
|
|
|
|
| 103 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
| 104 |
if user_prompt:
|
| 105 |
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
| 106 |
+
with st.chat_message("user", avatar="⛷️"):
|
| 107 |
+
st.write(prompt)
|
| 108 |
+
# response = handle_query(user_prompt)
|
| 109 |
+
# st.session_state.messages.append({'role': 'assistant', "content": response})
|
| 110 |
|
| 111 |
+
# for message in st.session_state.messages:
|
| 112 |
+
# with st.chat_message(message['role']):
|
| 113 |
+
# st.write(message['content'])
|
| 114 |
+
|
| 115 |
+
# Generate a new response if last message is not from assistant
|
| 116 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 117 |
+
with st.chat_message("assistant"):
|
| 118 |
+
response = handle_query(user_prompt)
|
| 119 |
+
full_response = st.write_stream(response)
|
| 120 |
+
message = {"role": "assistant", "content": full_response}
|
| 121 |
+
st.session_state.messages.append(message)
|