nishantgaurav23 commited on
Commit
6665ca1
Β·
verified Β·
1 Parent(s): 5f96986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -371,6 +371,11 @@ class RAGPipeline:
371
 
372
  def process_query(self, query: str, placeholder) -> str:
373
  try:
 
 
 
 
 
374
  query = self.preprocess_query(query)
375
  status = placeholder.empty()
376
  status.write("πŸ” Finding relevant information...")
@@ -385,7 +390,7 @@ class RAGPipeline:
385
  cleaned_text = self.postprocess_response(doc)
386
  if cleaned_text:
387
  cleaned_docs.append(cleaned_text)
388
-
389
  status.write("πŸ’­ Generating response...")
390
 
391
  prompt = f"""Context information is below:
@@ -393,7 +398,7 @@ class RAGPipeline:
393
 
394
  Given the context above, please answer the following question:
395
  {query}
396
-
397
  Guidelines for your response:
398
  - Structure your response in clear, logical paragraphs
399
  - Start a new paragraph for each new main point or aspect
@@ -407,7 +412,7 @@ class RAGPipeline:
407
  - Exclude any dates, timestamps, or user comments
408
  - Focus on factual sports information
409
  - If you cannot answer based on the context, say so politely
410
-
411
  Format your response with proper paragraph breaks where appropriate.
412
 
413
  Answer:"""
@@ -421,7 +426,7 @@ class RAGPipeline:
421
  final_response = self.postprocess_response(response_text)
422
 
423
  # Convert cleaned response to markdown with proper paragraph spacing
424
- markdown_response = final_response.replace('\n\n', '\n\n \n\n') # Add visual spacing between paragraphs
425
 
426
  response_placeholder.markdown(markdown_response)
427
  return final_response
@@ -440,7 +445,7 @@ class RAGPipeline:
440
  logging.error(f"Process error: {str(e)}")
441
  message = "Something went wrong. Please try again with a different question."
442
  placeholder.warning(message)
443
- return messag
444
 
445
  def query_model(self, prompt: str) -> str:
446
  """Query the local Llama model"""
@@ -790,6 +795,12 @@ def main():
790
  if not check_environment():
791
  return
792
 
 
 
 
 
 
 
793
  # Improved CSS styling
794
  st.markdown("""
795
  <style>
@@ -868,18 +879,12 @@ def main():
868
  Hey there! πŸ‘‹ I can help you with information on Ice Hockey, Baseball, American Football, Soccer, and Basketball.
869
  With access to the ESPN API, I'm up to date with the latest details for these sports up until October 2024.
870
  </p>
871
- """, unsafe_allow_html=True)
872
-
873
- st.markdown("""
874
  <p class='description'>
875
  Got any general questions? Feel free to askβ€”I'll do my best to provide answers based on the information I've been trained on!
876
  </p>
877
  """, unsafe_allow_html=True)
878
 
879
- # Add spacing
880
- st.markdown("<br>", unsafe_allow_html=True)
881
-
882
- # Initialize the pipeline with better error handling
883
  if 'rag' not in st.session_state:
884
  try:
885
  with st.spinner("Loading resources..."):
@@ -904,8 +909,18 @@ def main():
904
  # Centered button with unique key
905
  if st.button("Get Answer", key="answer_button"):
906
  if query:
 
 
 
 
907
  response_placeholder = st.empty()
 
 
908
  try:
 
 
 
 
909
  # Log query processing start
910
  logging.info(f"Processing query: {query}")
911
 
@@ -916,11 +931,15 @@ def main():
916
  # Log successful response
917
  logging.info(f"Generated response: {response}")
918
 
 
 
 
919
  except Exception as e:
920
  # Log error details
921
  logging.error(f"Query processing error: {str(e)}")
922
  logging.error("Full error details: ", exc_info=True)
923
  response_placeholder.warning("Unable to process your question. Please try again.")
 
924
  else:
925
  st.warning("Please enter a question!")
926
 
 
371
 
372
  def process_query(self, query: str, placeholder) -> str:
373
  try:
374
+ # Verify this is the current query being processed
375
+ if hasattr(st.session_state, 'current_query') and query != st.session_state.current_query:
376
+ logging.warning(f"Skipping outdated query: {query}")
377
+ return ""
378
+
379
  query = self.preprocess_query(query)
380
  status = placeholder.empty()
381
  status.write("πŸ” Finding relevant information...")
 
390
  cleaned_text = self.postprocess_response(doc)
391
  if cleaned_text:
392
  cleaned_docs.append(cleaned_text)
393
+
394
  status.write("πŸ’­ Generating response...")
395
 
396
  prompt = f"""Context information is below:
 
398
 
399
  Given the context above, please answer the following question:
400
  {query}
401
+
402
  Guidelines for your response:
403
  - Structure your response in clear, logical paragraphs
404
  - Start a new paragraph for each new main point or aspect
 
412
  - Exclude any dates, timestamps, or user comments
413
  - Focus on factual sports information
414
  - If you cannot answer based on the context, say so politely
415
+
416
  Format your response with proper paragraph breaks where appropriate.
417
 
418
  Answer:"""
 
426
  final_response = self.postprocess_response(response_text)
427
 
428
  # Convert cleaned response to markdown with proper paragraph spacing
429
+ markdown_response = final_response.replace('\n\n', '\n\n&nbsp;\n\n')
430
 
431
  response_placeholder.markdown(markdown_response)
432
  return final_response
 
445
  logging.error(f"Process error: {str(e)}")
446
  message = "Something went wrong. Please try again with a different question."
447
  placeholder.warning(message)
448
+ return message
449
 
450
  def query_model(self, prompt: str) -> str:
451
  """Query the local Llama model"""
 
795
  if not check_environment():
796
  return
797
 
798
+ # Initialize session state variables
799
+ if 'current_query' not in st.session_state:
800
+ st.session_state.current_query = None
801
+ if 'processing' not in st.session_state:
802
+ st.session_state.processing = False
803
+
804
  # Improved CSS styling
805
  st.markdown("""
806
  <style>
 
879
  Hey there! πŸ‘‹ I can help you with information on Ice Hockey, Baseball, American Football, Soccer, and Basketball.
880
  With access to the ESPN API, I'm up to date with the latest details for these sports up until October 2024.
881
  </p>
 
 
 
882
  <p class='description'>
883
  Got any general questions? Feel free to askβ€”I'll do my best to provide answers based on the information I've been trained on!
884
  </p>
885
  """, unsafe_allow_html=True)
886
 
887
+ # Initialize the pipeline
 
 
 
888
  if 'rag' not in st.session_state:
889
  try:
890
  with st.spinner("Loading resources..."):
 
909
  # Centered button with unique key
910
  if st.button("Get Answer", key="answer_button"):
911
  if query:
912
+ # Clear any previous response
913
+ if 'response_placeholder' in st.session_state:
914
+ st.session_state.response_placeholder.empty()
915
+
916
  response_placeholder = st.empty()
917
+ st.session_state.response_placeholder = response_placeholder
918
+
919
  try:
920
+ # Update current query and processing state
921
+ st.session_state.current_query = query
922
+ st.session_state.processing = True
923
+
924
  # Log query processing start
925
  logging.info(f"Processing query: {query}")
926
 
 
931
  # Log successful response
932
  logging.info(f"Generated response: {response}")
933
 
934
+ # Reset processing state
935
+ st.session_state.processing = False
936
+
937
  except Exception as e:
938
  # Log error details
939
  logging.error(f"Query processing error: {str(e)}")
940
  logging.error("Full error details: ", exc_info=True)
941
  response_placeholder.warning("Unable to process your question. Please try again.")
942
+ st.session_state.processing = False
943
  else:
944
  st.warning("Please enter a question!")
945