HumanPlusX / src /pages /protocol.py
FajarHidaa's picture
Upload 3 files
ed5d5d7 verified
Raw History Blame Contribute Delete
7.64 kB
"""
Human+ β€” Page 3: Protocol Result
Refactored: protocol sections pakai st.expander native.
"""
import streamlit as st
from ui.components import (
render_section_label,
render_protocol_section,
render_highlight_quote,
render_success_banner,
render_error_state,
render_loading_steps,
render_progress_bar,
PROTOCOL_STEPS,
)
def render_protocol_page():
if not st.session_state.get("extracted_biomarkers"):
render_error_state(
title="Belum ada hasil analisis",
message="Silakan kembali dan upload hasil lab Anda terlebih dahulu.",
)
if st.button("← Kembali ke Upload", use_container_width=True):
st.session_state.step = 1
st.rerun()
return
if st.session_state.get("generated_protocol"):
_render_protocol_results()
return
_run_protocol_generation()
# ── Generator ────────────────────────────────────────────────────────
def _run_protocol_generation():
render_section_label("MENYUSUN PROTOCOL ANDA", "🧬")
status_ph = st.empty()
progress_ph = st.empty()
steps_ph = st.empty()
def _update(step_idx: int, msg: str):
with status_ph:
st.caption(f"🧬 {msg}...")
with progress_ph:
render_progress_bar((step_idx + 0.5) / len(PROTOCOL_STEPS))
with steps_ph:
render_loading_steps(PROTOCOL_STEPS, step_idx)
# Step 0 β€” load vector store
_update(0, PROTOCOL_STEPS[0])
try:
from core.rag_engine import get_vector_store
vector_store = get_vector_store()
except Exception as e:
_render_gen_error(str(e), steps_ph, status_ph, progress_ph)
return
# Step 1-3 β€” generate
_update(1, PROTOCOL_STEPS[1])
_update(2, PROTOCOL_STEPS[2])
try:
from core.protocol_generator import generate_protocol
protocol = generate_protocol(
biomarkers=st.session_state.extracted_biomarkers,
vector_store=vector_store,
)
except Exception as e:
_render_gen_error(str(e), steps_ph, status_ph, progress_ph)
return
_update(3, PROTOCOL_STEPS[3])
import time; time.sleep(0.3)
st.session_state.generated_protocol = protocol
with progress_ph:
render_progress_bar(1.0)
with status_ph:
st.caption("βœ… Protocol siap")
import time; time.sleep(0.4)
st.rerun()
def _render_gen_error(msg, steps_ph, status_ph, progress_ph):
for ph in [steps_ph, status_ph, progress_ph]:
ph.empty()
render_error_state(title="Gagal Membuat Protocol", message=msg, show_contact=True)
col1, col2 = st.columns(2)
with col1:
if st.button("← Kembali ke Hasil Lab", use_container_width=True):
st.session_state.step = 2
st.rerun()
with col2:
if st.button("πŸ”„ Coba Lagi", type="primary", use_container_width=True):
st.rerun()
# ── Results ──────────────────────────────────────────────────────────
def _render_protocol_results():
protocol = st.session_state.generated_protocol
render_success_banner(
title="Protocol Personal Siap ✨",
subtitle="Hyperpersonalized berdasarkan biomarker Anda Β· Human+ Bali",
)
# Summary block β€” native info box
if protocol.get("summary"):
st.info(f"πŸ” **RINGKASAN TEMUAN UTAMA**\n\n{protocol['summary']}")
render_section_label("PROTOCOL PERSONAL ANDA", "πŸ“‹")
render_protocol_section("iv_drip", protocol.get("iv_drip", ""))
render_protocol_section("supplements", protocol.get("supplements", ""))
render_protocol_section("lifestyle", protocol.get("lifestyle", ""))
render_protocol_section("retest", protocol.get("retest", ""))
render_highlight_quote(
"Tubuh Anda bukan sekadar 'normal' β€” Anda layak mendapatkan yang optimal. "
"Protocol ini adalah langkah pertama dari perjalanan From Injured β†’ Optimized."
)
# ── Download ──────────────────────────────────────────────────
render_section_label("SIMPAN & BAGIKAN", "πŸ’Ύ")
col_pdf, col_nav = st.columns(2)
with col_pdf:
try:
from core.report_generator import generate_pdf_report
pdf_bytes = generate_pdf_report(
biomarkers=st.session_state.get("extracted_biomarkers", []),
protocol=protocol,
)
st.download_button(
label="πŸ“„ Download PDF",
data=pdf_bytes,
file_name="Human+_Protocol_Report.pdf",
mime="application/pdf",
use_container_width=True,
)
except Exception:
# Fallback txt
st.download_button(
label="πŸ“₯ Download Report (.txt)",
data=_format_txt(protocol),
file_name="Human+_Protocol_Report.txt",
mime="text/plain",
use_container_width=True,
)
with col_nav:
if st.button("← Lihat Hasil Lab", use_container_width=True):
st.session_state.generated_protocol = None
st.session_state.step = 2
st.rerun()
# ── Book session CTA β€” native st.link_button ──────────────────
st.write("")
st.markdown("---")
st.markdown("### 🌴 Ready untuk level berikutnya?")
st.write("Book sesi konsultasi dengan tim Human+ Bali untuk protocol yang direview langsung oleh Benjamin White.")
st.link_button(
"Book di Instagram β†’",
"https://instagram.com/bennywhitethatsright",
use_container_width=True,
type="primary",
)
st.write("")
if st.button("πŸ”„ Analisis Lab Baru", use_container_width=True):
for k in ["uploaded_pdf_bytes", "extracted_biomarkers", "generated_protocol", "pdf_filename"]:
st.session_state[k] = None
st.session_state.step = 1
st.rerun()
def _format_txt(protocol: dict) -> str:
prios = ", ".join(protocol.get("key_priorities", [])) or "-"
return (
"╔══════════════════════════════════════════╗\n"
"β•‘ HUMAN+ LAB-TO-PROTOCOL REPORT β•‘\n"
"β•‘ From Injured β†’ Optimized β•‘\n"
"β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n\n"
f"PRIORITAS UTAMA: {prios}\n\n"
f"πŸ“Œ RINGKASAN\n{'-'*40}\n{protocol.get('summary','')}\n\n"
f"πŸ’‰ IV DRIP\n{'-'*40}\n{protocol.get('iv_drip','')}\n\n"
f"πŸ’Š SUPLEMEN\n{'-'*40}\n{protocol.get('supplements','')}\n\n"
f"🧊 LIFESTYLE\n{'-'*40}\n{protocol.get('lifestyle','')}\n\n"
f"πŸ“… RETEST\n{'-'*40}\n{protocol.get('retest','')}\n\n"
"══════════════════════════════════════════\n"
"Human+ Bali Β· @bennywhitethatsright\n"
"Bukan pengganti konsultasi medis.\n"
"══════════════════════════════════════════\n"
)