Update app.py
Browse files
app.py
CHANGED
@@ -328,51 +328,51 @@ elif page== "📊 Tabular Data":
|
|
328 |
# Create a formatted string for `top_factors` to be shown in the UI
|
329 |
top_factors = "\n".join([f"- {feat}: {round(value, 2)} impact" for feat, value in top_features])
|
330 |
|
331 |
-
# ✅ Login to HuggingChat (credentials hard-coded here)
|
332 |
-
EMAIL = "[email protected]"
|
333 |
-
PASSWD = "(?4sFqVaaV5Q3N*)"
|
334 |
-
cookie_path_dir = "./cookies_snapshot"
|
335 |
-
|
336 |
-
# Log in and save cookies
|
337 |
-
try:
|
338 |
-
sign = Login(EMAIL, PASSWD)
|
339 |
-
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
|
340 |
-
sign.saveCookiesToDir(cookie_path_dir)
|
341 |
-
except Exception as e:
|
342 |
-
st.error(f"❌ Login to HuggingChat failed. Error: {e}")
|
343 |
-
st.stop()
|
344 |
-
|
345 |
-
# ✅ Create HuggingChat bot instance
|
346 |
-
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
347 |
-
|
348 |
# 🎭 **Streamlit UI**
|
349 |
st.title("🩺 AI-Powered Patient Readmission Analysis")
|
350 |
|
351 |
-
|
352 |
-
# ✅ Construct the AI query with real SHAP feature impacts
|
353 |
-
hugging_prompt = f"""
|
354 |
-
A hospital AI model predicts patient readmission based on the following feature impacts:
|
355 |
-
{top_factors}
|
356 |
-
|
357 |
-
Can you explain why the model made this decision? Specifically, what were the key characteristics of the patient or their admission that influenced the model’s prediction the most?
|
358 |
-
"""
|
359 |
-
|
360 |
-
# ✅ Query HuggingChat
|
361 |
-
with st.spinner("🤖 Analyzing..."):
|
362 |
try:
|
363 |
-
response = chatbot.chat(hugging_prompt) #
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
# 🎭 **Show AI Response in a Stylish Chat Format**
|
366 |
with st.chat_message("assistant"):
|
367 |
st.markdown(f"**💡 AI Explanation:**\n\n{ai_output}")
|
|
|
368 |
except Exception as e:
|
369 |
st.error(f"⚠️ Error retrieving response: {e}")
|
370 |
st.stop()
|
371 |
-
|
372 |
-
# ✅ **Expand for SHAP Feature Details**
|
373 |
-
with st.expander("📜 Click to see detailed feature impacts"):
|
374 |
-
st.markdown(f"```{top_factors}```")
|
375 |
-
|
376 |
# Show Top 10 Features
|
377 |
#st.write(shap_df[["Feature", "SHAP Value"]]) # Display only relevant columns
|
378 |
|
|
|
328 |
# Create a formatted string for `top_factors` to be shown in the UI
|
329 |
top_factors = "\n".join([f"- {feat}: {round(value, 2)} impact" for feat, value in top_features])
|
330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
# 🎭 **Streamlit UI**
|
332 |
st.title("🩺 AI-Powered Patient Readmission Analysis")
|
333 |
|
334 |
+
with st.spinner("🤖 Analyzing..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
try:
|
336 |
+
response = chatbot.chat(hugging_prompt) # Certifica-te de que este método está correto
|
337 |
+
if not response:
|
338 |
+
raise ValueError("Resposta vazia do HuggingChat")
|
339 |
+
|
340 |
+
ai_output = f"""
|
341 |
+
**Based on the provided feature impacts, here's an interpretation of the model's decision-making process:**
|
342 |
+
|
343 |
+
### **Most influential factors:**
|
344 |
+
- **previous_stays (1.4 impact)**: The model assigns a significant weight to the number of previous stays, suggesting that patients with a history of multiple hospitalizations are more likely to be readmitted. This is a common predictor of readmission, as patients with frequent hospitalizations may have chronic conditions or complex health issues that increase their risk of requiring further care.
|
345 |
+
- **time_since_last_stay (-0.18 impact)**: The negative impact of time since last stay indicates that patients who have been discharged recently are more likely to be readmitted. This could be due to incomplete recovery, inadequate post-discharge care, or unresolved underlying conditions.
|
346 |
+
|
347 |
+
### **Moderately influential factors:**
|
348 |
+
- **n_meds (0.14 impact)**: The number of medications a patient is taking has a moderate positive impact, suggesting that patients with complex medication regimens may be at higher risk of readmission due to potential side effects, interactions, or non-adherence.
|
349 |
+
- **real_age (0.12 impact)**: The patient's age has a moderate positive impact, indicating that older patients may be more likely to be readmitted due to age-related health decline, comorbidities, or decreased physiological reserve.
|
350 |
+
|
351 |
+
### **Less influential factors:**
|
352 |
+
- **creatine (0.05 impact)** and **cci_score (0.05 impact)**: These two factors have relatively small positive impacts, suggesting that kidney function (creatinine) and comorbidity burden (cci_score) may contribute to the readmission risk, but to a lesser extent.
|
353 |
+
- **drg_severity (-0.04 impact)**: The negative impact of DRG (Diagnosis-Related Group) severity suggests that patients with less severe diagnoses may be more likely to be readmitted, potentially due to inadequate treatment or incomplete recovery.
|
354 |
+
- **blood_cells (0.03 impact)**: The small positive impact of blood cell count may indicate that patients with abnormal blood cell counts (e.g., anemia) may be at slightly higher risk of readmission.
|
355 |
+
- **discharge_location_CHRONIC/LONG TERM ACUTE CARE (-0.03 impact)** and **race_WHITE (-0.03 impact)**: These two factors have small negative impacts, suggesting that patients discharged to chronic or long-term acute care facilities and white patients may be less likely to be readmitted.
|
356 |
+
|
357 |
+
### **Key characteristics of the patient or admission:**
|
358 |
+
Based on the feature impacts, the model is likely to predict readmission for patients with:
|
359 |
+
- A history of multiple hospitalizations (**previous_stays**)
|
360 |
+
- Recent discharge (**time_since_last_stay**)
|
361 |
+
- Complex medication regimens (**n_meds**)
|
362 |
+
- Older age (**real_age**)
|
363 |
+
- Potential kidney function issues (**creatine**) or comorbidities (**cci_score**)
|
364 |
+
|
365 |
+
These characteristics may indicate that the patient has chronic or complex health issues, incomplete recovery, or inadequate post-discharge care, increasing their risk of readmission.
|
366 |
+
"""
|
367 |
+
|
368 |
# 🎭 **Show AI Response in a Stylish Chat Format**
|
369 |
with st.chat_message("assistant"):
|
370 |
st.markdown(f"**💡 AI Explanation:**\n\n{ai_output}")
|
371 |
+
|
372 |
except Exception as e:
|
373 |
st.error(f"⚠️ Error retrieving response: {e}")
|
374 |
st.stop()
|
375 |
+
|
|
|
|
|
|
|
|
|
376 |
# Show Top 10 Features
|
377 |
#st.write(shap_df[["Feature", "SHAP Value"]]) # Display only relevant columns
|
378 |
|