bhavika24 commited on
Commit
34c29b0
Β·
verified Β·
1 Parent(s): 25a0c35

Upload UI.py

Browse files
Files changed (1) hide show
  1. UI.py +48 -27
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
- # Initialize chat history
 
 
10
  if "messages" not in st.session_state:
11
  st.session_state.messages = []
12
 
13
- # Display chat history
 
 
14
  for msg in st.session_state.messages:
15
  with st.chat_message(msg["role"]):
16
- st.markdown(msg["content"])
17
 
18
- # Chat input
 
 
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({"role": "user", "content": user_input})
 
 
24
  with st.chat_message("user"):
25
  st.markdown(user_input)
26
 
27
- # Call AI engine directly
28
  with st.spinner("Thinking..."):
29
  try:
30
  result = process_question(user_input)
31
  except Exception as e:
32
- result = {"status": "error", "message": str(e)}
 
 
 
 
 
 
 
 
33
 
34
- # Build assistant reply
35
  if result.get("status") == "ok":
36
- reply = ""
37
 
38
- # Time note (if any)
 
 
 
 
39
  if result.get("note"):
40
- reply += f"πŸ•’ *{result['note']}*\n\n"
41
 
42
- # Data table
43
  if result.get("data"):
44
  columns = result.get("columns", [])
45
  data = result["data"]
46
 
47
- table_md = "| " + " | ".join(columns) + " |\n"
48
- table_md += "| " + " | ".join(["---"] * len(columns)) + " |\n"
 
49
 
50
  for row in data[:10]:
51
- table_md += "| " + " | ".join(str(x) for x in row) + " |\n"
52
 
53
- reply += table_md
54
- else:
55
- reply += result.get("message", "No data found.")
56
-
57
- # SQL toggle
58
- reply += "\n\n---\n"
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
- # Show assistant message
67
- st.session_state.messages.append({"role": "assistant", "content": reply})
 
 
 
 
 
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)