Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
UI.py
CHANGED
|
@@ -1,102 +1,134 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from engine import process_question
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
st.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from engine import process_question
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
# =========================
|
| 6 |
+
# PAGE SETUP
|
| 7 |
+
# =========================
|
| 8 |
+
st.set_page_config(page_title="Hospital AI Assistant", layout="wide")
|
| 9 |
+
|
| 10 |
+
st.title("π₯ Hospital AI Assistant")
|
| 11 |
+
st.caption("Ask questions about patients, conditions, visits, medications, labs")
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
+
# SESSION STATE
|
| 15 |
+
# =========================
|
| 16 |
+
if "messages" not in st.session_state:
|
| 17 |
+
st.session_state.messages = []
|
| 18 |
+
|
| 19 |
+
if "transcript" not in st.session_state:
|
| 20 |
+
st.session_state.transcript = []
|
| 21 |
+
|
| 22 |
+
# =========================
|
| 23 |
+
# SHOW CHAT HISTORY
|
| 24 |
+
# =========================
|
| 25 |
+
for msg in st.session_state.messages:
|
| 26 |
+
with st.chat_message(msg["role"]):
|
| 27 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|
| 28 |
+
|
| 29 |
+
# =========================
|
| 30 |
+
# CHAT INPUT
|
| 31 |
+
# =========================
|
| 32 |
+
user_input = st.chat_input("Ask a question about hospital data...")
|
| 33 |
+
|
| 34 |
+
if user_input:
|
| 35 |
+
# Show user message
|
| 36 |
+
st.session_state.messages.append(
|
| 37 |
+
{"role": "user", "content": user_input}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
with st.chat_message("user"):
|
| 41 |
+
st.markdown(user_input)
|
| 42 |
+
|
| 43 |
+
# Call backend
|
| 44 |
+
with st.spinner("Thinking..."):
|
| 45 |
+
try:
|
| 46 |
+
result = process_question(user_input)
|
| 47 |
+
|
| 48 |
+
# β
Store transcript safely
|
| 49 |
+
st.session_state.transcript.append({
|
| 50 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 51 |
+
"question": user_input,
|
| 52 |
+
"sql": result.get("sql"),
|
| 53 |
+
"result_preview": result.get("data", [])[:10],
|
| 54 |
+
"error": result.get("message") if result.get("status") != "ok" else None
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
result = {
|
| 59 |
+
"status": "error",
|
| 60 |
+
"message": str(e)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
# =========================
|
| 64 |
+
# BUILD RESPONSE
|
| 65 |
+
# =========================
|
| 66 |
+
reply = ""
|
| 67 |
+
|
| 68 |
+
if result.get("status") == "ok":
|
| 69 |
+
|
| 70 |
+
if result.get("message"):
|
| 71 |
+
reply += f"β {result['message']}\n\n"
|
| 72 |
+
|
| 73 |
+
if result.get("note"):
|
| 74 |
+
reply += f"π {result['note']}\n\n"
|
| 75 |
+
|
| 76 |
+
if result.get("data"):
|
| 77 |
+
columns = result.get("columns", [])
|
| 78 |
+
data = result["data"]
|
| 79 |
+
|
| 80 |
+
reply += "### Result\n"
|
| 81 |
+
reply += "| " + " | ".join(columns) + " |\n"
|
| 82 |
+
reply += "| " + " | ".join(["---"] * len(columns)) + " |\n"
|
| 83 |
+
|
| 84 |
+
for row in data[:10]:
|
| 85 |
+
reply += "| " + " | ".join(str(x) for x in row) + " |\n"
|
| 86 |
+
|
| 87 |
+
if result.get("sql"):
|
| 88 |
+
reply += "\n---\n"
|
| 89 |
+
reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
|
| 90 |
+
reply += f"```sql\n{result['sql']}\n```"
|
| 91 |
+
reply += "\n</details>"
|
| 92 |
+
|
| 93 |
+
else:
|
| 94 |
+
reply = f"β {result.get('message', 'Something went wrong')}"
|
| 95 |
+
|
| 96 |
+
# Show assistant message
|
| 97 |
+
st.session_state.messages.append(
|
| 98 |
+
{"role": "assistant", "content": reply}
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with st.chat_message("assistant"):
|
| 102 |
+
st.markdown(reply, unsafe_allow_html=True)
|
| 103 |
+
|
| 104 |
+
# =========================
|
| 105 |
+
# DOWNLOAD TRANSCRIPT (BOTTOM ONLY)
|
| 106 |
+
# =========================
|
| 107 |
+
def download_transcript_txt():
|
| 108 |
+
lines = []
|
| 109 |
+
for i, entry in enumerate(st.session_state.transcript, 1):
|
| 110 |
+
lines.append(f"\n--- Query {i} ---")
|
| 111 |
+
lines.append(f"Time: {entry['timestamp']}")
|
| 112 |
+
lines.append(f"Question: {entry['question']}")
|
| 113 |
+
|
| 114 |
+
if entry.get("sql"):
|
| 115 |
+
lines.append(f"SQL: {entry['sql']}")
|
| 116 |
+
|
| 117 |
+
if entry.get("result_preview"):
|
| 118 |
+
lines.append(f"Result Preview: {entry['result_preview']}")
|
| 119 |
+
|
| 120 |
+
if entry.get("error"):
|
| 121 |
+
lines.append(f"Error: {entry['error']}")
|
| 122 |
+
|
| 123 |
+
return "\n".join(lines)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
if st.session_state.transcript:
|
| 127 |
+
st.divider()
|
| 128 |
+
st.download_button(
|
| 129 |
+
label="β¬οΈ Download conversation",
|
| 130 |
+
data=download_transcript_txt(),
|
| 131 |
+
file_name="chat_transcript.txt",
|
| 132 |
+
mime="text/plain",
|
| 133 |
+
use_container_width=True
|
| 134 |
+
)
|