File size: 3,025 Bytes
c33bcd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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)] |