Update src/streamlit_app.py
Browse files- src/streamlit_app.py +7 -67
src/streamlit_app.py
CHANGED
@@ -18,7 +18,6 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, Tabl
|
|
18 |
from reportlab.lib import colors
|
19 |
import tempfile
|
20 |
from sendgrid_service import SendGridService
|
21 |
-
from whatsapp_service import WhatsAppService
|
22 |
|
23 |
|
24 |
# Set page config with dark theme
|
@@ -381,8 +380,6 @@ if 'pdf_data' not in st.session_state:
|
|
381 |
st.session_state.pdf_data = None
|
382 |
if 'show_email_form' not in st.session_state:
|
383 |
st.session_state.show_email_form = False
|
384 |
-
if 'whatsapp_step' not in st.session_state:
|
385 |
-
st.session_state.whatsapp_step = False
|
386 |
|
387 |
# Helper function to check if explanation has meaningful content
|
388 |
def has_meaningful_content(text):
|
@@ -723,8 +720,7 @@ def generate_and_download_report():
|
|
723 |
if 'report_step' not in st.session_state:
|
724 |
st.session_state.report_step = 1
|
725 |
st.session_state.patient_info = {"name": "", "age": "", "gender": ""}
|
726 |
-
st.session_state.pdf_data = None # Store PDF data for email
|
727 |
-
st.session_state.whatsapp_step = False # Flag to track WhatsApp form display
|
728 |
|
729 |
# Step 1: Collect patient name
|
730 |
if st.session_state.report_step == 1:
|
@@ -768,16 +764,16 @@ def generate_and_download_report():
|
|
768 |
|
769 |
# Generate PDF
|
770 |
pdf_bytes = build_medical_report(report_json)
|
771 |
-
st.session_state.pdf_data = pdf_bytes # Store PDF data
|
772 |
|
773 |
# Display success message
|
774 |
st.success("Your medical report is ready!")
|
775 |
|
776 |
-
# Create columns for download
|
777 |
-
col1, col2
|
778 |
|
779 |
with col1:
|
780 |
-
#
|
781 |
st.download_button(
|
782 |
label="π₯ Download Report",
|
783 |
data=pdf_bytes,
|
@@ -787,17 +783,9 @@ def generate_and_download_report():
|
|
787 |
)
|
788 |
|
789 |
with col2:
|
790 |
-
# Email button
|
791 |
if st.button("π§ Email Report"):
|
792 |
st.session_state.show_email_form = True
|
793 |
-
st.session_state.whatsapp_step = False
|
794 |
-
st.rerun()
|
795 |
-
|
796 |
-
with col3:
|
797 |
-
# WhatsApp button
|
798 |
-
if st.button("π± WhatsApp Report"):
|
799 |
-
st.session_state.whatsapp_step = True
|
800 |
-
st.session_state.show_email_form = False
|
801 |
st.rerun()
|
802 |
|
803 |
# Show email form if the button was clicked
|
@@ -839,62 +827,14 @@ def generate_and_download_report():
|
|
839 |
st.error(message)
|
840 |
|
841 |
# Cancel button to hide email form
|
842 |
-
if st.button("Cancel
|
843 |
st.session_state.show_email_form = False
|
844 |
st.rerun()
|
845 |
-
|
846 |
-
# Show WhatsApp form if that button was clicked
|
847 |
-
if st.session_state.get('whatsapp_step', False):
|
848 |
-
st.subheader("Send Report via WhatsApp")
|
849 |
-
|
850 |
-
# Check Twilio configuration
|
851 |
-
twilio_sid = os.getenv('TWILIO_ACCOUNT_SID')
|
852 |
-
twilio_number = os.getenv('TWILIO_WHATSAPP_NUMBER')
|
853 |
-
if not all([twilio_sid, twilio_number]):
|
854 |
-
st.warning("WhatsApp service not configured. Please contact administrator.")
|
855 |
-
|
856 |
-
# Phone number form with instructions
|
857 |
-
st.markdown("Enter recipient's phone number with country code (e.g., +1234567890)")
|
858 |
-
phone = st.text_input("WhatsApp Number:")
|
859 |
-
|
860 |
-
# WhatsApp send button
|
861 |
-
if st.button("Send to WhatsApp"):
|
862 |
-
if not phone:
|
863 |
-
st.error("Please enter a phone number.")
|
864 |
-
else:
|
865 |
-
# Create service and validate/send
|
866 |
-
whatsapp = WhatsAppService()
|
867 |
-
is_valid, _ = whatsapp.validate_phone(phone)
|
868 |
-
|
869 |
-
if not is_valid:
|
870 |
-
st.error("Please enter a valid phone number with country code (e.g., +1234567890)")
|
871 |
-
else:
|
872 |
-
# Send via WhatsApp
|
873 |
-
with st.spinner("Sending to WhatsApp... This may take a few seconds."):
|
874 |
-
success, message = whatsapp.send_report(
|
875 |
-
phone,
|
876 |
-
st.session_state.pdf_data,
|
877 |
-
st.session_state.patient_info["name"]
|
878 |
-
)
|
879 |
-
|
880 |
-
if success:
|
881 |
-
st.success(message)
|
882 |
-
# Hide the form after successful send
|
883 |
-
st.session_state.whatsapp_step = False
|
884 |
-
st.rerun()
|
885 |
-
else:
|
886 |
-
st.error(message)
|
887 |
-
|
888 |
-
# Cancel button to hide WhatsApp form
|
889 |
-
if st.button("Cancel WhatsApp"):
|
890 |
-
st.session_state.whatsapp_step = False
|
891 |
-
st.rerun()
|
892 |
|
893 |
# Button to start over
|
894 |
if st.button("Generate Another Report"):
|
895 |
st.session_state.report_step = 1
|
896 |
st.session_state.show_email_form = False
|
897 |
-
st.session_state.whatsapp_step = False
|
898 |
st.rerun()
|
899 |
|
900 |
# Put the toggle in the sidebar - this is the most reliable approach
|
|
|
18 |
from reportlab.lib import colors
|
19 |
import tempfile
|
20 |
from sendgrid_service import SendGridService
|
|
|
21 |
|
22 |
|
23 |
# Set page config with dark theme
|
|
|
380 |
st.session_state.pdf_data = None
|
381 |
if 'show_email_form' not in st.session_state:
|
382 |
st.session_state.show_email_form = False
|
|
|
|
|
383 |
|
384 |
# Helper function to check if explanation has meaningful content
|
385 |
def has_meaningful_content(text):
|
|
|
720 |
if 'report_step' not in st.session_state:
|
721 |
st.session_state.report_step = 1
|
722 |
st.session_state.patient_info = {"name": "", "age": "", "gender": ""}
|
723 |
+
st.session_state.pdf_data = None # Store PDF data for email
|
|
|
724 |
|
725 |
# Step 1: Collect patient name
|
726 |
if st.session_state.report_step == 1:
|
|
|
764 |
|
765 |
# Generate PDF
|
766 |
pdf_bytes = build_medical_report(report_json)
|
767 |
+
st.session_state.pdf_data = pdf_bytes # Store PDF data for email
|
768 |
|
769 |
# Display success message
|
770 |
st.success("Your medical report is ready!")
|
771 |
|
772 |
+
# Create two columns for the download and email buttons
|
773 |
+
col1, col2 = st.columns(2)
|
774 |
|
775 |
with col1:
|
776 |
+
# Offer download
|
777 |
st.download_button(
|
778 |
label="π₯ Download Report",
|
779 |
data=pdf_bytes,
|
|
|
783 |
)
|
784 |
|
785 |
with col2:
|
786 |
+
# Email button that shows email form when clicked
|
787 |
if st.button("π§ Email Report"):
|
788 |
st.session_state.show_email_form = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
789 |
st.rerun()
|
790 |
|
791 |
# Show email form if the button was clicked
|
|
|
827 |
st.error(message)
|
828 |
|
829 |
# Cancel button to hide email form
|
830 |
+
if st.button("Cancel"):
|
831 |
st.session_state.show_email_form = False
|
832 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
833 |
|
834 |
# Button to start over
|
835 |
if st.button("Generate Another Report"):
|
836 |
st.session_state.report_step = 1
|
837 |
st.session_state.show_email_form = False
|
|
|
838 |
st.rerun()
|
839 |
|
840 |
# Put the toggle in the sidebar - this is the most reliable approach
|