mxp1404 commited on
Commit
5ea7161
Β·
verified Β·
1 Parent(s): cd6e566

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -1,9 +1,4 @@
1
- """
2
- UHC Medical Policy Chatbot β€” Streamlit Web UI.
3
-
4
- Deployed on HuggingFace Spaces. Uses MedEmbed for retrieval from Qdrant Cloud
5
- and Groq (Llama 3.1 8B) for answer generation.
6
- """
7
 
8
  import time
9
  import streamlit as st
@@ -13,18 +8,12 @@ from chatbot.retriever import PolicyRetriever
13
  from chatbot.llm_groq import GroqClient, GroqError
14
  from chatbot.prompts import format_context, build_messages, deduplicate_chunks
15
 
16
- # ---------------------------------------------------------------------------
17
- # Page config
18
- # ---------------------------------------------------------------------------
19
  st.set_page_config(
20
  page_title="UHC Policy Chatbot",
21
  page_icon="πŸ₯",
22
  layout="centered",
23
  )
24
 
25
- # ---------------------------------------------------------------------------
26
- # Cached singletons β€” loaded once, shared across reruns
27
- # ---------------------------------------------------------------------------
28
 
29
  @st.cache_resource(show_spinner=False)
30
  def load_retriever() -> PolicyRetriever:
@@ -62,18 +51,17 @@ with st.sidebar:
62
  )
63
  st.divider()
64
 
 
 
65
  if st.button("πŸ—‘οΈ Clear conversation"):
66
  st.session_state.messages = []
67
  st.session_state.chunks_history = []
68
  st.rerun()
69
 
70
- st.caption(
71
- "Built for the CombineHealth Technical Assignment. "
72
- "Answers are generated from policy documents only."
73
- )
74
 
75
  # ---------------------------------------------------------------------------
76
- # Init session state
77
  # ---------------------------------------------------------------------------
78
  if "messages" not in st.session_state:
79
  st.session_state.messages = []
@@ -92,9 +80,6 @@ except GroqError as e:
92
  st.error(f"LLM initialization failed: {e}")
93
  st.stop()
94
 
95
- # ---------------------------------------------------------------------------
96
- # Header
97
- # ---------------------------------------------------------------------------
98
  st.title("πŸ₯ UHC Medical Policy Chatbot")
99
  st.caption(
100
  "Ask questions about UnitedHealthcare insurance policies. "
@@ -132,7 +117,6 @@ if query := st.chat_input("Ask about UHC medical policies..."):
132
  with st.chat_message("user"):
133
  st.markdown(query)
134
 
135
- # -- Retrieve -------------------------------------------------------------
136
  with st.chat_message("assistant"):
137
  with st.spinner("Searching policies..."):
138
  t0 = time.perf_counter()
@@ -166,7 +150,6 @@ if query := st.chat_input("Ask about UHC medical policies..."):
166
 
167
  messages = build_messages(query, context, history=history_for_llm)
168
 
169
- # -- Generate -----------------------------------------------------------
170
  try:
171
  t1 = time.perf_counter()
172
  response_text = st.write_stream(llm.chat_stream(messages))
@@ -175,7 +158,6 @@ if query := st.chat_input("Ask about UHC medical policies..."):
175
  st.error(str(e))
176
  st.stop()
177
 
178
- # -- Sources -----------------------------------------------------------
179
  deduped = deduplicate_chunks(chunks)
180
  with st.expander("πŸ“š Sources", expanded=False):
181
  for c in deduped:
@@ -188,6 +170,15 @@ if query := st.chat_input("Ask about UHC medical policies..."):
188
  f"Retrieval: {t_retrieval:.1f}s Β· Generation: {t_gen:.1f}s"
189
  )
190
 
 
 
 
 
 
 
 
 
 
191
  st.session_state.messages.append(
192
  {"role": "assistant", "content": response_text}
193
  )
 
1
+ """Streamlit web interface for the UHC medical policy chatbot."""
 
 
 
 
 
2
 
3
  import time
4
  import streamlit as st
 
8
  from chatbot.llm_groq import GroqClient, GroqError
9
  from chatbot.prompts import format_context, build_messages, deduplicate_chunks
10
 
 
 
 
11
  st.set_page_config(
12
  page_title="UHC Policy Chatbot",
13
  page_icon="πŸ₯",
14
  layout="centered",
15
  )
16
 
 
 
 
17
 
18
  @st.cache_resource(show_spinner=False)
19
  def load_retriever() -> PolicyRetriever:
 
51
  )
52
  st.divider()
53
 
54
+ tts_enabled = st.toggle("πŸ”Š Read answers aloud", value=False)
55
+
56
  if st.button("πŸ—‘οΈ Clear conversation"):
57
  st.session_state.messages = []
58
  st.session_state.chunks_history = []
59
  st.rerun()
60
 
61
+ st.caption("Answers are based on official UHC policy documents only.")
 
 
 
62
 
63
  # ---------------------------------------------------------------------------
64
+ # Session state
65
  # ---------------------------------------------------------------------------
66
  if "messages" not in st.session_state:
67
  st.session_state.messages = []
 
80
  st.error(f"LLM initialization failed: {e}")
81
  st.stop()
82
 
 
 
 
83
  st.title("πŸ₯ UHC Medical Policy Chatbot")
84
  st.caption(
85
  "Ask questions about UnitedHealthcare insurance policies. "
 
117
  with st.chat_message("user"):
118
  st.markdown(query)
119
 
 
120
  with st.chat_message("assistant"):
121
  with st.spinner("Searching policies..."):
122
  t0 = time.perf_counter()
 
150
 
151
  messages = build_messages(query, context, history=history_for_llm)
152
 
 
153
  try:
154
  t1 = time.perf_counter()
155
  response_text = st.write_stream(llm.chat_stream(messages))
 
158
  st.error(str(e))
159
  st.stop()
160
 
 
161
  deduped = deduplicate_chunks(chunks)
162
  with st.expander("πŸ“š Sources", expanded=False):
163
  for c in deduped:
 
170
  f"Retrieval: {t_retrieval:.1f}s Β· Generation: {t_gen:.1f}s"
171
  )
172
 
173
+ if tts_enabled and response_text:
174
+ with st.spinner("Generating audio..."):
175
+ try:
176
+ from chatbot.tts import synthesize
177
+ audio_bytes = synthesize(response_text)
178
+ st.audio(audio_bytes, format="audio/wav", autoplay=True)
179
+ except Exception as e:
180
+ st.caption(f"⚠️ TTS unavailable: {e}")
181
+
182
  st.session_state.messages.append(
183
  {"role": "assistant", "content": response_text}
184
  )