Spaces:
Sleeping
Sleeping
Upload UI.py
Browse files
UI.py
CHANGED
|
@@ -6,64 +6,85 @@ st.set_page_config(page_title="Hospital AI Assistant", layout="wide")
|
|
| 6 |
st.title("π₯ Hospital AI Assistant")
|
| 7 |
st.caption("Ask questions about patients, conditions, visits, medications, labs")
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
if "messages" not in st.session_state:
|
| 11 |
st.session_state.messages = []
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
| 14 |
for msg in st.session_state.messages:
|
| 15 |
with st.chat_message(msg["role"]):
|
| 16 |
-
st.markdown(msg["content"])
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
| 19 |
user_input = st.chat_input("Ask a question about hospital data...")
|
| 20 |
|
| 21 |
if user_input:
|
| 22 |
# Show user message
|
| 23 |
-
st.session_state.messages.append(
|
|
|
|
|
|
|
| 24 |
with st.chat_message("user"):
|
| 25 |
st.markdown(user_input)
|
| 26 |
|
| 27 |
-
# Call
|
| 28 |
with st.spinner("Thinking..."):
|
| 29 |
try:
|
| 30 |
result = process_question(user_input)
|
| 31 |
except Exception as e:
|
| 32 |
-
result = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Build assistant reply
|
| 35 |
if result.get("status") == "ok":
|
| 36 |
-
reply = ""
|
| 37 |
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
if result.get("note"):
|
| 40 |
-
reply += f"π
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if result.get("data"):
|
| 44 |
columns = result.get("columns", [])
|
| 45 |
data = result["data"]
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
for row in data[:10]:
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
reply +=
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
|
| 60 |
-
reply += f"```sql\n{result['sql']}\n```"
|
| 61 |
-
reply += "\n</details>"
|
| 62 |
|
| 63 |
else:
|
| 64 |
reply = f"β {result.get('message', 'Something went wrong')}"
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
with st.chat_message("assistant"):
|
| 69 |
-
st.markdown(reply)
|
|
|
|
| 6 |
st.title("π₯ Hospital AI Assistant")
|
| 7 |
st.caption("Ask questions about patients, conditions, visits, medications, labs")
|
| 8 |
|
| 9 |
+
# =========================
|
| 10 |
+
# Session State
|
| 11 |
+
# =========================
|
| 12 |
if "messages" not in st.session_state:
|
| 13 |
st.session_state.messages = []
|
| 14 |
|
| 15 |
+
# =========================
|
| 16 |
+
# Show Chat History
|
| 17 |
+
# =========================
|
| 18 |
for msg in st.session_state.messages:
|
| 19 |
with st.chat_message(msg["role"]):
|
| 20 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|
| 21 |
|
| 22 |
+
# =========================
|
| 23 |
+
# Chat Input
|
| 24 |
+
# =========================
|
| 25 |
user_input = st.chat_input("Ask a question about hospital data...")
|
| 26 |
|
| 27 |
if user_input:
|
| 28 |
# Show user message
|
| 29 |
+
st.session_state.messages.append(
|
| 30 |
+
{"role": "user", "content": user_input}
|
| 31 |
+
)
|
| 32 |
with st.chat_message("user"):
|
| 33 |
st.markdown(user_input)
|
| 34 |
|
| 35 |
+
# Call backend
|
| 36 |
with st.spinner("Thinking..."):
|
| 37 |
try:
|
| 38 |
result = process_question(user_input)
|
| 39 |
except Exception as e:
|
| 40 |
+
result = {
|
| 41 |
+
"status": "error",
|
| 42 |
+
"message": str(e)
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# =========================
|
| 46 |
+
# Build Assistant Reply
|
| 47 |
+
# =========================
|
| 48 |
+
reply = ""
|
| 49 |
|
|
|
|
| 50 |
if result.get("status") == "ok":
|
|
|
|
| 51 |
|
| 52 |
+
# Message (important for time-based or empty data)
|
| 53 |
+
if result.get("message"):
|
| 54 |
+
reply += f"β {result['message']}\n\n"
|
| 55 |
+
|
| 56 |
+
# Time note
|
| 57 |
if result.get("note"):
|
| 58 |
+
reply += f"π {result['note']}\n\n"
|
| 59 |
|
| 60 |
+
# Table output
|
| 61 |
if result.get("data"):
|
| 62 |
columns = result.get("columns", [])
|
| 63 |
data = result["data"]
|
| 64 |
|
| 65 |
+
reply += "### Result\n"
|
| 66 |
+
reply += "| " + " | ".join(columns) + " |\n"
|
| 67 |
+
reply += "| " + " | ".join(["---"] * len(columns)) + " |\n"
|
| 68 |
|
| 69 |
for row in data[:10]:
|
| 70 |
+
reply += "| " + " | ".join(str(x) for x in row) + " |\n"
|
| 71 |
|
| 72 |
+
# SQL (ONLY if present)
|
| 73 |
+
if result.get("sql"):
|
| 74 |
+
reply += "\n---\n"
|
| 75 |
+
reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
|
| 76 |
+
reply += f"```sql\n{result['sql']}\n```"
|
| 77 |
+
reply += "\n</details>"
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
else:
|
| 80 |
reply = f"β {result.get('message', 'Something went wrong')}"
|
| 81 |
|
| 82 |
+
# =========================
|
| 83 |
+
# Display Assistant Message
|
| 84 |
+
# =========================
|
| 85 |
+
st.session_state.messages.append(
|
| 86 |
+
{"role": "assistant", "content": reply}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
with st.chat_message("assistant"):
|
| 90 |
+
st.markdown(reply, unsafe_allow_html=True)
|