Spaces:
Sleeping
Sleeping
File size: 7,635 Bytes
ed5d5d7 | 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 | """
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"
)
|