Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ Don't Try To Make Up An Answer. Don't Provide Anything Out Of The Given Context.
|
|
| 26 |
Context: {context}
|
| 27 |
Question: {question}
|
| 28 |
|
| 29 |
-
Start The Answer Directly
|
| 30 |
Consider Yourself As God Krishna And Answer The Question Result Should Not Start With "Answer"
|
| 31 |
""" # Keep your template here
|
| 32 |
|
|
@@ -37,7 +37,8 @@ def initialize_session_states():
|
|
| 37 |
"selected_question": None,
|
| 38 |
"show_predefined": True,
|
| 39 |
"last_response": None,
|
| 40 |
-
"translation_done": False
|
|
|
|
| 41 |
}
|
| 42 |
for key, val in session_defaults.items():
|
| 43 |
if key not in st.session_state:
|
|
@@ -124,33 +125,54 @@ def format_source_docs(source_documents):
|
|
| 124 |
|
| 125 |
def handle_user_input(prompt, qa_chain):
|
| 126 |
if prompt:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
with st.chat_message("user", avatar="🐿"):
|
| 128 |
st.markdown(prompt)
|
| 129 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 130 |
|
| 131 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
response = qa_chain.invoke({"query": prompt})
|
| 133 |
result = response["result"]
|
| 134 |
source_documents = response["source_documents"]
|
| 135 |
|
| 136 |
-
|
| 137 |
accumulated_text = ""
|
| 138 |
for char in result:
|
| 139 |
accumulated_text += char
|
| 140 |
response_placeholder.markdown(f'<div class="english-text">{accumulated_text}</div>', unsafe_allow_html=True)
|
| 141 |
time.sleep(0.01)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
st.session_state.last_response = accumulated_text
|
| 149 |
st.session_state.show_predefined = False
|
| 150 |
st.session_state.translation_done = False
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
except Exception as e:
|
| 153 |
st.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
def handle_translation():
|
| 156 |
if "last_response" in st.session_state and st.session_state.last_response:
|
|
|
|
| 26 |
Context: {context}
|
| 27 |
Question: {question}
|
| 28 |
|
| 29 |
+
Start The Answer Directly., Please. The Answer Should Contain All 3 Contexts.
|
| 30 |
Consider Yourself As God Krishna And Answer The Question Result Should Not Start With "Answer"
|
| 31 |
""" # Keep your template here
|
| 32 |
|
|
|
|
| 37 |
"selected_question": None,
|
| 38 |
"show_predefined": True,
|
| 39 |
"last_response": None,
|
| 40 |
+
"translation_done": False,
|
| 41 |
+
"last_prompt": None # Add this line
|
| 42 |
}
|
| 43 |
for key, val in session_defaults.items():
|
| 44 |
if key not in st.session_state:
|
|
|
|
| 125 |
|
| 126 |
def handle_user_input(prompt, qa_chain):
|
| 127 |
if prompt:
|
| 128 |
+
# Check if this prompt has already been processed
|
| 129 |
+
if st.session_state.get("last_prompt") == prompt:
|
| 130 |
+
return
|
| 131 |
+
|
| 132 |
+
# Store the current prompt to prevent reprocessing
|
| 133 |
+
st.session_state.last_prompt = prompt
|
| 134 |
+
|
| 135 |
with st.chat_message("user", avatar="🐿"):
|
| 136 |
st.markdown(prompt)
|
| 137 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 138 |
|
| 139 |
try:
|
| 140 |
+
# Add temporary assistant message
|
| 141 |
+
with st.chat_message("assistant", avatar="🪈"):
|
| 142 |
+
response_placeholder = st.empty()
|
| 143 |
+
|
| 144 |
+
# Process query and generate response
|
| 145 |
response = qa_chain.invoke({"query": prompt})
|
| 146 |
result = response["result"]
|
| 147 |
source_documents = response["source_documents"]
|
| 148 |
|
| 149 |
+
# Build response incrementally
|
| 150 |
accumulated_text = ""
|
| 151 |
for char in result:
|
| 152 |
accumulated_text += char
|
| 153 |
response_placeholder.markdown(f'<div class="english-text">{accumulated_text}</div>', unsafe_allow_html=True)
|
| 154 |
time.sleep(0.01)
|
| 155 |
|
| 156 |
+
# Update session state with final response
|
| 157 |
+
st.session_state.messages.append({
|
| 158 |
+
"role": "assistant",
|
| 159 |
+
"content": f'<div class="english-text">{accumulated_text}</div>',
|
| 160 |
+
"original": accumulated_text
|
| 161 |
+
})
|
| 162 |
+
|
| 163 |
st.session_state.last_response = accumulated_text
|
| 164 |
st.session_state.show_predefined = False
|
| 165 |
st.session_state.translation_done = False
|
| 166 |
|
| 167 |
+
if "don't have information" not in result.lower():
|
| 168 |
+
with st.expander("Source Documents"):
|
| 169 |
+
st.markdown(format_source_docs(source_documents))
|
| 170 |
+
|
| 171 |
except Exception as e:
|
| 172 |
st.error(f"Error: {str(e)}")
|
| 173 |
+
# Remove temporary assistant message on error
|
| 174 |
+
if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
|
| 175 |
+
st.session_state.messages.pop()
|
| 176 |
|
| 177 |
def handle_translation():
|
| 178 |
if "last_response" in st.session_state and st.session_state.last_response:
|