Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
from openai import AzureOpenAI
|
5 |
+
from streamlit_pills import pills
|
6 |
+
|
7 |
+
st.set_page_config(layout="wide")
|
8 |
+
|
9 |
+
# Initialize the Azure OpenAI client
|
10 |
+
client = AzureOpenAI(
|
11 |
+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
|
12 |
+
api_key= os.getenv("AZURE_OPENAI_API_KEY"),
|
13 |
+
api_version="2024-05-01-preview"
|
14 |
+
)
|
15 |
+
|
16 |
+
# Retrieve the assistant
|
17 |
+
assistant = client.beta.assistants.retrieve("asst_Tbjd3ckxAfOjj29TeP6KaZ0v")
|
18 |
+
|
19 |
+
# Streamlit app
|
20 |
+
st.title("MultiCare AI Summary Demo")
|
21 |
+
|
22 |
+
# Initialize session state
|
23 |
+
if "thread_id" not in st.session_state:
|
24 |
+
thread = client.beta.threads.create()
|
25 |
+
st.session_state.thread_id = thread.id
|
26 |
+
|
27 |
+
if "messages" not in st.session_state:
|
28 |
+
st.session_state.messages = []
|
29 |
+
|
30 |
+
if "selected_pill" not in st.session_state:
|
31 |
+
st.session_state.selected_pill = None
|
32 |
+
|
33 |
+
|
34 |
+
def clean_content(content):
|
35 |
+
# Remove 【18:18†source】and 【5†source】style citations
|
36 |
+
content = re.sub(r'【\d+(?::\d+)?†source】', '', content)
|
37 |
+
# Prettify [1][2] style citations to [1, 2]
|
38 |
+
content = re.sub(r'\[(\d+)\](?:\[(\d+)\])+', lambda m: f"[{', '.join(sorted(set(m.groups())))}]", content)
|
39 |
+
return content.strip()
|
40 |
+
|
41 |
+
def process_message_content(message):
|
42 |
+
message_content = message.content[0].text
|
43 |
+
annotations = message_content.annotations
|
44 |
+
citations = []
|
45 |
+
citation_map = {}
|
46 |
+
|
47 |
+
for index, annotation in enumerate(annotations):
|
48 |
+
if file_citation := getattr(annotation, "file_citation", None):
|
49 |
+
cited_file = client.files.retrieve(file_citation.file_id)
|
50 |
+
citation = f"{cited_file.filename}"
|
51 |
+
if citation not in citation_map:
|
52 |
+
citation_map[citation] = len(citation_map) + 1
|
53 |
+
citations.append(f"[{citation_map[citation]}] {citation}")
|
54 |
+
message_content.value = message_content.value.replace(annotation.text, f"[{citation_map[citation]}]")
|
55 |
+
elif file_path := getattr(annotation, "file_path", None):
|
56 |
+
cited_file = client.files.retrieve(file_path.file_id)
|
57 |
+
citation = f"{cited_file.filename}"
|
58 |
+
if citation not in citation_map:
|
59 |
+
citation_map[citation] = len(citation_map) + 1
|
60 |
+
citations.append(f"[{citation_map[citation]}] {citation}")
|
61 |
+
message_content.value = message_content.value.replace(annotation.text, f"[{citation_map[citation]}]")
|
62 |
+
|
63 |
+
cleaned_content = clean_content(message_content.value)
|
64 |
+
return cleaned_content, list(set(citations))
|
65 |
+
|
66 |
+
# Function to handle message submission
|
67 |
+
def submit_message(message):
|
68 |
+
st.session_state.messages.append({"role": "user", "content": message})
|
69 |
+
with st.chat_message("user", avatar="⚕️"):
|
70 |
+
st.markdown(message)
|
71 |
+
|
72 |
+
client.beta.threads.messages.create(
|
73 |
+
thread_id=st.session_state.thread_id,
|
74 |
+
role="user",
|
75 |
+
content=message
|
76 |
+
)
|
77 |
+
|
78 |
+
with st.spinner("Assistant is thinking..."):
|
79 |
+
run = client.beta.threads.runs.create_and_poll(
|
80 |
+
thread_id=st.session_state.thread_id,
|
81 |
+
assistant_id=assistant.id
|
82 |
+
)
|
83 |
+
|
84 |
+
if run.status == 'completed':
|
85 |
+
messages = client.beta.threads.messages.list(
|
86 |
+
thread_id=st.session_state.thread_id
|
87 |
+
)
|
88 |
+
assistant_message = messages.data[0]
|
89 |
+
|
90 |
+
processed_content, citations = process_message_content(assistant_message)
|
91 |
+
|
92 |
+
st.session_state.messages.append({
|
93 |
+
"role": "assistant",
|
94 |
+
"content": processed_content,
|
95 |
+
"citations": citations
|
96 |
+
})
|
97 |
+
|
98 |
+
with st.chat_message("assistant", avatar="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTETAUBM4r3hXmQ3eC31vWws8A9VbPvOxQNZQ&s"):
|
99 |
+
st.markdown(processed_content)
|
100 |
+
if citations:
|
101 |
+
st.markdown("---")
|
102 |
+
st.markdown("**Citations:**")
|
103 |
+
for citation in citations:
|
104 |
+
st.markdown(citation)
|
105 |
+
else:
|
106 |
+
st.error(f"Error: {run.status}")
|
107 |
+
|
108 |
+
# Display chat messages
|
109 |
+
for message in st.session_state.messages:
|
110 |
+
with st.chat_message(message["role"]):
|
111 |
+
st.markdown(message["content"])
|
112 |
+
if "citations" in message and message["citations"]:
|
113 |
+
st.markdown("---")
|
114 |
+
st.markdown("**Citations:**")
|
115 |
+
for citation in message["citations"]:
|
116 |
+
st.markdown(citation)
|
117 |
+
|
118 |
+
suggested_messages = [
|
119 |
+
"I'm a physician working at a primary care clinic, what Epic update changes will affect me?",
|
120 |
+
"Summarize Epic update changes that will impact a Med Surg Registered Nurse.",
|
121 |
+
"What Epic update changes need to be reviewed by ED registration staff?",
|
122 |
+
"Which Epic update changes impact surgeons?",
|
123 |
+
"Are there any Epic update changes that will affect X-ray technicians?",
|
124 |
+
"Create a summary of all Epic update changes that are relevant for referral coordinators?"
|
125 |
+
]
|
126 |
+
|
127 |
+
selected_pill = pills(
|
128 |
+
"Quick Questions by Role",
|
129 |
+
suggested_messages,
|
130 |
+
icons=['👨⚕️', '👩⚕️', '🏥', '🥽', '📷', '📞'],
|
131 |
+
index=None,
|
132 |
+
label_visibility="visible",
|
133 |
+
key="suggested_messages"
|
134 |
+
)
|
135 |
+
|
136 |
+
if selected_pill:
|
137 |
+
submit_message(selected_pill)
|
138 |
+
|
139 |
+
# Chat input
|
140 |
+
user_input = st.chat_input("What would you like to ask?")
|
141 |
+
|
142 |
+
if user_input:
|
143 |
+
submit_message(user_input)
|