Spaces:
Sleeping
Sleeping
Dima
commited on
Commit
•
a4f80cf
1
Parent(s):
389aa3a
first
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from audiorecorder import audiorecorder
|
3 |
+
import openai
|
4 |
+
import os
|
5 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
6 |
+
|
7 |
+
|
8 |
+
def get_completion(messages, model="gpt-3.5-turbo"):
|
9 |
+
response = openai.ChatCompletion.create(
|
10 |
+
model=model,
|
11 |
+
messages=messages,
|
12 |
+
temperature=0)
|
13 |
+
return response.choices[0].message["content"]
|
14 |
+
|
15 |
+
|
16 |
+
def transcribe(audio_path):
|
17 |
+
audio_file = open(audio_path, "rb")
|
18 |
+
transcript = openai.Audio.translate("whisper-1", audio_file)
|
19 |
+
return transcript["text"]
|
20 |
+
|
21 |
+
|
22 |
+
def get_ddx(vignette):
|
23 |
+
messages_ddx = [
|
24 |
+
{'role': 'system', 'content': 'You are a Physician AI assistant tool. Write a differential diagnosis for a patient. Write just diagnoses and justification. Do no write any additional information. Do not write any introduction.'},
|
25 |
+
{'role': 'user', 'content': vignette}]
|
26 |
+
ddx = get_completion(messages_ddx)
|
27 |
+
return ddx
|
28 |
+
|
29 |
+
|
30 |
+
def get_orders(vignette, ddx):
|
31 |
+
messages_orders = [
|
32 |
+
{'role': 'system', 'content': 'You are a Physician AI assistant tool. Write an order set for a patient to differentiate between conditions. Write just orders and justification. Do no write any additional information. Do not write any introduction.'},
|
33 |
+
{'role': 'user', 'content': f'Information about patient: {vignette}. Differential diagnosis: {ddx}'}]
|
34 |
+
orders = get_completion(messages_orders)
|
35 |
+
return orders
|
36 |
+
|
37 |
+
|
38 |
+
if 'vignette' not in st.session_state:
|
39 |
+
st.session_state['vignette'] = ''
|
40 |
+
|
41 |
+
if 'ddx' not in st.session_state:
|
42 |
+
st.session_state['ddx'] = ''
|
43 |
+
|
44 |
+
if 'orders' not in st.session_state:
|
45 |
+
st.session_state['orders'] = ''
|
46 |
+
|
47 |
+
if 'length' not in st.session_state:
|
48 |
+
st.session_state['length'] = 0
|
49 |
+
|
50 |
+
|
51 |
+
st.title("AI loop for healthcare providers")
|
52 |
+
st.markdown(
|
53 |
+
"Record your patient presentation and get the differential diagnoses and orders.")
|
54 |
+
st.divider()
|
55 |
+
|
56 |
+
audio = audiorecorder("Record", "Stop")
|
57 |
+
|
58 |
+
|
59 |
+
if (len(audio) != st.session_state['length']):
|
60 |
+
st.session_state['length'] = len(audio)
|
61 |
+
wav_file = open("audio.mp3", "wb")
|
62 |
+
wav_file.write(audio.tobytes())
|
63 |
+
st.session_state['vignette'] += transcribe("audio.mp3")
|
64 |
+
|
65 |
+
|
66 |
+
st.session_state['vignette'] = st.text_area(
|
67 |
+
"Vignette", value=st.session_state['vignette'])
|
68 |
+
|
69 |
+
|
70 |
+
if st.button("Get DDX and Orders"):
|
71 |
+
vignette = st.session_state['vignette']
|
72 |
+
ddx = get_ddx(vignette)
|
73 |
+
st.session_state['ddx'] = ddx
|
74 |
+
st.session_state['orders'] = get_orders(vignette, ddx)
|
75 |
+
|
76 |
+
|
77 |
+
col1, col2 = st.columns(2)
|
78 |
+
|
79 |
+
with col1:
|
80 |
+
st.markdown(
|
81 |
+
f"**DDX**\n\n{st.session_state['ddx']}", unsafe_allow_html=True)
|
82 |
+
|
83 |
+
with col2:
|
84 |
+
st.markdown(
|
85 |
+
f"**ORDERS**\n\n{st.session_state['orders']}", unsafe_allow_html=True)
|