aasiavakil commited on
Commit
bf5fabf
·
verified ·
1 Parent(s): 69d1e0e

Update app.py

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