| import gradio as gr |
| from similarity import compute_similarity |
| from custom_css import CUSTOM_CSS_FOR_INTERFACE |
| from default_values import DEFAULT_DOCUMENTS, DEFAULT_QUESTION |
|
|
|
|
| def build_interface(): |
| """Build and return the Gradio interface""" |
| with gr.Blocks(css=CUSTOM_CSS_FOR_INTERFACE, theme=gr.themes.Soft()) as demo: |
| with gr.Column(elem_id="title"): |
| gr.HTML("<h1>🔍 Smart Medical Search</h1>") |
| gr.HTML("<h2> Ask questions, get instant answers from your documents</h2>") |
|
|
| with gr.Row(): |
| with gr.Column(scale=1): |
| with gr.Group(elem_classes="input-section"): |
| reference_input = gr.Textbox( |
| label="📌 Enter your question about your medical documents:", |
| placeholder="Enter your question here...", |
| value=DEFAULT_QUESTION, |
| lines=3, |
| max_lines=5 |
| ) |
|
|
| gr.HTML("""<h3> 🔍 Your medical documents:</h3>""") |
|
|
| comparison_container = gr.Column() |
|
|
| with comparison_container: |
| comparison_inputs = [] |
| for i in range(15): |
| value = DEFAULT_DOCUMENTS[i] if i < len(DEFAULT_DOCUMENTS) else "" |
| comparison_box = gr.Textbox( |
| label=f"Sentence {i + 1}", |
| placeholder="Enter a document to search...", |
| value=value, |
| lines=2, |
| elem_classes="comparison-box", |
| visible=True if i < 5 else False |
| ) |
| comparison_inputs.append(comparison_box) |
|
|
|
|
| add_btn = gr.Button("➕ Add another document", elem_classes="add-btn", size="md") |
|
|
| visible_count = gr.State(5) |
|
|
| add_btn.click( |
| fn=_add_comparison_box, |
| inputs=[visible_count], |
| outputs=[visible_count] + comparison_inputs |
| ) |
|
|
| submit_btn = gr.Button("Search for the answer in your documents", variant="primary", elem_id="calc-btn", size="lg") |
|
|
| with gr.Column(scale=1): |
| output = gr.HTML(label="Similarity Scores") |
|
|
| submit_btn.click( |
| fn=compute_similarity, |
| inputs=[reference_input] + comparison_inputs, |
| outputs=output |
| ) |
|
|
| return demo |
|
|
|
|
| def _add_comparison_box(count: int) -> list[int]: |
| """Check whether new boxes can be added.""" |
| if count < 15: |
| new_count = count + 1 |
| visibility = [gr.update(visible=True) if i < new_count else gr.update(visible=False) |
| for i in range(15)] |
| return [new_count] + visibility |
| else: |
| return [count] + [gr.update() for _ in range(15)] |