Spaces:
Runtime error
Runtime error
Fix: Correct indentation - all components inside Blocks context
Browse files
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 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 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 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
|
| 744 |
-
with gr.
|
| 745 |
-
|
| 746 |
-
label="
|
| 747 |
-
|
| 748 |
interactive=False,
|
| 749 |
-
|
| 750 |
)
|
| 751 |
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 758 |
|
| 759 |
-
|
| 760 |
-
label="
|
| 761 |
-
|
| 762 |
interactive=False,
|
| 763 |
-
|
| 764 |
)
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 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
|