Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import reset_patient, set_patient, ask_another_question
|
| 3 |
+
from regular_rag import qa_tool_regular_rag
|
| 4 |
+
from graph_rag import qa_tool_graph_rag
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
def qa_tool(user_question, method):
|
| 11 |
+
logger.info(f"Method selected: {method}")
|
| 12 |
+
if method == "Regular RAG":
|
| 13 |
+
answer, images, ask_another_visible, change_patient_visible = qa_tool_regular_rag(user_question)
|
| 14 |
+
logger.info("Regular RAG answer generated")
|
| 15 |
+
logger.info(f"Regular RAG images: {images}") # Additional log for debugging
|
| 16 |
+
return answer, images, ask_another_visible, change_patient_visible, gr.update(visible=False)
|
| 17 |
+
elif method == "Graph-RAG":
|
| 18 |
+
answer, images, ask_another_visible, change_patient_visible, graph_fig = qa_tool_graph_rag(user_question)
|
| 19 |
+
logger.info("Graph-RAG answer generated")
|
| 20 |
+
logger.info(f"Graph-RAG images: {images}") # Additional log for debugging
|
| 21 |
+
return answer, images, ask_another_visible, change_patient_visible, gr.update(visible=True, value=graph_fig)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as app:
|
| 25 |
+
gr.Markdown("# Clinical Diagram QA Tool")
|
| 26 |
+
|
| 27 |
+
with gr.Group() as patient_input:
|
| 28 |
+
patient_desc = gr.Textbox(label="Patient Description")
|
| 29 |
+
set_patient_btn = gr.Button("Set Patient")
|
| 30 |
+
|
| 31 |
+
with gr.Group() as qa_interface:
|
| 32 |
+
qa_desc = gr.Markdown()
|
| 33 |
+
question_input = gr.Textbox(label="Enter your question")
|
| 34 |
+
method_choice = gr.Radio(["Regular RAG", "Graph-RAG"], label="Select Method")
|
| 35 |
+
get_answer_btn = gr.Button("Get Answer")
|
| 36 |
+
answer_output = gr.Textbox(label="Answer")
|
| 37 |
+
image_output = gr.Gallery(label="Relevant Images", show_label=True)
|
| 38 |
+
graph_output = gr.Plot(label="Knowledge Graph Visualization")
|
| 39 |
+
ask_another_question_btn = gr.Button("Ask Another Question")
|
| 40 |
+
change_patient_btn = gr.Button("Set Another Patient")
|
| 41 |
+
|
| 42 |
+
qa_interface.visible = False
|
| 43 |
+
ask_another_question_btn.visible = False
|
| 44 |
+
change_patient_btn.visible = False
|
| 45 |
+
|
| 46 |
+
set_patient_btn.click(
|
| 47 |
+
set_patient,
|
| 48 |
+
inputs=[patient_desc],
|
| 49 |
+
outputs=[
|
| 50 |
+
qa_desc,
|
| 51 |
+
qa_interface,
|
| 52 |
+
patient_input,
|
| 53 |
+
question_input,
|
| 54 |
+
answer_output,
|
| 55 |
+
image_output,
|
| 56 |
+
graph_output,
|
| 57 |
+
ask_another_question_btn,
|
| 58 |
+
change_patient_btn
|
| 59 |
+
]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
get_answer_btn.click(
|
| 63 |
+
qa_tool,
|
| 64 |
+
inputs=[question_input, method_choice],
|
| 65 |
+
outputs=[answer_output, image_output, ask_another_question_btn, change_patient_btn, graph_output]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
ask_another_question_btn.click(
|
| 69 |
+
ask_another_question,
|
| 70 |
+
outputs=[question_input, answer_output, image_output, graph_output, get_answer_btn, ask_another_question_btn]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
change_patient_btn.click(
|
| 74 |
+
reset_patient,
|
| 75 |
+
outputs=[
|
| 76 |
+
patient_desc,
|
| 77 |
+
qa_desc,
|
| 78 |
+
patient_input,
|
| 79 |
+
qa_interface,
|
| 80 |
+
change_patient_btn,
|
| 81 |
+
question_input,
|
| 82 |
+
answer_output,
|
| 83 |
+
image_output,
|
| 84 |
+
graph_output
|
| 85 |
+
]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
logger.info("Starting the application")
|
| 90 |
+
app.launch()
|