-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
221 lines (170 loc) · 6.58 KB
/
Copy pathapp.py
File metadata and controls
221 lines (170 loc) · 6.58 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Local Streamlit front end for the ENT RAG SLM chatbot.
Run from the project root after running run_time_bootstrap.py:
python -m streamlit run app.py \
--server.address=127.0.0.1 \
--server.port=8501
This app is intentionally local-only.
"""
from __future__ import annotations
import gc
import sys
from pathlib import Path
import streamlit as st
import torch
from PIL import Image
# -----------------------------------------------------------------------------
# Import runtime modules
APP_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = APP_DIR
RUNTIME_SCRIPT_DIR = PROJECT_ROOT / "run_time" / "scripts"
for path in [RUNTIME_SCRIPT_DIR, PROJECT_ROOT]:
if str(path) not in sys.path:
sys.path.insert(0, str(path))
from config import HF_TOKEN, SLM_CONFIG # noqa: E402
from chatbot_module import ChatbotArchitecture # noqa: E402
from session_module import ChatbotSession # noqa: E402
# -----------------------------------------------------------------------------
# Page setup
st.set_page_config(
page_title="ENT RAG SLM Chatbot",
page_icon="🩺",
)
st.title("RAG-Enhanced SLM Otolaryngology Chatbot")
st.caption("The Running Prototype")
# -----------------------------------------------------------------------------
# Session-state helpers
def clear_cuda_cache() -> None:
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
def close_existing_session() -> None:
session = st.session_state.get("chatbot_session")
if session is not None:
try:
session.close()
except Exception:
pass
st.session_state.pop("chatbot_session", None)
st.session_state.pop("active_model_label", None)
st.session_state.pop("active_model_card", None)
clear_cuda_cache()
def initialize_session(model_label: str, model_card: str) -> None:
with st.spinner(f"Loading {model_label}..."):
chatbot = ChatbotArchitecture(
model_card=model_card,
is_eval_mode=False,
)
st.session_state.chatbot_session = ChatbotSession(chatbot=chatbot)
st.session_state.active_model_label = model_label
st.session_state.active_model_card = model_card
# -----------------------------------------------------------------------------
# Sidebar controls
model_cards = SLM_CONFIG["MODEL_CARDS"]
model_labels = list(model_cards.keys())
architecture_options = [
"SLM",
"Text-RAG",
"Image-Text-RAG",
"Image-RAG",
]
with st.sidebar:
st.header("Runtime Controls")
if not HF_TOKEN:
st.warning(
"HF_TOKEN was not found in the environment. "
"Add it to .secrets or export it before loading the model."
)
selected_model_label = st.selectbox(
"Model card",
options=model_labels,
index=model_labels.index("Llama 3.2 3B") if "Llama 3.2 3B" in model_labels else 0,
)
selected_model_card = model_cards[selected_model_label]
selected_architecture = st.selectbox(
"Architecture",
options=architecture_options,
index=1,
help="SLM disables retrieval. Text-RAG retrieves text passages. Image-RAG retrieves image-linked neighbouring passages. Image-Text-RAG combines both retrieval routes.",
)
uploaded_image = None
if selected_architecture in {"Image-RAG", "Image-Text-RAG"}:
uploaded_image = st.file_uploader(
"Image input",
type=["png", "jpg", "jpeg", "webp"],
accept_multiple_files=False,
)
st.divider()
load_clicked = st.button("Load / Switch Model", type="primary")
clear_clicked = st.button("Clear Chat History")
close_clicked = st.button("Close Model")
if close_clicked:
close_existing_session()
st.success("Model session closed.")
st.rerun()
if clear_clicked and "chatbot_session" in st.session_state:
st.session_state.chatbot_session.clear_chat_history()
st.success("Chat history cleared.")
st.rerun()
st.divider()
st.markdown("**Active model**")
st.write(st.session_state.get("active_model_label", "No model loaded"))
# -----------------------------------------------------------------------------
# Model lifecycle
model_needs_loading = (
"chatbot_session" not in st.session_state
or st.session_state.get("active_model_card") != selected_model_card
)
if load_clicked or model_needs_loading:
if st.session_state.get("active_model_card") != selected_model_card:
close_existing_session()
initialize_session(selected_model_label, selected_model_card)
st.rerun()
session: ChatbotSession = st.session_state.chatbot_session
# -----------------------------------------------------------------------------
# Architecture routing
def build_generation_inputs(user_prompt: str):
"""Map the selected UI architecture to the session-level generate_response arguments."""
rag_enabled = selected_architecture != "SLM"
user_query = None
user_image = None
if selected_architecture in {"Text-RAG", "Image-Text-RAG"}:
user_query = user_prompt
if selected_architecture in {"Image-RAG", "Image-Text-RAG"}:
if uploaded_image is None:
raise ValueError("This architecture requires an uploaded image.")
user_image = Image.open(uploaded_image).convert("RGB")
return dict(
user_prompt=user_prompt,
user_query=user_query,
user_image=user_image,
RAG_enabled=rag_enabled,
)
# -----------------------------------------------------------------------------
# Chat history rendering
for message in session.chat_history:
role = message.get("role", "assistant")
content = message.get("content", "")
with st.chat_message(role):
st.markdown(content)
# -----------------------------------------------------------------------------
# Chat input and streaming response
placeholder = {
"SLM": "Ask an ENT question without retrieval...",
"Text-RAG": "Ask an ENT question using text retrieval...",
"Image-Text-RAG": "Ask an ENT question using text and image-linked retrieval...",
"Image-RAG": "Ask an ENT question using image-linked retrieval...",
}[selected_architecture]
user_prompt = st.chat_input(placeholder)
if user_prompt:
with st.chat_message("user"):
st.markdown(user_prompt)
try:
generation_inputs = build_generation_inputs(user_prompt)
with st.chat_message("assistant"):
st.write_stream(
session.generate_response(**generation_inputs),
cursor="▌",
)
except Exception as error:
st.error(str(error))