bhavika24 commited on
Commit
6ec503c
Β·
verified Β·
1 Parent(s): 324600f

Update UI.py

Browse files
Files changed (1) hide show
  1. UI.py +102 -102
UI.py CHANGED
@@ -1,102 +1,102 @@
1
- import streamlit as st
2
- from engine import process_question,download_transcript_txt,download_transcript_json
3
-
4
-
5
-
6
- st.set_page_config(page_title="Hospital AI Assistant", layout="wide")
7
-
8
- st.title("πŸ₯ Hospital AI Assistant")
9
- st.caption("Ask questions about patients, conditions, visits, medications, labs")
10
-
11
- # =========================
12
- # Session State
13
- # =========================
14
- if "messages" not in st.session_state:
15
- st.session_state.messages = []
16
-
17
- # =========================
18
- # Show Chat History
19
- # =========================
20
- for msg in st.session_state.messages:
21
- with st.chat_message(msg["role"]):
22
- st.markdown(msg["content"], unsafe_allow_html=True)
23
-
24
- # =========================
25
- # Chat Input
26
- # =========================
27
- user_input = st.chat_input("Ask a question about hospital data...")
28
-
29
- if user_input:
30
- # Show user message
31
- st.session_state.messages.append(
32
- {"role": "user", "content": user_input}
33
- )
34
- with st.chat_message("user"):
35
- st.markdown(user_input)
36
-
37
- # Call backend
38
- with st.spinner("Thinking..."):
39
- try:
40
- result = process_question(user_input)
41
- except Exception as e:
42
- result = {
43
- "status": "error",
44
- "message": str(e)
45
- }
46
-
47
- # =========================
48
- # Build Assistant Reply
49
- # =========================
50
- reply = ""
51
-
52
- if result.get("status") == "ok":
53
-
54
- # Message (important for time-based or empty data)
55
- if result.get("message"):
56
- reply += f"❗ {result['message']}\n\n"
57
-
58
- # Time note
59
- if result.get("note"):
60
- reply += f"πŸ•’ {result['note']}\n\n"
61
-
62
- # Table output
63
- if result.get("data"):
64
- columns = result.get("columns", [])
65
- data = result["data"]
66
-
67
- reply += "### Result\n"
68
- reply += "| " + " | ".join(columns) + " |\n"
69
- reply += "| " + " | ".join(["---"] * len(columns)) + " |\n"
70
-
71
- for row in data[:10]:
72
- reply += "| " + " | ".join(str(x) for x in row) + " |\n"
73
-
74
- # SQL (ONLY if present)
75
- if result.get("sql"):
76
- reply += "\n---\n"
77
- reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
78
- reply += f"```sql\n{result['sql']}\n```"
79
- reply += "\n</details>"
80
-
81
- else:
82
- reply = f"❌ {result.get('message', 'Something went wrong')}"
83
-
84
- # =========================
85
- # Display Assistant Message
86
- # =========================
87
- st.session_state.messages.append(
88
- {"role": "assistant", "content": reply}
89
- )
90
-
91
- with st.chat_message("assistant"):
92
- st.markdown(reply, unsafe_allow_html=True)
93
-
94
- # Download button inside chat bubble
95
- st.download_button(
96
- label="⬇️ Download conversation",
97
- data=download_transcript_txt(),
98
- file_name="chat_transcript.txt",
99
- mime="text/plain",
100
- use_container_width=True
101
- )
102
-
 
1
+ import streamlit as st
2
+ from engine import process_question,download_transcript_txt
3
+
4
+
5
+
6
+ st.set_page_config(page_title="Hospital AI Assistant", layout="wide")
7
+
8
+ st.title("πŸ₯ Hospital AI Assistant")
9
+ st.caption("Ask questions about patients, conditions, visits, medications, labs")
10
+
11
+ # =========================
12
+ # Session State
13
+ # =========================
14
+ if "messages" not in st.session_state:
15
+ st.session_state.messages = []
16
+
17
+ # =========================
18
+ # Show Chat History
19
+ # =========================
20
+ for msg in st.session_state.messages:
21
+ with st.chat_message(msg["role"]):
22
+ st.markdown(msg["content"], unsafe_allow_html=True)
23
+
24
+ # =========================
25
+ # Chat Input
26
+ # =========================
27
+ user_input = st.chat_input("Ask a question about hospital data...")
28
+
29
+ if user_input:
30
+ # Show user message
31
+ st.session_state.messages.append(
32
+ {"role": "user", "content": user_input}
33
+ )
34
+ with st.chat_message("user"):
35
+ st.markdown(user_input)
36
+
37
+ # Call backend
38
+ with st.spinner("Thinking..."):
39
+ try:
40
+ result = process_question(user_input)
41
+ except Exception as e:
42
+ result = {
43
+ "status": "error",
44
+ "message": str(e)
45
+ }
46
+
47
+ # =========================
48
+ # Build Assistant Reply
49
+ # =========================
50
+ reply = ""
51
+
52
+ if result.get("status") == "ok":
53
+
54
+ # Message (important for time-based or empty data)
55
+ if result.get("message"):
56
+ reply += f"❗ {result['message']}\n\n"
57
+
58
+ # Time note
59
+ if result.get("note"):
60
+ reply += f"πŸ•’ {result['note']}\n\n"
61
+
62
+ # Table output
63
+ if result.get("data"):
64
+ columns = result.get("columns", [])
65
+ data = result["data"]
66
+
67
+ reply += "### Result\n"
68
+ reply += "| " + " | ".join(columns) + " |\n"
69
+ reply += "| " + " | ".join(["---"] * len(columns)) + " |\n"
70
+
71
+ for row in data[:10]:
72
+ reply += "| " + " | ".join(str(x) for x in row) + " |\n"
73
+
74
+ # SQL (ONLY if present)
75
+ if result.get("sql"):
76
+ reply += "\n---\n"
77
+ reply += "<details><summary><b>Generated SQL</b></summary>\n\n"
78
+ reply += f"```sql\n{result['sql']}\n```"
79
+ reply += "\n</details>"
80
+
81
+ else:
82
+ reply = f"❌ {result.get('message', 'Something went wrong')}"
83
+
84
+ # =========================
85
+ # Display Assistant Message
86
+ # =========================
87
+ st.session_state.messages.append(
88
+ {"role": "assistant", "content": reply}
89
+ )
90
+
91
+ with st.chat_message("assistant"):
92
+ st.markdown(reply, unsafe_allow_html=True)
93
+
94
+ # Download button inside chat bubble
95
+ st.download_button(
96
+ label="⬇️ Download conversation",
97
+ data=download_transcript_txt(),
98
+ file_name="chat_transcript.txt",
99
+ mime="text/plain",
100
+ use_container_width=True
101
+ )
102
+