Spaces:
Sleeping
Sleeping
| 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() |