Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -87,110 +87,29 @@ def get_remaining_queries(user_id):
|
|
| 87 |
count = rate_limits.get(user_id, {}).get(today, 0)
|
| 88 |
return MAX_REQUESTS_PER_DAY - count
|
| 89 |
|
| 90 |
-
# Add custom CSS
|
| 91 |
-
def local_css():
|
| 92 |
-
st.markdown("""
|
| 93 |
-
<style>
|
| 94 |
-
.main {
|
| 95 |
-
background-color: #f8f9fa;
|
| 96 |
-
}
|
| 97 |
-
.stApp {
|
| 98 |
-
max-width: 900px;
|
| 99 |
-
margin: 0 auto;
|
| 100 |
-
}
|
| 101 |
-
.chat-message {
|
| 102 |
-
padding: 1.5rem;
|
| 103 |
-
border-radius: 0.5rem;
|
| 104 |
-
margin-bottom: 1rem;
|
| 105 |
-
display: flex;
|
| 106 |
-
flex-direction: column;
|
| 107 |
-
}
|
| 108 |
-
.chat-message.user {
|
| 109 |
-
background-color: #e6f3ff;
|
| 110 |
-
border-left: 5px solid #2b71d9;
|
| 111 |
-
}
|
| 112 |
-
.chat-message.bot {
|
| 113 |
-
background-color: #f0f7f0;
|
| 114 |
-
border-left: 5px solid #28a745;
|
| 115 |
-
}
|
| 116 |
-
.chat-message .message-content {
|
| 117 |
-
display: flex;
|
| 118 |
-
margin-top: 0.5rem;
|
| 119 |
-
}
|
| 120 |
-
.chat-message .avatar {
|
| 121 |
-
min-width: 30px;
|
| 122 |
-
margin-right: 1rem;
|
| 123 |
-
}
|
| 124 |
-
.chat-message .avatar img {
|
| 125 |
-
max-width: 30px;
|
| 126 |
-
max-height: 30px;
|
| 127 |
-
}
|
| 128 |
-
.chat-message .content {
|
| 129 |
-
width: 100%;
|
| 130 |
-
}
|
| 131 |
-
.stTextInput {
|
| 132 |
-
padding: 0.5rem 1rem;
|
| 133 |
-
border-radius: 0.5rem;
|
| 134 |
-
border: 1px solid #ced4da;
|
| 135 |
-
}
|
| 136 |
-
.stButton>button {
|
| 137 |
-
background-color: #28a745;
|
| 138 |
-
color: white;
|
| 139 |
-
border-radius: 0.5rem;
|
| 140 |
-
border: none;
|
| 141 |
-
padding: 0.5rem 1rem;
|
| 142 |
-
}
|
| 143 |
-
h1 {
|
| 144 |
-
color: #2b71d9;
|
| 145 |
-
font-weight: 700;
|
| 146 |
-
}
|
| 147 |
-
.highlight {
|
| 148 |
-
background-color: #f8f9fa;
|
| 149 |
-
padding: 1.5rem;
|
| 150 |
-
border-radius: 0.5rem;
|
| 151 |
-
border-left: 5px solid #dc3545;
|
| 152 |
-
margin: 1rem 0;
|
| 153 |
-
}
|
| 154 |
-
.stProgress > div > div > div {
|
| 155 |
-
background-color: #28a745;
|
| 156 |
-
}
|
| 157 |
-
.sidebar .sidebar-content {
|
| 158 |
-
background-color: #f8f9fa;
|
| 159 |
-
}
|
| 160 |
-
.stAlert {
|
| 161 |
-
border-radius: 0.5rem;
|
| 162 |
-
}
|
| 163 |
-
.progress-container {
|
| 164 |
-
width: 100%;
|
| 165 |
-
background-color: #e9ecef;
|
| 166 |
-
border-radius: 20px;
|
| 167 |
-
margin-top: 10px;
|
| 168 |
-
margin-bottom: 20px;
|
| 169 |
-
}
|
| 170 |
-
.progress-bar {
|
| 171 |
-
height: 10px;
|
| 172 |
-
border-radius: 20px;
|
| 173 |
-
background-color: #28a745;
|
| 174 |
-
}
|
| 175 |
-
.queries-text {
|
| 176 |
-
color: #6c757d;
|
| 177 |
-
font-size: 0.9rem;
|
| 178 |
-
text-align: center;
|
| 179 |
-
margin-top: 5px;
|
| 180 |
-
}
|
| 181 |
-
</style>
|
| 182 |
-
""", unsafe_allow_html=True)
|
| 183 |
-
|
| 184 |
# Set up page configuration
|
| 185 |
st.set_page_config(
|
| 186 |
-
page_title="
|
| 187 |
page_icon="🩺",
|
| 188 |
-
layout="
|
| 189 |
-
initial_sidebar_state="expanded"
|
| 190 |
)
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
# Initialize session state for chat history
|
| 196 |
if 'messages' not in st.session_state:
|
|
@@ -199,40 +118,16 @@ if 'messages' not in st.session_state:
|
|
| 199 |
# Initialize rate limiting
|
| 200 |
init_rate_limiting()
|
| 201 |
|
| 202 |
-
#
|
| 203 |
with st.sidebar:
|
| 204 |
-
|
| 205 |
-
st.
|
| 206 |
-
st.
|
| 207 |
-
|
| 208 |
-
st.markdown("### About")
|
| 209 |
-
st.markdown("""
|
| 210 |
-
This AI assistant is powered by First Aid USMLE Step 1 content and helps medical students prepare for their exams.
|
| 211 |
-
|
| 212 |
-
Ask questions about:
|
| 213 |
-
- Pathology concepts
|
| 214 |
-
- Pharmacology
|
| 215 |
-
- Biochemistry
|
| 216 |
-
- Anatomy
|
| 217 |
-
- And more!
|
| 218 |
-
""")
|
| 219 |
|
| 220 |
-
# Display remaining queries
|
| 221 |
user_id = get_user_id()
|
| 222 |
remaining_queries = get_remaining_queries(user_id)
|
| 223 |
-
|
| 224 |
-
percentage = (used_queries / MAX_REQUESTS_PER_DAY) * 100
|
| 225 |
-
|
| 226 |
-
st.markdown("### Daily Query Limit")
|
| 227 |
-
st.markdown(
|
| 228 |
-
f"""
|
| 229 |
-
<div class="progress-container">
|
| 230 |
-
<div class="progress-bar" style="width: {percentage}%;"></div>
|
| 231 |
-
</div>
|
| 232 |
-
<p class="queries-text">{remaining_queries} of {MAX_REQUESTS_PER_DAY} queries remaining today</p>
|
| 233 |
-
""",
|
| 234 |
-
unsafe_allow_html=True
|
| 235 |
-
)
|
| 236 |
|
| 237 |
st.markdown("---")
|
| 238 |
st.markdown("### Tips for Better Results")
|
|
@@ -258,123 +153,101 @@ os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
|
| 258 |
@st.cache_resource
|
| 259 |
def initialize_rag_chain():
|
| 260 |
try:
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
| 283 |
except Exception as e:
|
| 284 |
-
st.error(f"Error initializing system: {str(e)}")
|
| 285 |
import traceback
|
| 286 |
-
st.text(traceback.format_exc())
|
| 287 |
return None
|
| 288 |
|
| 289 |
-
# Main app
|
| 290 |
-
st.
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
-
|
| 294 |
-
st.markdown("---")
|
| 295 |
|
| 296 |
# Initialize the RAG chain
|
| 297 |
rag_chain = initialize_rag_chain()
|
| 298 |
|
| 299 |
if rag_chain is None:
|
| 300 |
-
st.error("Failed to initialize the system. Please
|
| 301 |
st.stop()
|
| 302 |
|
| 303 |
-
# Create two columns for user avatar
|
| 304 |
-
def display_message(message, is_user=False):
|
| 305 |
-
if is_user:
|
| 306 |
-
avatar = "👤"
|
| 307 |
-
message_type = "user"
|
| 308 |
-
else:
|
| 309 |
-
avatar = "🩺"
|
| 310 |
-
message_type = "bot"
|
| 311 |
-
|
| 312 |
-
st.markdown(
|
| 313 |
-
f"""
|
| 314 |
-
<div class="chat-message {message_type}">
|
| 315 |
-
<div class="message-content">
|
| 316 |
-
<div class="avatar">{avatar}</div>
|
| 317 |
-
<div class="content">{message}</div>
|
| 318 |
-
</div>
|
| 319 |
-
</div>
|
| 320 |
-
""",
|
| 321 |
-
unsafe_allow_html=True
|
| 322 |
-
)
|
| 323 |
-
|
| 324 |
# Display chat history
|
| 325 |
for message in st.session_state.messages:
|
| 326 |
-
|
|
|
|
| 327 |
|
| 328 |
# Get user input
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
# Display response without footer
|
| 356 |
-
display_message(response)
|
| 357 |
-
|
| 358 |
-
except Exception as e:
|
| 359 |
-
error_response = f"Error processing your request: {str(e)}"
|
| 360 |
-
display_message(error_response)
|
| 361 |
-
st.session_state.messages.append({"role": "assistant", "content": error_response})
|
| 362 |
-
st.experimental_rerun()
|
| 363 |
-
|
| 364 |
-
# Add assistant response to chat history
|
| 365 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
|
|
|
| 370 |
|
| 371 |
# Footer
|
| 372 |
st.markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
st.markdown("""
|
| 374 |
-
<div style="text-align: center">
|
| 375 |
-
<p style="
|
| 376 |
-
|
| 377 |
-
It's designed to help with studying, but should not replace professional medical advice.
|
| 378 |
-
</p>
|
| 379 |
</div>
|
| 380 |
""", unsafe_allow_html=True)
|
|
|
|
| 87 |
count = rate_limits.get(user_id, {}).get(today, 0)
|
| 88 |
return MAX_REQUESTS_PER_DAY - count
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# Set up page configuration
|
| 91 |
st.set_page_config(
|
| 92 |
+
page_title="USMLE Step 1 AI",
|
| 93 |
page_icon="🩺",
|
| 94 |
+
layout="centered"
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Custom CSS to style images and improve layout
|
| 98 |
+
st.markdown("""
|
| 99 |
+
<style>
|
| 100 |
+
.header-container {
|
| 101 |
+
display: flex;
|
| 102 |
+
align-items: center;
|
| 103 |
+
gap: 20px;
|
| 104 |
+
}
|
| 105 |
+
.header-image {
|
| 106 |
+
width: 80px;
|
| 107 |
+
}
|
| 108 |
+
.sidebar-image {
|
| 109 |
+
margin-bottom: 20px;
|
| 110 |
+
}
|
| 111 |
+
</style>
|
| 112 |
+
""", unsafe_allow_html=True)
|
| 113 |
|
| 114 |
# Initialize session state for chat history
|
| 115 |
if 'messages' not in st.session_state:
|
|
|
|
| 118 |
# Initialize rate limiting
|
| 119 |
init_rate_limiting()
|
| 120 |
|
| 121 |
+
# Add image to sidebar
|
| 122 |
with st.sidebar:
|
| 123 |
+
st.image("https://img.freepik.com/free-vector/medical-education-abstract-concept-vector-illustration_107173-26967.jpg", width=200, caption="First Aid USMLE", use_column_width=True)
|
| 124 |
+
st.markdown("#### About")
|
| 125 |
+
st.write("This AI assistant is powered by First Aid USMLE Step 1 content and helps medical students prepare for their exams.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Display remaining queries
|
| 128 |
user_id = get_user_id()
|
| 129 |
remaining_queries = get_remaining_queries(user_id)
|
| 130 |
+
st.write(f"Remaining queries today: {remaining_queries}/{MAX_REQUESTS_PER_DAY}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
st.markdown("---")
|
| 133 |
st.markdown("### Tips for Better Results")
|
|
|
|
| 153 |
@st.cache_resource
|
| 154 |
def initialize_rag_chain():
|
| 155 |
try:
|
| 156 |
+
st.sidebar.write("Loading embeddings model...")
|
| 157 |
+
embeddings = download_hugging_face_embeddings()
|
| 158 |
+
|
| 159 |
+
st.sidebar.write("Connecting to Pinecone...")
|
| 160 |
+
index_name = "medprep"
|
| 161 |
+
docsearch = Pinecone.from_existing_index(
|
| 162 |
+
index_name=index_name,
|
| 163 |
+
embedding=embeddings
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k": 3})
|
| 167 |
+
|
| 168 |
+
st.sidebar.write("Initializing OpenAI...")
|
| 169 |
+
llm = OpenAI(model="gpt-3.5-turbo", temperature=0.4, max_tokens=500)
|
| 170 |
+
|
| 171 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 172 |
+
("system", system_prompt),
|
| 173 |
+
("human", "{input}")
|
| 174 |
+
])
|
| 175 |
+
|
| 176 |
+
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
| 177 |
+
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 178 |
+
|
| 179 |
+
st.sidebar.success("✅ System initialized successfully!")
|
| 180 |
+
return rag_chain
|
| 181 |
except Exception as e:
|
| 182 |
+
st.sidebar.error(f"Error initializing system: {str(e)}")
|
| 183 |
import traceback
|
| 184 |
+
st.sidebar.text(traceback.format_exc())
|
| 185 |
return None
|
| 186 |
|
| 187 |
+
# Main app header with logo
|
| 188 |
+
st.markdown("""
|
| 189 |
+
<div class="header-container">
|
| 190 |
+
<img src="https://med.stanford.edu/content/dam/sm-news/images/2014/09/first-aid-logo.gif" class="header-image">
|
| 191 |
+
<h1>First AID USMLE Step 1</h1>
|
| 192 |
+
</div>
|
| 193 |
+
""", unsafe_allow_html=True)
|
| 194 |
|
| 195 |
+
st.write("Ask me any question from First AID USMLE Step 1 book, and I'll try to help!")
|
|
|
|
| 196 |
|
| 197 |
# Initialize the RAG chain
|
| 198 |
rag_chain = initialize_rag_chain()
|
| 199 |
|
| 200 |
if rag_chain is None:
|
| 201 |
+
st.error("Failed to initialize the system. Please check the sidebar for error details.")
|
| 202 |
st.stop()
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
# Display chat history
|
| 205 |
for message in st.session_state.messages:
|
| 206 |
+
with st.chat_message(message["role"]):
|
| 207 |
+
st.markdown(message["content"])
|
| 208 |
|
| 209 |
# Get user input
|
| 210 |
+
if prompt := st.chat_input("Ask a question..."):
|
| 211 |
+
# Add user message to chat history
|
| 212 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 213 |
+
|
| 214 |
+
# Display user message
|
| 215 |
+
with st.chat_message("user"):
|
| 216 |
+
st.markdown(prompt)
|
| 217 |
+
|
| 218 |
+
# Check rate limit
|
| 219 |
+
user_id = get_user_id()
|
| 220 |
+
allowed, count = check_rate_limit(user_id)
|
| 221 |
+
|
| 222 |
+
if not allowed:
|
| 223 |
+
response = f"⚠️ Daily limit reached. You've used {count} queries today. Please try again tomorrow."
|
| 224 |
+
else:
|
| 225 |
+
# Process the query with the RAG chain
|
| 226 |
+
with st.chat_message("assistant"):
|
| 227 |
+
with st.spinner("Thinking..."):
|
| 228 |
+
try:
|
| 229 |
+
result = rag_chain.invoke({"input": prompt})
|
| 230 |
+
response = result.get("answer", "Sorry, I couldn't find an answer to that.")
|
| 231 |
+
remaining = MAX_REQUESTS_PER_DAY - count
|
| 232 |
+
response += f"\n\n\n_You have {remaining} queries remaining today._"
|
| 233 |
+
except Exception as e:
|
| 234 |
+
response = f"Error processing your request: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
st.markdown(response)
|
| 237 |
+
|
| 238 |
+
# Add assistant response to chat history
|
| 239 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 240 |
|
| 241 |
# Footer
|
| 242 |
st.markdown("---")
|
| 243 |
+
# Add a small image in the footer
|
| 244 |
+
cols = st.columns([1, 2, 1])
|
| 245 |
+
with cols[1]:
|
| 246 |
+
st.image("https://i.pinimg.com/originals/ec/81/1c/ec811c4588d10f8286aad7e3ed33e478.png", width=80)
|
| 247 |
+
|
| 248 |
st.markdown("""
|
| 249 |
+
<div style="text-align: center; margin-top: 10px;">
|
| 250 |
+
<p style="font-size: 0.8rem;">This AI assistant uses retrieval augmented generation to provide information from First Aid USMLE Step 1 content.
|
| 251 |
+
It's designed to help with studying, but should not replace professional medical advice.</p>
|
|
|
|
|
|
|
| 252 |
</div>
|
| 253 |
""", unsafe_allow_html=True)
|