Func_med_history / chatbot_app.py
drkareemkamal's picture
Update chatbot_app.py
814a280 verified
import streamlit as st
from datetime import date
st.set_page_config(page_title="Male Intake Chatbot", layout="centered")
st.title("๐Ÿค– Male Intake Chatbot")
st.markdown("Welcome! I will guide you through the Male Intake Questionnaire step by step.")
# Initialize session state
if "step" not in st.session_state:
st.session_state.step = 0
st.session_state.answers = {}
questions = [
# Section: General Info
("general", "๐Ÿง‘ What is your full name?", "name", "text"),
("general", "๐ŸŽ‚ How old are you?", "age", "number"),
("general", "๐Ÿ“… What is your date of birth?", "dob", "date"),
("general", "๐Ÿ“ง What is your email address?", "email", "text"),
("general", "๐Ÿ  What is your home address?", "address", "text"),
("general", "๐Ÿ™๏ธ City?", "city", "text"),
("general", "๐ŸŒ State/Province?", "state", "text"),
("general", "๐Ÿ“ฎ Zip code?", "zip", "text"),
# Section: Contact Info
("contact", "๐Ÿ“ž Home phone number?", "phone_home", "text"),
("contact", "๐Ÿ“ฑ Cell phone number?", "phone_cell", "text"),
("contact", "โ˜Ž๏ธ Work phone number?", "phone_work", "text"),
# Section: Emergency
("emergency", "๐Ÿ‘ค Emergency contact name?", "emergency_name", "text"),
("emergency", "๐Ÿ“– Relationship to you?", "emergency_relationship", "text"),
("emergency", "๐Ÿ  Emergency contact home phone?", "emergency_phone_home", "text"),
("emergency", "๐Ÿ“ฑ Emergency contact cell phone?", "emergency_phone_cell", "text"),
("emergency", "โ˜Ž๏ธ Emergency contact work phone?", "emergency_phone_work", "text"),
# Section: Genetic Background
("genetic", "๐ŸŒ What is your genetic background? (Separate multiple with commas)", "genetic_background", "text"),
# Section: Nutrition
("nutrition", "๐Ÿฅฆ Do you follow any special diets? (e.g., Vegan, Low Carb, etc.)", "special_diets", "text"),
("nutrition", "โš ๏ธ Do you have sensitivities to certain foods?", "food_sensitivities", "radio"),
("nutrition", "๐Ÿคข If yes, list the foods and symptoms", "sensitivity_details", "text"),
("nutrition", "๐Ÿ˜– Do you have an aversion to certain foods?", "food_aversion", "radio"),
("nutrition", "๐Ÿ“„ If yes, explain the aversion", "aversion_details", "text"),
("nutrition", "โค๏ธ Do you crave or binge on any food?", "food_cravings", "radio"),
("nutrition", "๐Ÿซ If yes, what foods?", "craved_foods", "text"),
# Section: Meals
("meals", "๐Ÿฝ๏ธ Do you eat 3 meals per day?", "eat_3_meals", "radio"),
("meals", "๐Ÿ“Š If no, how many meals?", "meals_count", "text"),
("meals", "๐Ÿ˜ต Does skipping a meal affect you?", "skip_meal_effect", "radio"),
("meals", "๐Ÿ” How many meals do you eat out per week? (0-1, 1-3, etc.)", "meals_out_per_week", "text"),
# Section: Caffeine
("caffeine", "โ˜• Do you drink caffeinated beverages?", "drink_caffeine", "radio"),
("caffeine", "โ˜• Coffee (cups per day)?", "coffee_intake", "text"),
("caffeine", "๐Ÿต Tea (cups per day)?", "tea_intake", "text"),
("caffeine", "๐Ÿฅค Soda (cans per day)?", "soda_intake", "text"),
("caffeine", "๐Ÿ˜Ÿ Any adverse reactions to caffeine?", "caffeine_reaction", "radio"),
("caffeine", "๐Ÿ“„ If yes, explain:", "caffeine_reaction_detail", "text"),
# Section: Smoking
("smoking", "๐Ÿšฌ Do you currently smoke?", "smoke_now", "radio"),
("smoking", "๐Ÿ“ฆ Packs per day?", "packs_per_day", "text"),
("smoking", "๐Ÿ“… Number of years?", "smoke_years", "text"),
("smoking", "๐Ÿ“ฆ What type? (Cigarette, Pipe, etc.)", "smoke_type", "text"),
("smoking", "๐Ÿ” Have you tried quitting?", "tried_quit", "radio"),
("smoking", "๐Ÿ“‹ If yes, using what methods?", "quit_methods", "text"),
# Section: Stress
("stress", "๐Ÿ˜ฐ Do you feel you have too much stress?", "high_stress", "radio"),
("stress", "๐Ÿง˜ Can you handle stress easily?", "stress_handling", "radio"),
("stress", "๐Ÿ’ผ Rate work stress (1โ€“10)", "stress_work", "number"),
("stress", "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Rate family stress (1โ€“10)", "stress_family", "number"),
("stress", "๐Ÿ’ธ Rate financial stress (1โ€“10)", "stress_finance", "number"),
# More sections like: History, Dental, Men's Health, Family, etc. can be added here similarly...
]
# Step through questions
if st.session_state.step < len(questions):
section, question_text, field_name, field_type = questions[st.session_state.step]
st.markdown(f"### Section: {section.capitalize()}")
if field_type == "text":
response = st.text_input(question_text)
elif field_type == "number":
response = st.number_input(question_text, step=1)
elif field_type == "date":
response = st.date_input(question_text, value=date(2000, 1, 1))
elif field_type == "radio":
response = st.radio(question_text, ["Yes", "No"])
else:
response = st.text_input(question_text)
if response:
st.session_state.answers[field_name] = response
st.session_state.step += 1
st.experimental_rerun()
else:
st.success("๐ŸŽ‰ You've completed the intake chatbot!")
st.json(st.session_state.answers)
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
# Create a function to save intake form answers to PDF
def save_intake_to_pdf(answers: dict, output_path: str):
c = canvas.Canvas(output_path, pagesize=letter)
width, height = letter
line_height = 14
x_margin = 50
y = height - 50
c.setFont("Helvetica", 12)
c.drawString(x_margin, y, "Male Intake Questionnaire - Summary")
y -= line_height * 2
for key, value in answers.items():
# Wrap long values
text = f"{key.replace('_', ' ').capitalize()}: {value}"
lines = []
while len(text) > 90:
split_pos = text.rfind(" ", 0, 90)
lines.append(text[:split_pos])
text = text[split_pos+1:]
lines.append(text)
for line in lines:
if y <= 50:
c.showPage()
y = height - 50
c.setFont("Helvetica", 12)
c.drawString(x_margin, y, line)
y -= line_height
y -= 5 # extra spacing between entries
c.save()
return output_path
# Example usage
example_answers = {
"name": "John Doe",
"age": "35",
"dob": "1989-03-15",
"email": "[email protected]",
"stress_work": 7,
"smoke_now": "No",
"food_sensitivities": "Yes",
"sensitivity_details": "Dairy - causes bloating",
}
pdf_path = "intake_summary_example.pdf"
save_intake_to_pdf(example_answers, pdf_path)