File size: 2,645 Bytes
8cc757b
c2e898c
32a276c
e4cf30a
8cc757b
c2e898c
e4cf30a
03c65d9
5af06a1
e4cf30a
 
b97ca50
e4cf30a
 
5c947fb
f3c6f29
4c3c133
e4cf30a
d4fb40d
 
e4cf30a
c2e898c
e4cf30a
c2e898c
 
 
 
 
 
 
7ba3536
c2e898c
d4fb40d
 
e4cf30a
d4fb40d
32a276c
03c65d9
e4cf30a
78f41eb
0b6ce8b
d064c8c
 
d4fb40d
67b3ac5
 
 
03c65d9
 
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
import gradio as gr
import pysbd
from transformers import pipeline
from sentence_transformers import CrossEncoder

sentence_segmenter = pysbd.Segmenter(language='en',clean=False)
passage_retreival_model = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
qa_model = pipeline("question-answering",'a-ware/bart-squadv2')
 
def fetch_answers(question, clincal_note ):
    clincal_note_paragraphs = clincal_note.splitlines()
    query_paragraph_list = [(question, para) for para in clincal_note_paragraphs if len(para.strip()) > 0 ]
    
    scores = passage_retreival_model.predict(query_paragraph_list)
    top_5_indices = scores.argsort()[-5:]
    top_5_query_paragraph_list = [query_paragraph_list[i] for i in top_5_indices ]
    top_5_query_paragraph_list.reverse()
    
    top_5_query_paragraph_answer_list = ""
    count = 1
    for query, passage in top_5_query_paragraph_list:
     passage_sentences = sentence_segmenter.segment(passage)
     answer = qa_model(question = query, context = passage)['answer']
     
     for i in range(len(passage_sentences)):
         if answer.startswith('.') or answer.startswith(':'):
             answer = answer[1:].strip()
         if answer in passage_sentences[i]:
             passage_sentences[i] = "**"+passage_sentences[i].strip()+"**"
     
     result_str = "# RESULT NO: "+str(count)+"\n"  
     result_str = result_str + " ".join(passage_sentences) + "\n\n"
     top_5_query_paragraph_answer_list += result_str
     count+=1
     
    return top_5_query_paragraph_answer_list

demo = gr.Interface(
    fn=fetch_answers,
    #take input as real time audio and use OPENAPI whisper for S2T
    #clinical note upload as file (.This is an example of simple text. or doc/docx file)
    inputs=[gr.Textbox(lines=2, label='Question', show_label=True, placeholder="What is age of patient ?"),
            gr.Textbox(lines=10, label='Clinical Note', show_label=True, placeholder="The patient is a 71 year old male...")],
    outputs="markdown",
    examples='.',
    title='Question Answering System from Clinical Notes for Physicians',
    description="""Physicians frequently seek answers to questions from a patient’s EHR to support clinical decision-making.​ It is not too hard to imagine a future where a physician interacts with an EHR system and asks it complex questions and expects precise answers with adequate context from a patient’s past clinical notes. ​Central to such a world is a medical question answering system that processes natural language questions asked by physicians and finds answers to the questions from all sources in a patient’s record."""
)
demo.launch()