Spaces:
Sleeping
Sleeping
File size: 4,214 Bytes
81dac89 5ff4451 81dac89 5ff4451 81dac89 5ff4451 81dac89 5ff4451 81dac89 5ff4451 81dac89 | 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 | import streamlit as st
from engine import process_question
from datetime import datetime
# =========================
# PAGE SETUP
# =========================
st.set_page_config(page_title="Hospital AI Assistant", layout="wide")
st.title("π₯ Hospital AI Assistant")
st.caption("Ask questions about patients, conditions, visits, medications, labs")
# =========================
# SESSION STATE
# =========================
if "messages" not in st.session_state:
st.session_state.messages = []
if "transcript" not in st.session_state:
st.session_state.transcript = []
# =========================
# SHOW CHAT HISTORY
# =========================
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"], unsafe_allow_html=True)
# =========================
# CHAT INPUT
# =========================
user_input = st.chat_input("Ask a question about hospital data...")
if user_input:
# Show user message
st.session_state.messages.append(
{"role": "user", "content": user_input}
)
with st.chat_message("user"):
st.markdown(user_input)
# Call backend
with st.spinner("Thinking..."):
try:
result = process_question(user_input)
except Exception as e:
result = {
"status": "error",
"message": str(e)
}
# =========================
# BUILD RESPONSE
# =========================
reply = ""
if result.get("status") == "ok":
if result.get("message"):
reply += f"β {result['message']}\n\n"
if result.get("note"):
reply += f"π {result['note']}\n\n"
if result.get("data"):
columns = result.get("columns", [])
data = result["data"]
reply += "### Result\n"
reply += "| " + " | ".join(columns) + " |\n"
reply += "| " + " | ".join(["---"] * len(columns)) + " |\n"
for row in data[:10]:
reply += "| " + " | ".join(str(x) for x in row) + " |\n"
if result.get("sql"):
reply += "\n---\n"
reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
reply += f"```sql\n{result['sql']}\n```"
reply += "\n</details>"
else:
reply = f"β {result.get('message', 'Something went wrong')}"
# =========================
# SAVE TRANSCRIPT β
FIXED
# =========================
st.session_state.transcript.append({
"timestamp": datetime.utcnow().isoformat(),
"question": user_input,
"reply": reply, # β
THIS WAS MISSING
"sql": result.get("sql"),
"result_preview": result.get("data", [])[:10],
"error": result.get("message") if result.get("status") != "ok" else None
})
# =========================
# SHOW ASSISTANT MESSAGE
# =========================
st.session_state.messages.append(
{"role": "assistant", "content": reply}
)
with st.chat_message("assistant"):
st.markdown(reply, unsafe_allow_html=True)
# =========================
# DOWNLOAD TRANSCRIPT
# =========================
def download_transcript_txt():
lines = []
for i, entry in enumerate(st.session_state.transcript, 1):
lines.append(f"\n--- Query {i} ---")
lines.append(f"Time: {entry['timestamp']}")
lines.append(f"Question: {entry['question']}")
if entry.get("reply"):
lines.append("Reply:")
lines.append(entry["reply"])
if entry.get("sql"):
lines.append(f"SQL:\n{entry['sql']}")
if entry.get("error"):
lines.append(f"Error: {entry['error']}")
return "\n".join(lines)
# Show download button only if data exists
if st.session_state.transcript:
st.divider()
st.download_button(
label="β¬οΈ Download conversation",
data=download_transcript_txt(),
file_name="chat_transcript.txt",
mime="text/plain",
use_container_width=True
)
|