Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,18 +7,6 @@ program_explanations = {
|
|
| 7 |
"aca": "ACA subsidies are financial help from the Affordable Care Act that reduce your monthly insurance premium based on your income.",
|
| 8 |
}
|
| 9 |
|
| 10 |
-
# Load clinic info (organized by county)
|
| 11 |
-
with open("info.txt", "r", encoding="utf-8") as file:
|
| 12 |
-
clinic_data = file.read().lower()
|
| 13 |
-
|
| 14 |
-
# Keep track of conversation state
|
| 15 |
-
conversation_state = {
|
| 16 |
-
"stage": "intro",
|
| 17 |
-
"age": None,
|
| 18 |
-
"income": None,
|
| 19 |
-
"last_eligible_program": None, # <--- NEW FIELD
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
# Helper to get clinics by county
|
| 23 |
def find_clinic_by_county(county_name, file_path="info.txt"):
|
| 24 |
clinics = []
|
|
@@ -36,102 +24,117 @@ def find_clinic_by_county(county_name, file_path="info.txt"):
|
|
| 36 |
clinics.append(line.strip())
|
| 37 |
if not clinics:
|
| 38 |
return "Sorry, I couldn't find any clinics for that county. Please check your spelling or try a nearby county."
|
| 39 |
-
|
| 40 |
-
# Add Medicaid/EBT explanation
|
| 41 |
clinics.append(
|
| 42 |
"---\n\nMedicaid (Apple Health) is Washington’s free health insurance for low-income residents. "
|
| 43 |
"It covers doctor visits, mental health, prescriptions, dental, and more.\n\n"
|
| 44 |
"EBT (SNAP) helps you buy groceries if you qualify. You’ll get a card you can use at most stores."
|
| 45 |
)
|
| 46 |
-
|
| 47 |
-
# Return with double spacing between clinics
|
| 48 |
return "\n\n".join(clinics)
|
| 49 |
|
| 50 |
-
# Chatbot logic
|
| 51 |
-
def chatbot(message, history):
|
| 52 |
message = message.strip().lower()
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Check for program keywords in message
|
| 55 |
for key in program_explanations:
|
| 56 |
if key in message:
|
| 57 |
-
return program_explanations[key] + "\n\nAnything else you'd like help with?"
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
if any(phrase in message for phrase in
|
| 61 |
-
program =
|
| 62 |
if program and program in program_explanations:
|
| 63 |
-
return program_explanations[program] + "\n\nAnything else you'd like help with?"
|
| 64 |
|
| 65 |
response = ""
|
| 66 |
-
|
| 67 |
-
if
|
| 68 |
-
response = (
|
|
|
|
| 69 |
"or figure out what health programs you may qualify for. "
|
| 70 |
-
"What would you like help with? Just say 'clinic' or 'eligibility'."
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
elif
|
| 74 |
if "eligib" in message or "qualify" in message or "insurance" in message:
|
| 75 |
response = "Sure, I can help with that. How old are you and what’s your yearly income?"
|
| 76 |
-
|
| 77 |
elif "clinic" in message or "help" in message or "doctor" in message:
|
| 78 |
response = "What county in Washington are you in?"
|
| 79 |
-
|
| 80 |
else:
|
| 81 |
response = "Would you like help finding a clinic or seeing what programs you're eligible for? Just say 'clinic' or 'eligibility'."
|
| 82 |
|
| 83 |
-
elif
|
| 84 |
numbers = [int(w) for w in message.split() if w.isdigit()]
|
| 85 |
if len(numbers) >= 2:
|
| 86 |
age, income = numbers[0], numbers[1]
|
| 87 |
-
|
| 88 |
-
|
| 89 |
elif len(numbers) == 1:
|
| 90 |
num = numbers[0]
|
| 91 |
if num < 120:
|
| 92 |
-
|
| 93 |
-
return "Got it — now how much do you make each year?"
|
| 94 |
else:
|
| 95 |
-
|
| 96 |
-
return "Thanks! Now please tell me your age."
|
| 97 |
else:
|
| 98 |
-
return "Could you tell me your age and income? For example: 'I'm 30 and make $25,000'."
|
| 99 |
|
| 100 |
-
age =
|
| 101 |
-
income =
|
| 102 |
|
| 103 |
# Decide eligibility
|
| 104 |
if income < 20000:
|
| 105 |
eligible = "free Medicaid coverage"
|
| 106 |
-
|
| 107 |
elif income < 40000:
|
| 108 |
eligible = "ACA subsidies that reduce the cost of insurance"
|
| 109 |
-
|
| 110 |
else:
|
| 111 |
eligible = "some limited public programs, depending on your needs"
|
| 112 |
-
|
| 113 |
|
| 114 |
response = f"Based on what you shared, you may qualify for {eligible}. Is there anything else I can help you with?"
|
| 115 |
-
|
| 116 |
|
| 117 |
-
elif
|
| 118 |
county = message.strip()
|
| 119 |
clinic_info = find_clinic_by_county(county)
|
| 120 |
response = clinic_info + "\n\nNeed help with anything else?"
|
| 121 |
-
|
| 122 |
|
| 123 |
else:
|
| 124 |
response = "I'm not sure what you meant. Would you like help with free clinics or program eligibility?"
|
| 125 |
-
|
| 126 |
-
return response
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
with gr.Blocks(css="body { background-color: #EDE8E8; }") as demo:
|
| 130 |
with gr.Column():
|
| 131 |
gr.Image("heading.png", show_label=False, show_download_button=False)
|
| 132 |
-
gr.ChatInterface(
|
| 133 |
fn=chatbot,
|
| 134 |
title="HealthPal.io",
|
| 135 |
description="Use this chatbot to help you find affordable healthcare and figure out what medical subsidies you are eligible for!",
|
|
|
|
| 136 |
)
|
| 137 |
demo.launch()
|
|
|
|
| 7 |
"aca": "ACA subsidies are financial help from the Affordable Care Act that reduce your monthly insurance premium based on your income.",
|
| 8 |
}
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Helper to get clinics by county
|
| 11 |
def find_clinic_by_county(county_name, file_path="info.txt"):
|
| 12 |
clinics = []
|
|
|
|
| 24 |
clinics.append(line.strip())
|
| 25 |
if not clinics:
|
| 26 |
return "Sorry, I couldn't find any clinics for that county. Please check your spelling or try a nearby county."
|
|
|
|
|
|
|
| 27 |
clinics.append(
|
| 28 |
"---\n\nMedicaid (Apple Health) is Washington’s free health insurance for low-income residents. "
|
| 29 |
"It covers doctor visits, mental health, prescriptions, dental, and more.\n\n"
|
| 30 |
"EBT (SNAP) helps you buy groceries if you qualify. You’ll get a card you can use at most stores."
|
| 31 |
)
|
|
|
|
|
|
|
| 32 |
return "\n\n".join(clinics)
|
| 33 |
|
| 34 |
+
# Chatbot logic with persistent state passed in and returned
|
| 35 |
+
def chatbot(message, history, state):
|
| 36 |
message = message.strip().lower()
|
| 37 |
+
|
| 38 |
+
# Unpack state dict or initialize if empty
|
| 39 |
+
if state is None or not state:
|
| 40 |
+
state = {
|
| 41 |
+
"stage": "intro",
|
| 42 |
+
"age": None,
|
| 43 |
+
"income": None,
|
| 44 |
+
"last_eligible_program": None,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Explanation trigger phrases
|
| 48 |
+
explain_triggers = [
|
| 49 |
+
"what does that mean", "what's that", "explain", "what is that",
|
| 50 |
+
"can you explain", "tell me about that", "what do you mean"
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
# Check for program keywords in message
|
| 54 |
for key in program_explanations:
|
| 55 |
if key in message:
|
| 56 |
+
return program_explanations[key] + "\n\nAnything else you'd like help with?", history, state
|
| 57 |
+
|
| 58 |
+
# Check if user asks for explanation of last eligible program
|
| 59 |
+
if any(phrase in message for phrase in explain_triggers):
|
| 60 |
+
program = state.get("last_eligible_program")
|
| 61 |
if program and program in program_explanations:
|
| 62 |
+
return program_explanations[program] + "\n\nAnything else you'd like help with?", history, state
|
| 63 |
|
| 64 |
response = ""
|
| 65 |
+
|
| 66 |
+
if state["stage"] == "intro":
|
| 67 |
+
response = (
|
| 68 |
+
"Hi! I can help you find free clinics in your Washington county, "
|
| 69 |
"or figure out what health programs you may qualify for. "
|
| 70 |
+
"What would you like help with? Just say 'clinic' or 'eligibility'."
|
| 71 |
+
)
|
| 72 |
+
state["stage"] = "wait_for_choice"
|
| 73 |
|
| 74 |
+
elif state["stage"] == "wait_for_choice":
|
| 75 |
if "eligib" in message or "qualify" in message or "insurance" in message:
|
| 76 |
response = "Sure, I can help with that. How old are you and what’s your yearly income?"
|
| 77 |
+
state["stage"] = "wait_for_eligibility"
|
| 78 |
elif "clinic" in message or "help" in message or "doctor" in message:
|
| 79 |
response = "What county in Washington are you in?"
|
| 80 |
+
state["stage"] = "wait_for_county"
|
| 81 |
else:
|
| 82 |
response = "Would you like help finding a clinic or seeing what programs you're eligible for? Just say 'clinic' or 'eligibility'."
|
| 83 |
|
| 84 |
+
elif state["stage"] == "wait_for_eligibility":
|
| 85 |
numbers = [int(w) for w in message.split() if w.isdigit()]
|
| 86 |
if len(numbers) >= 2:
|
| 87 |
age, income = numbers[0], numbers[1]
|
| 88 |
+
state["age"] = age
|
| 89 |
+
state["income"] = income
|
| 90 |
elif len(numbers) == 1:
|
| 91 |
num = numbers[0]
|
| 92 |
if num < 120:
|
| 93 |
+
state["age"] = num
|
| 94 |
+
return "Got it — now how much do you make each year?", history, state
|
| 95 |
else:
|
| 96 |
+
state["income"] = num
|
| 97 |
+
return "Thanks! Now please tell me your age.", history, state
|
| 98 |
else:
|
| 99 |
+
return "Could you tell me your age and income? For example: 'I'm 30 and make $25,000'.", history, state
|
| 100 |
|
| 101 |
+
age = state["age"]
|
| 102 |
+
income = state["income"]
|
| 103 |
|
| 104 |
# Decide eligibility
|
| 105 |
if income < 20000:
|
| 106 |
eligible = "free Medicaid coverage"
|
| 107 |
+
state["last_eligible_program"] = "medicaid"
|
| 108 |
elif income < 40000:
|
| 109 |
eligible = "ACA subsidies that reduce the cost of insurance"
|
| 110 |
+
state["last_eligible_program"] = "aca"
|
| 111 |
else:
|
| 112 |
eligible = "some limited public programs, depending on your needs"
|
| 113 |
+
state["last_eligible_program"] = "medicare"
|
| 114 |
|
| 115 |
response = f"Based on what you shared, you may qualify for {eligible}. Is there anything else I can help you with?"
|
| 116 |
+
state["stage"] = "wait_for_choice"
|
| 117 |
|
| 118 |
+
elif state["stage"] == "wait_for_county":
|
| 119 |
county = message.strip()
|
| 120 |
clinic_info = find_clinic_by_county(county)
|
| 121 |
response = clinic_info + "\n\nNeed help with anything else?"
|
| 122 |
+
state["stage"] = "wait_for_choice"
|
| 123 |
|
| 124 |
else:
|
| 125 |
response = "I'm not sure what you meant. Would you like help with free clinics or program eligibility?"
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
return response, history, state
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# Gradio UI with state persistence
|
| 131 |
with gr.Blocks(css="body { background-color: #EDE8E8; }") as demo:
|
| 132 |
with gr.Column():
|
| 133 |
gr.Image("heading.png", show_label=False, show_download_button=False)
|
| 134 |
+
chat = gr.ChatInterface(
|
| 135 |
fn=chatbot,
|
| 136 |
title="HealthPal.io",
|
| 137 |
description="Use this chatbot to help you find affordable healthcare and figure out what medical subsidies you are eligible for!",
|
| 138 |
+
state={"stage": "intro", "age": None, "income": None, "last_eligible_program": None},
|
| 139 |
)
|
| 140 |
demo.launch()
|