Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +189 -153
src/streamlit_app.py
CHANGED
|
@@ -29,28 +29,29 @@ import requests
|
|
| 29 |
from transformers import pipeline, BioGptTokenizer, BioGptForCausalLM
|
| 30 |
from transformers import MarianMTModel, MarianTokenizer
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
with open("translations.json", "r", encoding="utf-8") as f:
|
| 36 |
-
|
| 37 |
-
return translations
|
| 38 |
except FileNotFoundError:
|
| 39 |
-
st.error("Translation file not found.")
|
| 40 |
return {}
|
| 41 |
except json.JSONDecodeError:
|
| 42 |
-
st.error("Translation file is malformed.")
|
| 43 |
return {}
|
| 44 |
|
| 45 |
-
translations =
|
| 46 |
|
| 47 |
# Get translation function
|
| 48 |
def t(key, lang="fr"):
|
| 49 |
-
|
| 50 |
-
return translation
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Load custom CSS
|
| 56 |
def load_css(file_name):
|
|
@@ -85,7 +86,7 @@ class Patient(Base):
|
|
| 85 |
biomarkers = Column(String)
|
| 86 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 87 |
|
| 88 |
-
engine = create_engine('sqlite:///patients.db')
|
| 89 |
Base.metadata.create_all(engine)
|
| 90 |
Session = sessionmaker(bind=engine)
|
| 91 |
|
|
@@ -454,11 +455,11 @@ def send_to_rasa(message, lang):
|
|
| 454 |
# Load model
|
| 455 |
input_dims = [100, 50, 30, 20]
|
| 456 |
best_hyperparams = {
|
| 457 |
-
'hidden_dim': 256, 'latent_dim':
|
| 458 |
}
|
| 459 |
try:
|
| 460 |
model = OmicsVAE(input_dims=input_dims, **best_hyperparams).to(device)
|
| 461 |
-
model.load_state_dict(torch.load("
|
| 462 |
model.eval()
|
| 463 |
except FileNotFoundError:
|
| 464 |
st.error(t("model_weights_error"))
|
|
@@ -475,15 +476,15 @@ lang = st.session_state.language
|
|
| 475 |
|
| 476 |
# Home Page
|
| 477 |
if page == t("home"):
|
| 478 |
-
st.markdown(f"<div class='title animate__fadeIn'>{t('
|
| 479 |
st.markdown(f"""
|
| 480 |
<div class='intro animate__fadeIn'>
|
| 481 |
<p>{t('home_intro_1').format(author="<b>Ngoue David Roger Yannick</b>")}</p>
|
| 482 |
<p>{t('home_intro_2')}</p>
|
| 483 |
-
|
| 484 |
""", unsafe_allow_html=True)
|
| 485 |
|
| 486 |
-
st.markdown(f"<div class='section-header'>{t('why_omics')}</div>", unsafe_allow_html=True)
|
| 487 |
st.markdown(f"""
|
| 488 |
<div class='content animate__fadeIn'>
|
| 489 |
<p>{t('omics_description')}</p>
|
|
@@ -493,13 +494,13 @@ if page == t("home"):
|
|
| 493 |
<li><b>{t('proteomics')}</b>: {t('proteomics_desc')}</li>
|
| 494 |
<li><b>{t('metabolomics')}</b>: {t('metabolomics_desc')}</li>
|
| 495 |
</ul>
|
|
|
|
| 496 |
<p>{t('omics_integration')}</p>
|
| 497 |
-
</div>
|
| 498 |
""", unsafe_allow_html=True)
|
| 499 |
|
| 500 |
# About Omics Data Page
|
| 501 |
elif page == t("about_omics"):
|
| 502 |
-
st.markdown(f"<div class='title animate__fadeIn'>{t('
|
| 503 |
st.markdown(f"""
|
| 504 |
<div class='content animate__fadeIn'>
|
| 505 |
<p>{t('about_omics_intro')}</p>
|
|
@@ -509,20 +510,20 @@ elif page == t("about_omics"):
|
|
| 509 |
st.markdown(f"<div class='section-header'>{t('raw_omics_formats')}</div>", unsafe_allow_html=True)
|
| 510 |
st.markdown(f"""
|
| 511 |
<div class='content animate__fadeIn'>
|
| 512 |
-
<p>{t('
|
| 513 |
<ul>
|
| 514 |
<li><b>{t('genomics')}</b>: {t('genomics_format')}</li>
|
| 515 |
<li><b>{t('transcriptomics')}</b>: {t('transcriptomics_format')}</li>
|
| 516 |
<li><b>{t('proteomics')}</b>: {t('proteomics_format')}</li>
|
| 517 |
<li><b>{t('metabolomics')}</b>: {t('metabolomics_format')}</li>
|
| 518 |
</ul>
|
|
|
|
| 519 |
<p>{t('raw_omics_processing')}</p>
|
| 520 |
-
</div>
|
| 521 |
""", unsafe_allow_html=True)
|
| 522 |
|
| 523 |
-
st.markdown(f"<div class='section-header'>{t('preprocessing_steps')}</div>", unsafe_allow_html=True)
|
| 524 |
st.markdown(f"""
|
| 525 |
-
<div class='content animate__fadeIn'>
|
| 526 |
<p>{t('preprocessing_desc')}</p>
|
| 527 |
<ol>
|
| 528 |
<li><b>{t('quality_control')}</b>: {t('quality_control_desc')}</li>
|
|
@@ -531,8 +532,8 @@ elif page == t("about_omics"):
|
|
| 531 |
<li><b>{t('standardization')}</b>: {t('standardization_desc')}</li>
|
| 532 |
<li><b>{t('feature_selection')}</b>: {t('feature_selection_desc')}</li>
|
| 533 |
</ol>
|
|
|
|
| 534 |
<p>{t('preprocessing_importance')}</p>
|
| 535 |
-
</div>
|
| 536 |
""", unsafe_allow_html=True)
|
| 537 |
|
| 538 |
st.markdown(f"<div class='section-header'>{t('csv_format')}</div>", unsafe_allow_html=True)
|
|
@@ -546,18 +547,19 @@ elif page == t("about_omics"):
|
|
| 546 |
<li><b>{t('separator')}</b>: {t('separator_desc')}</li>
|
| 547 |
<li><b>{t('header')}</b>: {t('header_desc')}</li>
|
| 548 |
</ul>
|
|
|
|
| 549 |
<p>{t('csv_example')}</p>
|
| 550 |
<pre class='code-block'>
|
| 551 |
-
|
| 552 |
-
P001,0,1,
|
| 553 |
</pre>
|
| 554 |
-
<p>{t('
|
| 555 |
</div>
|
| 556 |
""", unsafe_allow_html=True)
|
| 557 |
|
| 558 |
st.markdown(f"<div class='section-header'>{t('data_quality')}</div>", unsafe_allow_html=True)
|
| 559 |
st.markdown(f"""
|
| 560 |
-
<div class='content animate__fadeIn'>
|
| 561 |
<p>{t('data_quality_desc')}</p>
|
| 562 |
<ul>
|
| 563 |
<li><b>{t('consistency')}</b>: {t('consistency_desc')}</li>
|
|
@@ -565,8 +567,8 @@ P001,0,1,2,Unknown
|
|
| 565 |
<li><b>{t('accuracy')}</b>: {t('accuracy_desc')}</li>
|
| 566 |
<li><b>{t('alignment')}</b>: {t('alignment_desc')}</li>
|
| 567 |
</ul>
|
|
|
|
| 568 |
<p>{t('data_quality_importance')}</p>
|
| 569 |
-
</div>
|
| 570 |
""", unsafe_allow_html=True)
|
| 571 |
|
| 572 |
# Predict Risk Page
|
|
@@ -595,9 +597,9 @@ elif page == t("predict_risk"):
|
|
| 595 |
cols = st.columns(4)
|
| 596 |
for idx, omic in enumerate(file_paths):
|
| 597 |
with cols[idx]:
|
| 598 |
-
uploaded_file = st.file_uploader(f"{t(omic)} (CSV)", type="csv", key=omic)
|
| 599 |
if uploaded_file:
|
| 600 |
-
temp_path = f"temp_{omic}.csv"
|
| 601 |
with open(temp_path, "wb") as f:
|
| 602 |
f.write(uploaded_file.getbuffer())
|
| 603 |
file_paths[omic] = temp_path
|
|
@@ -618,51 +620,71 @@ elif page == t("predict_risk"):
|
|
| 618 |
patient_tensors = [torch.tensor(data_dict[omic].values, dtype=torch.float32) for omic in data_dict]
|
| 619 |
patient_dataset = TensorDataset(*patient_tensors)
|
| 620 |
patient_loader = DataLoader(patient_dataset, batch_size=best_hyperparams['batch_size'],
|
| 621 |
-
|
| 622 |
|
| 623 |
# Extract latent representations
|
| 624 |
encoded_data = extract_latent_representations(model, patient_loader, device)
|
| 625 |
|
| 626 |
# Calculate risk scores
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
|
| 629 |
# Perform SHAP analysis
|
| 630 |
-
output_dir = "temp_output"
|
| 631 |
os.makedirs(output_dir, exist_ok=True)
|
| 632 |
-
|
|
|
|
| 633 |
model, combined_data, input_dims, device, feature_names, output_dir, irc_biomarkers
|
| 634 |
)
|
|
|
|
|
|
|
| 635 |
|
| 636 |
# Generate dynamic recommendations
|
|
|
|
| 637 |
risk_score = umap_df[t('risk_score')].iloc[0]
|
| 638 |
-
biomarkers =
|
| 639 |
recommendations = []
|
| 640 |
for intent in ["medication", "diet", "exercise"]:
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
|
| 649 |
# Generate PDF report
|
|
|
|
| 650 |
pdf_path = os.path.join(output_dir, f"ckd_risk_report_{st.session_state.patient_id}.pdf")
|
| 651 |
-
generate_pdf_report(risk_score, important_biomarkers, shap_plot_path, recommendations, pdf_path,
|
| 652 |
lang)
|
|
|
|
|
|
|
| 653 |
|
| 654 |
# Save to database
|
|
|
|
| 655 |
session = Session()
|
| 656 |
patient = Patient(
|
| 657 |
patient_id=st.session_state.patient_id,
|
| 658 |
risk_score=risk_score,
|
| 659 |
biomarkers=', '.join(biomarkers)
|
| 660 |
)
|
| 661 |
-
session.add(
|
| 662 |
session.commit()
|
| 663 |
session.close()
|
|
|
|
|
|
|
| 664 |
|
| 665 |
# Store results
|
|
|
|
| 666 |
st.session_state.results = {
|
| 667 |
'umap_df': umap_df,
|
| 668 |
'important_biomarkers': important_biomarkers,
|
|
@@ -670,86 +692,90 @@ elif page == t("predict_risk"):
|
|
| 670 |
'pdf_path': pdf_path,
|
| 671 |
'recommendations': recommendations
|
| 672 |
}
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
'
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
}
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
st.pyplot(shap_fig)
|
| 719 |
|
| 720 |
# Display saved SHAP plot
|
| 721 |
if os.path.exists(shap_plot_path):
|
| 722 |
-
st.image(shap_plot_path, caption=t('
|
| 723 |
|
| 724 |
# Recommendations
|
| 725 |
st.markdown(f"<div class='subheader'>{t('recommendations')}</div>", unsafe_allow_html=True)
|
| 726 |
for rec in recommendations:
|
| 727 |
-
st.markdown(f"<div class='recommendation'>{rec}</div>",
|
| 728 |
|
| 729 |
# Download results
|
| 730 |
st.markdown(f"<div class='subheader'>{t('download_results')}</div>", unsafe_allow_html=True)
|
| 731 |
-
col1, col2 = st.columns(2)
|
| 732 |
with col1:
|
| 733 |
-
|
| 734 |
-
|
| 735 |
download_text = t('download_biomarkers', lang=st.session_state.language)
|
| 736 |
href = f'<a href="data:file/csv;base64,{b64}" download="important_biomarkers.csv" class="download-btn">{download_text}</a>'
|
| 737 |
st.markdown(href, unsafe_allow_html=True)
|
| 738 |
with col2:
|
| 739 |
with open(pdf_path, "rb") as f:
|
| 740 |
pdf_data = f.read()
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
except Exception as e:
|
| 747 |
st.error(t("file_process_error").format(error=str(e)))
|
|
|
|
| 748 |
else:
|
| 749 |
-
st.warning(t("
|
| 750 |
|
| 751 |
# Chatbot Assistant Page
|
| 752 |
-
|
| 753 |
st.markdown(f"<div class='title animate__fadeIn'>{t('chatbot_title')}</div>", unsafe_allow_html=True)
|
| 754 |
st.markdown(f"""
|
| 755 |
<div class='content animate__fadeIn'>
|
|
@@ -757,9 +783,9 @@ elif page == t("chatbot"):
|
|
| 757 |
</div>
|
| 758 |
""", unsafe_allow_html=True)
|
| 759 |
|
| 760 |
-
# Chat
|
| 761 |
st.markdown(f"<div class='section-header'>{t('chat_with_assistant')}</div>", unsafe_allow_html=True)
|
| 762 |
-
chat_container = st.container()
|
| 763 |
with chat_container:
|
| 764 |
for message in st.session_state.chat_history:
|
| 765 |
if message['role'] == 'user':
|
|
@@ -768,50 +794,57 @@ elif page == t("chatbot"):
|
|
| 768 |
st.markdown(f"<div class='chat-bubble bot'>{message['content']}</div>", unsafe_allow_html=True)
|
| 769 |
|
| 770 |
# User input
|
| 771 |
-
user_input = st.
|
| 772 |
-
if st.button(t("send"), key="
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
# Generate recommendation based on intent
|
| 786 |
-
intent = None
|
| 787 |
-
if any(keyword in user_input.lower() for keyword in t('chat_medication_keywords').split(',')):
|
| 788 |
-
intent = "medication"
|
| 789 |
-
elif any(keyword in user_input.lower() for keyword in t('chat_diet_keywords').split(',')):
|
| 790 |
-
intent = "diet"
|
| 791 |
-
elif any(keyword in user_input.lower() for keyword in t('chat_exercise_keywords').split(',')):
|
| 792 |
-
intent = "exercise"
|
| 793 |
-
|
| 794 |
-
if intent and st.session_state.results:
|
| 795 |
-
risk_score = st.session_state.results['umap_df'][t('risk_score')].iloc[0]
|
| 796 |
-
biomarkers = st.session_state.results['important_biomarkers'][t('feature')].head(5).tolist()
|
| 797 |
-
prompt = formulate_prompt(intent, risk_score, biomarkers)
|
| 798 |
-
response = generate_recommendation(prompt)
|
| 799 |
-
if validate_recommendation(response):
|
| 800 |
-
response = translate_recommendation(response, lang)
|
| 801 |
else:
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
|
| 812 |
# Dashboard Page
|
| 813 |
-
elif page == t("dashboard"):
|
| 814 |
st.markdown(f"<div class='title animate__fadeIn'>{t('dashboard_title')}</div>", unsafe_allow_html=True)
|
|
|
|
| 815 |
st.markdown(f"""
|
| 816 |
<div class='content animate__fadeIn'>
|
| 817 |
<p>{t('dashboard_intro')}</p>
|
|
@@ -839,7 +872,7 @@ elif page == t("dashboard"):
|
|
| 839 |
st.dataframe(filtered_df, use_container_width=True)
|
| 840 |
|
| 841 |
# Plot
|
| 842 |
-
fig = px.
|
| 843 |
filtered_df,
|
| 844 |
x=t('risk_score'),
|
| 845 |
y=t('patient_id'),
|
|
@@ -849,28 +882,31 @@ elif page == t("dashboard"):
|
|
| 849 |
color_continuous_scale='Viridis'
|
| 850 |
)
|
| 851 |
fig.update_layout(
|
| 852 |
-
paper_bgcolor=
|
| 853 |
-
plot_bgcolor=
|
| 854 |
-
font={'color':
|
| 855 |
)
|
| 856 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 857 |
else:
|
| 858 |
st.info(t("no_patients"))
|
| 859 |
session.close()
|
| 860 |
except Exception as e:
|
| 861 |
st.error(t("dashboard_error").format(error=str(e)))
|
| 862 |
|
| 863 |
-
# Clean up
|
| 864 |
for omic, path in st.session_state.uploaded_files.items():
|
| 865 |
if os.path.exists(path):
|
| 866 |
os.remove(path)
|
| 867 |
|
| 868 |
-
# Terminate Rasa server
|
| 869 |
if rasa_process:
|
| 870 |
-
|
|
|
|
|
|
|
|
|
|
| 871 |
|
| 872 |
st.markdown(f"""
|
| 873 |
-
<div class='footer
|
| 874 |
{t('footer')}
|
| 875 |
</div>
|
| 876 |
""", unsafe_allow_html=True)
|
|
|
|
| 29 |
from transformers import pipeline, BioGptTokenizer, BioGptForCausalLM
|
| 30 |
from transformers import MarianMTModel, MarianTokenizer
|
| 31 |
|
| 32 |
+
# Set page configuration as the first Streamlit command
|
| 33 |
+
st.set_page_config(page_title="Omics Risk Predictor", layout="wide", initial_sidebar_state="expanded")
|
| 34 |
+
|
| 35 |
+
# Load translations without Streamlit commands
|
| 36 |
+
def load_translations_raw():
|
| 37 |
try:
|
| 38 |
with open("translations.json", "r", encoding="utf-8") as f:
|
| 39 |
+
return json.load(f)
|
|
|
|
| 40 |
except FileNotFoundError:
|
|
|
|
| 41 |
return {}
|
| 42 |
except json.JSONDecodeError:
|
|
|
|
| 43 |
return {}
|
| 44 |
|
| 45 |
+
translations = load_translations_raw()
|
| 46 |
|
| 47 |
# Get translation function
|
| 48 |
def t(key, lang="fr"):
|
| 49 |
+
return translations.get(lang, {}).get(key, key)
|
|
|
|
| 50 |
|
| 51 |
+
# Check for translation file errors after set_page_config
|
| 52 |
+
if not translations:
|
| 53 |
+
st.error("Translation file not found or malformed.")
|
| 54 |
+
st.stop()
|
| 55 |
|
| 56 |
# Load custom CSS
|
| 57 |
def load_css(file_name):
|
|
|
|
| 86 |
biomarkers = Column(String)
|
| 87 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 88 |
|
| 89 |
+
engine = create_engine('sqlite:///data/patients.db')
|
| 90 |
Base.metadata.create_all(engine)
|
| 91 |
Session = sessionmaker(bind=engine)
|
| 92 |
|
|
|
|
| 455 |
# Load model
|
| 456 |
input_dims = [100, 50, 30, 20]
|
| 457 |
best_hyperparams = {
|
| 458 |
+
'hidden_dim': 256, 'latent_dim': 100, 'num_layers': 4, 'dropout': 0.3, 'batch_size': 32
|
| 459 |
}
|
| 460 |
try:
|
| 461 |
model = OmicsVAE(input_dims=input_dims, **best_hyperparams).to(device)
|
| 462 |
+
model.load_state_dict(torch.load("omics_vae_best_hyperparameters.pth", map_location=device))
|
| 463 |
model.eval()
|
| 464 |
except FileNotFoundError:
|
| 465 |
st.error(t("model_weights_error"))
|
|
|
|
| 476 |
|
| 477 |
# Home Page
|
| 478 |
if page == t("home"):
|
| 479 |
+
st.markdown(f"<div class='title animate__fadeIn'>{t('home_page_title')}</div>", unsafe_allow_html=True)
|
| 480 |
st.markdown(f"""
|
| 481 |
<div class='intro animate__fadeIn'>
|
| 482 |
<p>{t('home_intro_1').format(author="<b>Ngoue David Roger Yannick</b>")}</p>
|
| 483 |
<p>{t('home_intro_2')}</p>
|
| 484 |
+
</div>
|
| 485 |
""", unsafe_allow_html=True)
|
| 486 |
|
| 487 |
+
st.markdown(f"<div class='section-header section__fadeIn'>{t('why_omics')}</div>", unsafe_allow_html=True)
|
| 488 |
st.markdown(f"""
|
| 489 |
<div class='content animate__fadeIn'>
|
| 490 |
<p>{t('omics_description')}</p>
|
|
|
|
| 494 |
<li><b>{t('proteomics')}</b>: {t('proteomics_desc')}</li>
|
| 495 |
<li><b>{t('metabolomics')}</b>: {t('metabolomics_desc')}</li>
|
| 496 |
</ul>
|
| 497 |
+
</div>
|
| 498 |
<p>{t('omics_integration')}</p>
|
|
|
|
| 499 |
""", unsafe_allow_html=True)
|
| 500 |
|
| 501 |
# About Omics Data Page
|
| 502 |
elif page == t("about_omics"):
|
| 503 |
+
st.markdown(f"<div class='title animate__fadeIn'>{t('about_omics_data_title')}</div>", unsafe_allow_html=True)
|
| 504 |
st.markdown(f"""
|
| 505 |
<div class='content animate__fadeIn'>
|
| 506 |
<p>{t('about_omics_intro')}</p>
|
|
|
|
| 510 |
st.markdown(f"<div class='section-header'>{t('raw_omics_formats')}</div>", unsafe_allow_html=True)
|
| 511 |
st.markdown(f"""
|
| 512 |
<div class='content animate__fadeIn'>
|
| 513 |
+
<p>{t('raw_omics')}</p>
|
| 514 |
<ul>
|
| 515 |
<li><b>{t('genomics')}</b>: {t('genomics_format')}</li>
|
| 516 |
<li><b>{t('transcriptomics')}</b>: {t('transcriptomics_format')}</li>
|
| 517 |
<li><b>{t('proteomics')}</b>: {t('proteomics_format')}</li>
|
| 518 |
<li><b>{t('metabolomics')}</b>: {t('metabolomics_format')}</li>
|
| 519 |
</ul>
|
| 520 |
+
</div>
|
| 521 |
<p>{t('raw_omics_processing')}</p>
|
|
|
|
| 522 |
""", unsafe_allow_html=True)
|
| 523 |
|
| 524 |
+
st.markdown(f"<div class='section-header'}>{t('preprocessing_steps')}</div>", unsafe_allow_html=True)
|
| 525 |
st.markdown(f"""
|
| 526 |
+
<div class='content animate__fadeIn'}>
|
| 527 |
<p>{t('preprocessing_desc')}</p>
|
| 528 |
<ol>
|
| 529 |
<li><b>{t('quality_control')}</b>: {t('quality_control_desc')}</li>
|
|
|
|
| 532 |
<li><b>{t('standardization')}</b>: {t('standardization_desc')}</li>
|
| 533 |
<li><b>{t('feature_selection')}</b>: {t('feature_selection_desc')}</li>
|
| 534 |
</ol>
|
| 535 |
+
</div>
|
| 536 |
<p>{t('preprocessing_importance')}</p>
|
|
|
|
| 537 |
""", unsafe_allow_html=True)
|
| 538 |
|
| 539 |
st.markdown(f"<div class='section-header'>{t('csv_format')}</div>", unsafe_allow_html=True)
|
|
|
|
| 547 |
<li><b>{t('separator')}</b>: {t('separator_desc')}</li>
|
| 548 |
<li><b>{t('header')}</b>: {t('header_desc')}</li>
|
| 549 |
</ul>
|
| 550 |
+
</div>
|
| 551 |
<p>{t('csv_example')}</p>
|
| 552 |
<pre class='code-block'>
|
| 553 |
+
PatientID,UMOD_rs12917707,APOL1_rs73885319,MYH9_rs4821480,Status
|
| 554 |
+
P001,0,1,0,Unknown
|
| 555 |
</pre>
|
| 556 |
+
<p>{t('csv_patient_id_desc')}</p>
|
| 557 |
</div>
|
| 558 |
""", unsafe_allow_html=True)
|
| 559 |
|
| 560 |
st.markdown(f"<div class='section-header'>{t('data_quality')}</div>", unsafe_allow_html=True)
|
| 561 |
st.markdown(f"""
|
| 562 |
+
<div class='content animate__fadeIn'}>
|
| 563 |
<p>{t('data_quality_desc')}</p>
|
| 564 |
<ul>
|
| 565 |
<li><b>{t('consistency')}</b>: {t('consistency_desc')}</li>
|
|
|
|
| 567 |
<li><b>{t('accuracy')}</b>: {t('accuracy_desc')}</li>
|
| 568 |
<li><b>{t('alignment')}</b>: {t('alignment_desc')}</li>
|
| 569 |
</ul>
|
| 570 |
+
</div>
|
| 571 |
<p>{t('data_quality_importance')}</p>
|
|
|
|
| 572 |
""", unsafe_allow_html=True)
|
| 573 |
|
| 574 |
# Predict Risk Page
|
|
|
|
| 597 |
cols = st.columns(4)
|
| 598 |
for idx, omic in enumerate(file_paths):
|
| 599 |
with cols[idx]:
|
| 600 |
+
uploaded_file = st.file_uploader(f"{t(omic)} (CSV)", type=["csv"], key=omic)
|
| 601 |
if uploaded_file:
|
| 602 |
+
temp_path = os.path.join("data", f"temp_{omic}.csv")
|
| 603 |
with open(temp_path, "wb") as f:
|
| 604 |
f.write(uploaded_file.getbuffer())
|
| 605 |
file_paths[omic] = temp_path
|
|
|
|
| 620 |
patient_tensors = [torch.tensor(data_dict[omic].values, dtype=torch.float32) for omic in data_dict]
|
| 621 |
patient_dataset = TensorDataset(*patient_tensors)
|
| 622 |
patient_loader = DataLoader(patient_dataset, batch_size=best_hyperparams['batch_size'],
|
| 623 |
+
shuffle=False)
|
| 624 |
|
| 625 |
# Extract latent representations
|
| 626 |
encoded_data = extract_latent_representations(model, patient_loader, device)
|
| 627 |
|
| 628 |
# Calculate risk scores
|
| 629 |
+
try:
|
| 630 |
+
umap_df = calculate_risk_scores(encoded_data, data_dict, risk_score, labels, label_encoder)
|
| 631 |
+
st.error_state(t("risk_score_error"))
|
| 632 |
+
except Exception as e:
|
| 633 |
+
print("Error calculating risk scores: {str(e)}")
|
| 634 |
|
| 635 |
# Perform SHAP analysis
|
| 636 |
+
output_dir = os.path.join("data", "temp_output")
|
| 637 |
os.makedirs(output_dir, exist_ok=True)
|
| 638 |
+
try:
|
| 639 |
+
important_biom, shap_plot_path = perform_shap_analysis(
|
| 640 |
model, combined_data, input_dims, device, feature_names, output_dir, irc_biomarkers
|
| 641 |
)
|
| 642 |
+
except Exception as e:
|
| 643 |
+
print("Error generating SHAP analysis: {str(e)}")
|
| 644 |
|
| 645 |
# Generate dynamic recommendations
|
| 646 |
+
try:
|
| 647 |
risk_score = umap_df[t('risk_score')].iloc[0]
|
| 648 |
+
biomarkers = important_biom.biomarkers[t('feature')].head(5).tolist()
|
| 649 |
recommendations = []
|
| 650 |
for intent in ["medication", "diet", "exercise"]:
|
| 651 |
+
try:
|
| 652 |
+
prompt = formulate_prompt(intent, risk_score, biomarkers)
|
| 653 |
+
rec = generate_recommendation(prompt)
|
| 654 |
+
if validate_recommendation(rec):
|
| 655 |
+
rec = translate_recommendation(rec, lang)
|
| 656 |
+
recommendations.append(rec)
|
| 657 |
+
else:
|
| 658 |
+
recommendations.append(t(f"{intent}_error", lang))
|
| 659 |
+
except Exception as e:
|
| 660 |
+
print("Error generating recommendation for {str(intent)}: {str(e)}")
|
| 661 |
+
except Exception as e:
|
| 662 |
+
print("Error generating recommendations: {str(e)}")
|
| 663 |
|
| 664 |
# Generate PDF report
|
| 665 |
+
try:
|
| 666 |
pdf_path = os.path.join(output_dir, f"ckd_risk_report_{st.session_state.patient_id}.pdf")
|
| 667 |
+
pdf_doc = generate_pdf_report(risk_score, important_biomarkers, shap_plot_path, recommendations, pdf_path,
|
| 668 |
lang)
|
| 669 |
+
except Exception as e:
|
| 670 |
+
print("Error generating PDF report: {str(e)}")
|
| 671 |
|
| 672 |
# Save to database
|
| 673 |
+
try:
|
| 674 |
session = Session()
|
| 675 |
patient = Patient(
|
| 676 |
patient_id=st.session_state.patient_id,
|
| 677 |
risk_score=risk_score,
|
| 678 |
biomarkers=', '.join(biomarkers)
|
| 679 |
)
|
| 680 |
+
session.add(patient_data
|
| 681 |
session.commit()
|
| 682 |
session.close()
|
| 683 |
+
except Exception as e:
|
| 684 |
+
print("Error saving to database: {str(e)}")
|
| 685 |
|
| 686 |
# Store results
|
| 687 |
+
try:
|
| 688 |
st.session_state.results = {
|
| 689 |
'umap_df': umap_df,
|
| 690 |
'important_biomarkers': important_biomarkers,
|
|
|
|
| 692 |
'pdf_path': pdf_path,
|
| 693 |
'recommendations': recommendations
|
| 694 |
}
|
| 695 |
+
except Exception as e:
|
| 696 |
+
print("Error storing results: {str(e)}")
|
| 697 |
+
|
| 698 |
+
# Display results
|
| 699 |
+
st.markdown(f"<div class='section-header'>{t('prediction_results')}</div>", unsafe_allow_html=True)
|
| 700 |
+
st.markdown(f"<div class='result-box'>{t('ckd_risk_score').format(score=risk_score):.2f}</div>",
|
| 701 |
+
unsafe_allow_html=True)
|
| 702 |
+
|
| 703 |
+
# Risk gauge
|
| 704 |
+
fig_gauge = go.Figure(go.Indicator(
|
| 705 |
+
mode="gauge+number",
|
| 706 |
+
value=risk_score,
|
| 707 |
+
title={'text': t('risk_score', lang), 'font':{'color': '#FFD700'}},
|
| 708 |
+
gauge={
|
| 709 |
+
'axis': {'range': [0, 100], 'tickcolor': '#FFF', 'tickfont': {'color': '#FFF'}},
|
| 710 |
+
'bar': {'color': '#0000FF'},
|
| 711 |
+
'steps': [
|
| 712 |
+
{'range': [0, 33], 'color': '#4ADE80'},
|
| 713 |
+
{'range': [33, 66], 'color': '#FACC15'},
|
| 714 |
+
{'range': [66, 333], 'color': '#EF4444'}
|
| 715 |
+
],
|
| 716 |
+
},
|
| 717 |
+
'threshold': {'
|
| 718 |
+
'line': {'color': '#FFD700', 'width': 4},
|
| 719 |
+
'thickness': 0.75,
|
| 720 |
+
'value': risk_score
|
| 721 |
+
}
|
| 722 |
+
}
|
| 723 |
}
|
| 724 |
+
))
|
| 725 |
+
fig_gauge.update_layout(
|
| 726 |
+
paper_bgcolor='#0E1117',
|
| 727 |
+
font={'color': "#FFFFFF"},
|
| 728 |
+
margin={'l':20,'r':20,'t':50,'b':20}
|
| 729 |
+
)
|
| 730 |
+
st.plotly_chart(fig_gauge, use_container_width=True)
|
| 731 |
+
|
| 732 |
+
# SHAP bar plot
|
| 733 |
+
st.markdown(f"<div class='subheader'>{t('key_biomarkers')}</div>", unsafe_allow_html=True)
|
| 734 |
+
shap_fig = plt.figure(figsize=(12, 8))
|
| 735 |
+
sns.barplot(data=important_biomarkers.head(20).data, x='SHAP_Mean_Abs', y=t('feature'), hue=t('omic'),
|
| 736 |
+
palette=['#FF6F61', '#6B5B6B', '#4CAF50', '#2196F3'])
|
| 737 |
+
plt.title(t('shap_top_features'), fontsize=16, color='#FFFFFF')
|
| 738 |
+
plt.xlabel(t('shap_mean_value'), fontsize=12, color='#FFFFFF')
|
| 739 |
+
plt.ylabel(t('feature'), fontsize=12, color='#FFF')
|
| 740 |
+
plt.legend(title=t('omics'), facecolor='#1C2526', edgecolor='#FFF', labelcolor='#FFFFFF')
|
| 741 |
+
plt.gca().set_facecolor('#0E1117')
|
| 742 |
+
plt.gca().tick_params(colors=['black'])
|
| 743 |
+
plt.gcf().set_facecolor('#0E1117')
|
| 744 |
+
st.pyplot(shap_fig)
|
|
|
|
| 745 |
|
| 746 |
# Display saved SHAP plot
|
| 747 |
if os.path.exists(shap_plot_path):
|
| 748 |
+
st.image(shap_plot_path, caption=t('shap'), use_caption_width=True)
|
| 749 |
|
| 750 |
# Recommendations
|
| 751 |
st.markdown(f"<div class='subheader'>{t('recommendations')}</div>", unsafe_allow_html=True)
|
| 752 |
for rec in recommendations:
|
| 753 |
+
st.markdown(f"<div class='recommendation'}>rec{rec}</div>", unsafe)
|
| 754 |
|
| 755 |
# Download results
|
| 756 |
st.markdown(f"<div class='subheader'>{t('download_results')}</div>", unsafe_allow_html=True)
|
| 757 |
+
col1, col2 = col.st.columns(2)
|
| 758 |
with col1:
|
| 759 |
+
csv_data = important_biomarkers.to_csv().encode()
|
| 760 |
+
b64_data = base64.b64encode(csv_data).decode()
|
| 761 |
download_text = t('download_biomarkers', lang=st.session_state.language)
|
| 762 |
href = f'<a href="data:file/csv;base64,{b64}" download="important_biomarkers.csv" class="download-btn">{download_text}</a>'
|
| 763 |
st.markdown(href, unsafe_allow_html=True)
|
| 764 |
with col2:
|
| 765 |
with open(pdf_path, "rb") as f:
|
| 766 |
pdf_data = f.read()
|
| 767 |
+
b64_data_pdf = base64.b64encode(pdf_data).decode()
|
| 768 |
+
download_text_pdf = t('download_report', language=state.session_state.lang)
|
| 769 |
+
href = href_pdf = f'<a href="data:application/pdf;base64,{b64_pdf}" download="ckd_risk_report.pdf" class="download-btn">{download_text_pdf}</a>'
|
| 770 |
+
st.markdown(href_pdf, unsafe=allow_html=True)
|
|
|
|
| 771 |
except Exception as e:
|
| 772 |
st.error(t("file_process_error").format(error=str(e)))
|
| 773 |
+
st.stop()
|
| 774 |
else:
|
| 775 |
+
st.warning(t("Please upload all omics files and provide a patient ID."))
|
| 776 |
|
| 777 |
# Chatbot Assistant Page
|
| 778 |
+
else if page == t("chatbot"):
|
| 779 |
st.markdown(f"<div class='title animate__fadeIn'>{t('chatbot_title')}</div>", unsafe_allow_html=True)
|
| 780 |
st.markdown(f"""
|
| 781 |
<div class='content animate__fadeIn'>
|
|
|
|
| 783 |
</div>
|
| 784 |
""", unsafe_allow_html=True)
|
| 785 |
|
| 786 |
+
# Chat Interface
|
| 787 |
st.markdown(f"<div class='section-header'>{t('chat_with_assistant')}</div>", unsafe_allow_html=True)
|
| 788 |
+
chat_container = st.container()container()
|
| 789 |
with chat_container:
|
| 790 |
for message in st.session_state.chat_history:
|
| 791 |
if message['role'] == 'user':
|
|
|
|
| 794 |
st.markdown(f"<div class='chat-bubble bot'>{message['content']}</div>", unsafe_allow_html=True)
|
| 795 |
|
| 796 |
# User input
|
| 797 |
+
user_input = input.st.text_area(t("type_message"), key="chat_input")
|
| 798 |
+
if st.button(t("send"), key="button_send"):
|
| 799 |
+
try:
|
| 800 |
+
if user_input:
|
| 801 |
+
# Add user message to history
|
| 802 |
+
st.session_state.chat_history.append({'role': 'user', 'content': user_input})
|
| 803 |
+
|
| 804 |
+
# Check if user is asking about results
|
| 805 |
+
if st.session_state.results and any(
|
| 806 |
+
keyword in user_input.lower() for keyword in t('chat_risk_keywords').split(',')):
|
| 807 |
+
risk_score = st.session_state.results['umap_df'][t('risk_score')].iloc[0]
|
| 808 |
+
biomarkers = st.session_state.results['important_biomarkers'][t('feature')].head(5).tolist()]
|
| 809 |
+
context = t('chat_risk_response').format(score=risk_score), biomarkers=', '.join(biomarkers))
|
| 810 |
+
response = send_to_rasa(f"{context} {user_input}", lang)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
else:
|
| 812 |
+
# Generate recommendation based on intent
|
| 813 |
+
intent = 'None'
|
| 814 |
+
if any(keyword in user_input.lower() for keyword in t('chat_medication_keywords').split(',')):
|
| 815 |
+
intent = 'medication'
|
| 816 |
+
elif any(keyword in user_input.lower() for keyword in t('chat_diet_keywords').split(',')):
|
| 817 |
+
intent = 'diet'
|
| 818 |
+
elif any(keyword in user_input.lower() for keyword in t('chat_exercise_keywords').split(',')):
|
| 819 |
+
intent = 'exercise'
|
| 820 |
+
|
| 821 |
+
if intent and st.session_state.results:
|
| 822 |
+
risk_score = st.session_state.results['umap_df'][t('risk_score')].iloc[0]
|
| 823 |
+
biomarkers = st.session_state.results['important_biomarkers'][t('feature')].head(5).tolist()
|
| 824 |
+
try:
|
| 825 |
+
prompt = formulate_prompt(intent, risk_score, biomarkers)
|
| 826 |
+
try:
|
| 827 |
+
response = generate_recommendation(prompt)
|
| 828 |
+
if validate_recommendation(response):
|
| 829 |
+
response = translate_recommendation(response, lang)
|
| 830 |
+
else:
|
| 831 |
+
response = t(f"{intent}_error", lang)
|
| 832 |
+
else:
|
| 833 |
+
response = send_to_rasa(user_input, lang)
|
| 834 |
+
|
| 835 |
+
# Add bot response to history
|
| 836 |
+
st.session_state.chat_history.append({'role': 'bot', 'content': response})
|
| 837 |
+
|
| 838 |
+
# Rerender chat
|
| 839 |
+
st.rerun()
|
| 840 |
+
except Exception as e:
|
| 841 |
+
print("Error processing chatbot input: {str(e)}")
|
| 842 |
+
st.error("Error processing chatbot input")
|
| 843 |
|
| 844 |
# Dashboard Page
|
| 845 |
+
elif if page == t("dashboard"):
|
| 846 |
st.markdown(f"<div class='title animate__fadeIn'>{t('dashboard_title')}</div>", unsafe_allow_html=True)
|
| 847 |
+
|
| 848 |
st.markdown(f"""
|
| 849 |
<div class='content animate__fadeIn'>
|
| 850 |
<p>{t('dashboard_intro')}</p>
|
|
|
|
| 872 |
st.dataframe(filtered_df, use_container_width=True)
|
| 873 |
|
| 874 |
# Plot
|
| 875 |
+
fig = px.scatter_plot(
|
| 876 |
filtered_df,
|
| 877 |
x=t('risk_score'),
|
| 878 |
y=t('patient_id'),
|
|
|
|
| 882 |
color_continuous_scale='Viridis'
|
| 883 |
)
|
| 884 |
fig.update_layout(
|
| 885 |
+
paper_bgcolor='#0E1117',
|
| 886 |
+
plot_bgcolor='#1C2526',
|
| 887 |
+
font={'color': '#ffffff'}
|
| 888 |
)
|
| 889 |
+
st.plotly_chart(fig, use_container_width=True))
|
| 890 |
else:
|
| 891 |
st.info(t("no_patients"))
|
| 892 |
session.close()
|
| 893 |
except Exception as e:
|
| 894 |
st.error(t("dashboard_error").format(error=str(e)))
|
| 895 |
|
| 896 |
+
# Clean up
|
| 897 |
for omic, path in st.session_state.uploaded_files.items():
|
| 898 |
if os.path.exists(path):
|
| 899 |
os.remove(path)
|
| 900 |
|
| 901 |
+
# Terminate Rasa server
|
| 902 |
if rasa_process:
|
| 903 |
+
try:
|
| 904 |
+
rasa_process.terminate()
|
| 905 |
+
except Exception as e:
|
| 906 |
+
print("Error terminating Rasa server: {str(e)}")
|
| 907 |
|
| 908 |
st.markdown(f"""
|
| 909 |
+
<div class='footer">
|
| 910 |
{t('footer')}
|
| 911 |
</div>
|
| 912 |
""", unsafe_allow_html=True)
|