bhavika24 commited on
Commit
68078e3
Β·
verified Β·
1 Parent(s): 4f370da

Update UI.py

Browse files
Files changed (1) hide show
  1. UI.py +121 -119
UI.py CHANGED
@@ -1,119 +1,121 @@
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 Conversation
95
- # =========================
96
- st.divider()
97
- st.subheader("πŸ“₯ Download Conversation")
98
-
99
- col1, col2 = st.columns(2)
100
-
101
- with col1:
102
- if st.button("Download Transcript (TXT)"):
103
- txt = download_transcript_txt()
104
- st.download_button(
105
- label="Download TXT",
106
- data=txt,
107
- file_name="chat_transcript.txt",
108
- mime="text/plain"
109
- )
110
-
111
- with col2:
112
- if st.button("Download Transcript (JSON)"):
113
- js = download_transcript_json()
114
- st.download_button(
115
- label="Download JSON",
116
- data=js,
117
- file_name="chat_transcript.json",
118
- mime="application/json"
119
- )
 
 
 
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 Conversation
95
+ # =========================
96
+ st.divider()
97
+ st.subheader("πŸ“₯ Download Conversation")
98
+
99
+ # Create 3 columns: spacer | TXT | JSON
100
+ col_spacer, col_txt, col_json = st.columns([6, 2, 2])
101
+
102
+ with col_txt:
103
+ txt = download_transcript_txt()
104
+ st.download_button(
105
+ label="πŸ“„ TXT",
106
+ data=txt,
107
+ file_name="chat_transcript.txt",
108
+ mime="text/plain",
109
+ use_container_width=True
110
+ )
111
+
112
+ with col_json:
113
+ js = download_transcript_json()
114
+ st.download_button(
115
+ label="🧾 JSON",
116
+ data=js,
117
+ file_name="chat_transcript.json",
118
+ mime="application/json",
119
+ use_container_width=True
120
+ )
121
+