arahrooh commited on
Commit
d1f0495
·
1 Parent(s): 65eb481

Fix: Correct indentation - all components inside Blocks context

Browse files
Files changed (1) hide show
  1. app.py +154 -161
app.py CHANGED
@@ -667,6 +667,7 @@ def create_interface(initial_bot: RAGBot, use_inference_api: bool = False) -> gr
667
  initial_model_short = list(MODEL_MAP.keys())[0]
668
 
669
  # Create the Gradio interface with error handling
 
670
  try:
671
  with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
672
  gr.Markdown("""
@@ -676,176 +677,168 @@ def create_interface(initial_bot: RAGBot, use_inference_api: bool = False) -> gr
676
 
677
  The chatbot uses a Retrieval-Augmented Generation (RAG) system to provide evidence-based answers from medical literature.
678
  """)
679
- except Exception as blocks_error:
680
- logger.error(f"Error creating Gradio Blocks: {blocks_error}", exc_info=True)
681
- # Create a minimal fallback
682
- with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
683
- gr.Markdown(f"# Error\n\nFailed to create interface: {blocks_error}")
684
- return demo
685
-
686
- try:
687
-
688
- with gr.Row():
689
- with gr.Column(scale=2):
690
- question_input = gr.Textbox(
691
- label="Your Question",
692
- placeholder="e.g., What is Lynch Syndrome? What screening is recommended for BRCA1 carriers?",
693
- lines=3
694
- )
695
-
696
- with gr.Row():
697
- model_dropdown = gr.Dropdown(
698
- choices=list(MODEL_MAP.keys()),
699
- value=initial_model_short,
700
- label="Select Model",
701
- info="Choose which LLM model to use for generating answers"
702
  )
703
 
704
- education_dropdown = gr.Dropdown(
705
- choices=list(EDUCATION_LEVELS.keys()),
706
- value=list(EDUCATION_LEVELS.keys())[0],
707
- label="Education Level",
708
- info="Select your education level for personalized answers"
709
- )
710
-
711
- with gr.Accordion("Advanced Settings", open=False):
712
- k_slider = gr.Slider(
713
- minimum=1,
714
- maximum=10,
715
- value=5,
716
- step=1,
717
- label="Number of document chunks to retrieve (k)"
718
- )
719
- temperature_slider = gr.Slider(
720
- minimum=0.1,
721
- maximum=1.0,
722
- value=0.2,
723
- step=0.1,
724
- label="Temperature (lower = more focused)"
725
- )
726
- max_tokens_slider = gr.Slider(
727
- minimum=128,
728
- maximum=1024,
729
- value=512,
730
- step=128,
731
- label="Max Tokens (lower = faster responses)"
732
- )
733
-
734
- submit_btn = gr.Button("Ask Question", variant="primary", size="lg")
735
-
736
- with gr.Column(scale=3):
737
- answer_output = gr.Textbox(
738
- label="Answer",
739
- lines=20,
740
- interactive=False,
741
- elem_classes=["answer-box"]
742
- )
743
 
744
- with gr.Row():
745
- flesch_output = gr.Textbox(
746
- label="Flesch-Kincaid Grade Level",
747
- value="N/A",
748
  interactive=False,
749
- scale=1
750
  )
751
 
752
- similarity_output = gr.Textbox(
753
- label="Similarity Scores",
754
- value="",
755
- interactive=False,
756
- scale=1
757
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
758
 
759
- category_output = gr.Textbox(
760
- label="Question Category",
761
- value="",
762
  interactive=False,
763
- scale=1
764
  )
765
-
766
- sources_output = gr.Textbox(
767
- label="Source Documents (with Chunk Text)",
768
- lines=15,
769
- interactive=False,
770
- info="Shows the retrieved document chunks with full text. File paths are shown for easy access."
771
- )
772
-
773
- # Example questions - all questions from the results CSV (scrollable)
774
- gr.Markdown("### 💡 Example Questions")
775
- gr.Markdown(f"Select a question below to use it in the chatbot ({len(EXAMPLE_QUESTIONS)} questions - scrollable dropdown):")
776
-
777
- # Use Dropdown which is naturally scrollable with many options
778
- example_questions_dropdown = gr.Dropdown(
779
- choices=EXAMPLE_QUESTIONS,
780
- label="Example Questions",
781
- value=None,
782
- info="Open the dropdown and scroll through all questions. Select one to use it.",
783
- interactive=True,
784
- container=True,
785
- scale=1
786
- )
787
-
788
- # Update question input when dropdown selection changes
789
- def update_question_from_dropdown(selected_question):
790
- return selected_question if selected_question else ""
791
-
792
- example_questions_dropdown.change(
793
- fn=update_question_from_dropdown,
794
- inputs=example_questions_dropdown,
795
- outputs=question_input
796
- )
797
-
798
- # Footer
799
- gr.Markdown("""
800
- ---
801
- **Note:** This chatbot provides informational answers based on medical literature.
802
- It is not a substitute for professional medical advice, diagnosis, or treatment.
803
- Always consult with qualified healthcare providers for medical decisions.
804
- """)
805
-
806
- # Connect the submit button
807
- def process_with_education_level(question, model, education, k, temp, max_tok):
808
- education_key = EDUCATION_LEVELS[education]
809
- return interface.process_question(question, model, education_key, k, temp, max_tok)
810
-
811
- submit_btn.click(
812
- fn=process_with_education_level,
813
- inputs=[
814
- question_input,
815
- model_dropdown,
816
- education_dropdown,
817
- k_slider,
818
- temperature_slider,
819
- max_tokens_slider
820
- ],
821
- outputs=[
822
- answer_output,
823
- flesch_output,
824
- sources_output,
825
- similarity_output,
826
- category_output
827
- ]
828
- )
829
-
830
- # Also allow Enter key to submit
831
- question_input.submit(
832
- fn=process_with_education_level,
833
- inputs=[
834
- question_input,
835
- model_dropdown,
836
- education_dropdown,
837
- k_slider,
838
- temperature_slider,
839
- max_tokens_slider
840
- ],
841
- outputs=[
842
- answer_output,
843
- flesch_output,
844
- sources_output,
845
- similarity_output,
846
- category_output
847
- ]
848
- )
849
  except Exception as interface_error:
850
  logger.error(f"Error setting up Gradio interface components: {interface_error}", exc_info=True)
851
  import traceback
 
667
  initial_model_short = list(MODEL_MAP.keys())[0]
668
 
669
  # Create the Gradio interface with error handling
670
+ # CRITICAL: All components and event handlers must be INSIDE the with gr.Blocks() context
671
  try:
672
  with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
673
  gr.Markdown("""
 
677
 
678
  The chatbot uses a Retrieval-Augmented Generation (RAG) system to provide evidence-based answers from medical literature.
679
  """)
680
+
681
+ with gr.Row():
682
+ with gr.Column(scale=2):
683
+ question_input = gr.Textbox(
684
+ label="Your Question",
685
+ placeholder="e.g., What is Lynch Syndrome? What screening is recommended for BRCA1 carriers?",
686
+ lines=3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687
  )
688
 
689
+ with gr.Row():
690
+ model_dropdown = gr.Dropdown(
691
+ choices=list(MODEL_MAP.keys()),
692
+ value=initial_model_short,
693
+ label="Select Model",
694
+ info="Choose which LLM model to use for generating answers"
695
+ )
696
+
697
+ education_dropdown = gr.Dropdown(
698
+ choices=list(EDUCATION_LEVELS.keys()),
699
+ value=list(EDUCATION_LEVELS.keys())[0],
700
+ label="Education Level",
701
+ info="Select your education level for personalized answers"
702
+ )
703
+
704
+ with gr.Accordion("Advanced Settings", open=False):
705
+ k_slider = gr.Slider(
706
+ minimum=1,
707
+ maximum=10,
708
+ value=5,
709
+ step=1,
710
+ label="Number of document chunks to retrieve (k)"
711
+ )
712
+ temperature_slider = gr.Slider(
713
+ minimum=0.1,
714
+ maximum=1.0,
715
+ value=0.2,
716
+ step=0.1,
717
+ label="Temperature (lower = more focused)"
718
+ )
719
+ max_tokens_slider = gr.Slider(
720
+ minimum=128,
721
+ maximum=1024,
722
+ value=512,
723
+ step=128,
724
+ label="Max Tokens (lower = faster responses)"
725
+ )
726
+
727
+ submit_btn = gr.Button("Ask Question", variant="primary", size="lg")
728
 
729
+ with gr.Column(scale=3):
730
+ answer_output = gr.Textbox(
731
+ label="Answer",
732
+ lines=20,
733
  interactive=False,
734
+ elem_classes=["answer-box"]
735
  )
736
 
737
+ with gr.Row():
738
+ flesch_output = gr.Textbox(
739
+ label="Flesch-Kincaid Grade Level",
740
+ value="N/A",
741
+ interactive=False,
742
+ scale=1
743
+ )
744
+
745
+ similarity_output = gr.Textbox(
746
+ label="Similarity Scores",
747
+ value="",
748
+ interactive=False,
749
+ scale=1
750
+ )
751
+
752
+ category_output = gr.Textbox(
753
+ label="Question Category",
754
+ value="",
755
+ interactive=False,
756
+ scale=1
757
+ )
758
 
759
+ sources_output = gr.Textbox(
760
+ label="Source Documents (with Chunk Text)",
761
+ lines=15,
762
  interactive=False,
763
+ info="Shows the retrieved document chunks with full text. File paths are shown for easy access."
764
  )
765
+
766
+ # Example questions - all questions from the results CSV (scrollable)
767
+ gr.Markdown("### 💡 Example Questions")
768
+ gr.Markdown(f"Select a question below to use it in the chatbot ({len(EXAMPLE_QUESTIONS)} questions - scrollable dropdown):")
769
+
770
+ # Use Dropdown which is naturally scrollable with many options
771
+ example_questions_dropdown = gr.Dropdown(
772
+ choices=EXAMPLE_QUESTIONS,
773
+ label="Example Questions",
774
+ value=None,
775
+ info="Open the dropdown and scroll through all questions. Select one to use it.",
776
+ interactive=True,
777
+ container=True,
778
+ scale=1
779
+ )
780
+
781
+ # Update question input when dropdown selection changes
782
+ def update_question_from_dropdown(selected_question):
783
+ return selected_question if selected_question else ""
784
+
785
+ example_questions_dropdown.change(
786
+ fn=update_question_from_dropdown,
787
+ inputs=example_questions_dropdown,
788
+ outputs=question_input
789
+ )
790
+
791
+ # Footer
792
+ gr.Markdown("""
793
+ ---
794
+ **Note:** This chatbot provides informational answers based on medical literature.
795
+ It is not a substitute for professional medical advice, diagnosis, or treatment.
796
+ Always consult with qualified healthcare providers for medical decisions.
797
+ """)
798
+
799
+ # Connect the submit button
800
+ def process_with_education_level(question, model, education, k, temp, max_tok):
801
+ education_key = EDUCATION_LEVELS[education]
802
+ return interface.process_question(question, model, education_key, k, temp, max_tok)
803
+
804
+ submit_btn.click(
805
+ fn=process_with_education_level,
806
+ inputs=[
807
+ question_input,
808
+ model_dropdown,
809
+ education_dropdown,
810
+ k_slider,
811
+ temperature_slider,
812
+ max_tokens_slider
813
+ ],
814
+ outputs=[
815
+ answer_output,
816
+ flesch_output,
817
+ sources_output,
818
+ similarity_output,
819
+ category_output
820
+ ]
821
+ )
822
+
823
+ # Also allow Enter key to submit
824
+ question_input.submit(
825
+ fn=process_with_education_level,
826
+ inputs=[
827
+ question_input,
828
+ model_dropdown,
829
+ education_dropdown,
830
+ k_slider,
831
+ temperature_slider,
832
+ max_tokens_slider
833
+ ],
834
+ outputs=[
835
+ answer_output,
836
+ flesch_output,
837
+ sources_output,
838
+ similarity_output,
839
+ category_output
840
+ ]
841
+ )
 
 
 
 
 
 
 
842
  except Exception as interface_error:
843
  logger.error(f"Error setting up Gradio interface components: {interface_error}", exc_info=True)
844
  import traceback