aasiavakil commited on
Commit
af94026
·
verified ·
1 Parent(s): 1d03ed9

Update app.py

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