File size: 5,299 Bytes
8b09cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9129f2a
8b09cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0294e6
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Simple APP for specialty pharmacy 

# Import packages
import numpy as np 
import os 

import gradio as gr
from transformers import pipeline

#Import LLMs 
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI

# Prompt template 
from langchain import PromptTemplate

# Chains
from langchain.chains import LLMChain


# Import "secret" OPENAI_API_KEY
os.environ["OPENAI_API_KEY"] 

# Import GPT-4
llm_gpt = ChatOpenAI(model='gpt-4-0613',temperature=0.)

# ======================================================
# Set up an ASR pipeline using facebook's wav2vec2
p = pipeline("automatic-speech-recognition", chunk_length_s=40)

# =======================================================
# LLM Chains

# Dialogue chain
template_diag = """
You are an AI assistant with medical language understanding.

The input is a dialogue between a specialty pharmacist and patient: {input}

To give you context, the dialogue will have to do about symptoms, side effects, medications etc
of a rare disease, most probably multiple sclerosis.

You have a couple of tasks:

- First: If there are some non-sensical words, convert them to the most probable real word,
taking into account that this is a pharmaxist, so most of them should describe medical conditions
or symptoms, most probably about multiple sclerosis.
If a medication is mentioned, do your best to find which is that, if any. Correct any mispellings
Capitalize the names of the medications.

- Second: Convert the text into a dialogue of the form:

[Pat]: 
[PRx]:

Where [PRx]: Pharmacist, [Pat]: Patient

Use your judgement to distinguish between the two roles and who said what.
Output only this dialogue.

Output:
"""


prompt_diag = PromptTemplate(template=template_diag, input_variables=["input"])
chain_diag = LLMChain(llm=llm_gpt, prompt=prompt_diag, verbose=False)


# ==============================================
template_struct = """
You are an AI assistant with medical language understanding.

The input is a dialogue between a specialty pharmacist and patient: {input}

To give you context, the dialogue will have to do about symptoms, side effects, medications etc
of a rare disease, most probably multiple sclerosis.

Some words may not be clearly spelled, because they come from an automatic 
audio to text transcript. 

Your have a few tasks:

- First task: If there are some non-sensical words, convert them to the most probable real word,
taking into account that this is a dialogue about a medical condition, probably multiple sclerosis

- Second task: extract information from this dialogue

Specifically the following:

- A brief summary of the dialogue, highlighting the chief complaint 
- The main disease mentioned by the patient
- Medications mentioned by the patient
- Side effets mentioned by the patient

The output should have the form of a json file with those four keys: (Summary, Disease, Medications, Side_Effects)

Do not hallucinate and do not make up information that is not included in the original file.

Output:
"""

# SOAP notes 
prompt_struct = PromptTemplate(template=template_struct, input_variables=["input"])
chain_struct = LLMChain(llm=llm_gpt, prompt=prompt_struct, verbose=False)


# Transcription function
def transcribe(audio):
    #text = fake_audio
    text = p(audio)["text"]
    output_1 = eval(chain_struct.run(text))
    output_2 = chain_diag.run(text)
    summa = output_1['Summary']
    disease = output_1['Disease']
    meds = output_1['Medications']
    sides = output_1['Side_Effects']
    return summa, disease, meds, sides, output_2  
        
    
# 
with gr.Blocks(title="AI specialty scriber",theme=gr.themes.Soft()) as demo:
    
    with gr.Row():
        image_wag = gr.Image(value="Walgreens_AI.png", width=10, show_label=False,show_download_button=False, scale=1)
    gr.Markdown("## <center> Walgreens AI-powered specialty pharmacy tool </center>")
    #gr.Markdown("**<center>"+scriber_description+"</center>**")
    gr.Markdown("<center> ________________________________________________________________________ </center>")
    
    # ====================================================
    # Dictation tool
    gr.Markdown("**Record Patient Interaction**")
    audio = gr.Audio(label='Your recording here',source="microphone", type="filepath",container=True)
    audio_submit_btn = gr.Button(value="Submit Recording", variant="primary")
    
    # Clinical notess and transcript
    with gr.Tab("Extracted Information"):
        with gr.Row():
            summary = gr.Textbox(label='Summary',lines=3,interactive=True) 
            disease = gr.Textbox(label='Disease mentioned',lines=3,interactive=True)
        with gr.Row():
            medications = gr.Textbox(label='Medications mentioned',lines=3,interactive=True)
            sides = gr.Textbox(label='Side Effects mentioned',lines=3,interactive=True)
        
    with gr.Tab("Original Transcript"):
        dialogue = gr.Textbox(label='Full conversation transcript',lines=10)
        
    # ===============================================
    # Submit and clear tool    
    audio_submit_btn.click(transcribe, inputs = audio, outputs=[summary,disease,medications,sides,dialogue])
    audio_clear_btn = gr.ClearButton([audio,summary,disease,medications,sides,dialogue])
    
    
demo.launch()