File size: 6,075 Bytes
3a9cc29
bf5fabf
e4ec05d
af94026
 
 
 
 
 
 
 
 
 
bf5fabf
6f930da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af94026
bf5fabf
 
 
 
 
af94026
bf5fabf
af94026
 
da295c4
 
 
 
 
 
 
dff2aa5
bf5fabf
dff2aa5
 
af94026
e4ec05d
d7eb366
af94026
 
 
bf5fabf
af94026
dff2aa5
af94026
 
5aa239b
af94026
 
 
dff2aa5
 
af94026
dff2aa5
 
af94026
dff2aa5
 
af94026
dff2aa5
 
 
af94026
 
dff2aa5
 
 
af94026
 
dff2aa5
af94026
 
dff2aa5
af94026
 
 
bf5fabf
dff2aa5
 
af94026
dff2aa5
 
af94026
dff2aa5
 
af94026
dff2aa5
af94026
 
dff2aa5
 
 
af94026
dff2aa5
 
af94026
 
fc009a6
f279661
 
af94026
f279661
 
 
 
af94026
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
import gradio as gr
# Program explanation map

# Load clinic info (organized by county)
with open("info.txt", "r", encoding="utf-8") as file:
    clinic_data = file.read().lower()
# Keep track of conversation state
conversation_state = {
    "stage": "intro",
    "age": None,
    "income": None,
    "last_eligible_program": None,  # <--- NEW FIELD
}
# Helper to get clinics by county
def find_clinic_by_county(county_name, file_path="info.txt"):
    clinics = []
    with open(file_path, "r", encoding="utf-8") as file:
        lines = file.readlines()
    inside = False
    for line in lines:
        if line.strip().lower().startswith(county_name.lower() + " county"):
            inside = True
            continue
        if inside:
            if "county" in line.lower() and county_name.lower() not in line.lower():
                break
            if line.strip():
                clinics.append(line.strip())
    if not clinics:
        return "Sorry, I couldn't find any clinics for that county. Please check your spelling or try a nearby county."
    # Add Medicaid/EBT explanation
    clinics.append(
        "---\n\nMedicaid (Apple Health) is Washington’s free health insurance for low-income residents. "
        "It covers doctor visits, mental health, prescriptions, dental, and more.\n\n"
        "EBT (SNAP) helps you buy groceries if you qualify. You’ll get a card you can use at most stores."
    )
    # Return with double spacing between clinics
    return "\n\n".join(clinics)
# Chatbot logic
def chatbot(message, history):

    program_explanations = {
    "medicaid": "Medicaid (called Apple Health in Washington) is a free or low-cost health coverage program for eligible low-income adults, children, pregnant women, and people with disabilities.",
    "medicare": "Medicare is a federal health insurance program mainly for people age 65 or older and certain younger people with disabilities.",
    "aca": "ACA subsidies are financial help from the Affordable Care Act that reduce your monthly insurance premium based on your income.",
    }

    message = message.strip().lower()
    # Check for program keywords in message
    for key in program_explanations:
        if key in message:
            return program_explanations[key] + "\n\nAnything else you'd like help with?"
            
    
    # EXPLANATION TRIGGER FIX
    if any(phrase in message for phrase in ["what does that mean", "what's that", "explain", "what is that"]):
        program = conversation_state.get("last_eligible_program")
        if program and program in program_explanations:
            return program_explanations[program] + "\n\nAnything else you'd like help with?"
    response = ""
    if conversation_state["stage"] == "intro":
        response = ("Hi! I can help you find free clinics in your Washington county, "
            "or figure out what health programs you may qualify for. "
            "What would you like help with? Just say 'clinic' or 'eligibility'.")
        conversation_state["stage"] = "wait_for_choice"
    elif conversation_state["stage"] == "wait_for_choice":
        if "eligib" in message or "qualify" in message or "insurance" in message:
            response = "Sure, I can help with that. How old are you and what’s your yearly income?"
            conversation_state["stage"] = "wait_for_eligibility"
        elif "clinic" in message or "help" in message or "doctor" in message:
            response = "What county in Washington are you in?"
            conversation_state["stage"] = "wait_for_county"
        else:
            response = "Would you like help finding a clinic or seeing what programs you're eligible for? Just say 'clinic' or 'eligibility'."
    elif conversation_state["stage"] == "wait_for_eligibility":
        numbers = [int(w) for w in message.split() if w.isdigit()]
        if len(numbers) >= 2:
            age, income = numbers[0], numbers[1]
            conversation_state["age"] = age
            conversation_state["income"] = income
        elif len(numbers) == 1:
            num = numbers[0]
            if num < 120:
                conversation_state["age"] = num
                return "Got it — now how much do you make each year?"
            else:
                conversation_state["income"] = num
                return "Thanks! Now please tell me your age."
        else:
            return "Could you tell me your age and income? For example: 'I'm 30 and make $25,000'."
        age = conversation_state["age"]
        income = conversation_state["income"]
        # Decide eligibility
        if income < 20000:
            eligible = "free Medicaid coverage"
            conversation_state["last_eligible_program"] = "medicaid"
        elif income < 40000:
            eligible = "ACA subsidies that reduce the cost of insurance"
            conversation_state["last_eligible_program"] = "aca"
        else:
            eligible = "some limited public programs, depending on your needs"
            conversation_state["last_eligible_program"] = "medicare"
        response = f"Based on what you shared, you may qualify for {eligible}. Is there anything else I can help you with?"
        conversation_state["stage"] = "wait_for_choice"
    elif conversation_state["stage"] == "wait_for_county":
        county = message.strip()
        clinic_info = find_clinic_by_county(county)
        response = clinic_info + "\n\nNeed help with anything else?"
        conversation_state["stage"] = "wait_for_choice"
    else:
        response = "I'm not sure what you meant. Would you like help with free clinics or program eligibility?"
    return response
# Gradio UI
with gr.Blocks(css="body { background-color: #EDE8E8; }") as demo:
    with gr.Column():
        gr.Image("heading.png", show_label=False, show_download_button=False)
        gr.ChatInterface(
            fn=chatbot,
            title="HealthPal.io",
            description="Use this chatbot to help you find affordable healthcare and figure out what medical subsidies you are eligible for!",
        )
demo.launch()