Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,151 @@
|
|
| 1 |
-
from huggingface_hub import InferenceClient
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
-
import torch
|
| 4 |
import gradio as gr
|
| 5 |
-
import random
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
with open("info.txt", "r", encoding="utf-8") as file:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
clinics = []
|
|
|
|
|
|
|
|
|
|
| 26 |
inside_county = False
|
| 27 |
count = 0
|
| 28 |
-
for line in
|
| 29 |
if county_name.lower() in line.lower():
|
| 30 |
inside_county = True
|
| 31 |
continue
|
| 32 |
if inside_county:
|
| 33 |
if "county" in line.lower() and county_name.lower() not in line.lower():
|
| 34 |
break
|
| 35 |
-
if line.strip()
|
| 36 |
clinics.append(line.strip())
|
| 37 |
count += 1
|
| 38 |
if count >= limit * 5:
|
| 39 |
break
|
| 40 |
return "\n".join(clinics) if clinics else "No clinics found for that county."
|
| 41 |
|
| 42 |
-
|
| 43 |
def chatbot(message, history):
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
return f"Here are some free clinics in {message.title()} County:\n\n{clinics}"
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
response
|
| 63 |
-
prompt=full_prompt,
|
| 64 |
-
max_new_tokens=150,
|
| 65 |
-
temperature=temperature,
|
| 66 |
-
top_p=0.9,
|
| 67 |
-
stop=["<|user|>", "<|assistant|>"]
|
| 68 |
-
)
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
gr.ChatInterface(chatbot, title="Washington Healthcare Bot").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# helper to get clinics by county
|
| 4 |
+
def get_clinics_by_county(county_name, file_path="info.txt", limit=5):
|
| 5 |
+
clinics = []
|
| 6 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
| 7 |
+
lines = file.readlines()
|
| 8 |
+
|
| 9 |
+
inside_county = False
|
| 10 |
+
count = 0
|
| 11 |
+
for line in lines:
|
| 12 |
+
if county_name.lower() in line.lower():
|
| 13 |
+
inside_county = True
|
| 14 |
+
continue
|
| 15 |
+
if inside_county:
|
| 16 |
+
if "county" in line.lower() and county_name.lower() not in line.lower():
|
| 17 |
+
break
|
| 18 |
+
if line.strip():
|
| 19 |
+
clinics.append(line.strip())
|
| 20 |
+
count += 1
|
| 21 |
+
if count >= limit * 5:
|
| 22 |
+
break
|
| 23 |
+
return "\n".join(clinics) if clinics else "No clinics found for that county."
|
| 24 |
|
| 25 |
+
|
| 26 |
+
# Load clinic info (organized by county)
|
| 27 |
with open("info.txt", "r", encoding="utf-8") as file:
|
| 28 |
+
clinic_data = file.read().lower()
|
| 29 |
+
|
| 30 |
+
# Keep track of state
|
| 31 |
+
conversation_state = {
|
| 32 |
+
"stage": "intro", # intro → wait_for_choice → wait_for_eligibility / wait_for_county
|
| 33 |
+
"age": None,
|
| 34 |
+
"income": None,
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
program_explanations = {
|
| 38 |
+
"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.",
|
| 39 |
+
"medicare": "Medicare is a federal health insurance program mainly for people age 65 or older and certain younger people with disabilities.",
|
| 40 |
+
"aca": "ACA subsidies are financial help from the Affordable Care Act that reduce your monthly insurance premium based on your income.",
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def find_clinic_by_county(county):
|
| 44 |
+
county = county.lower()
|
| 45 |
+
sections = clinic_data.split("###")
|
| 46 |
+
for section in sections:
|
| 47 |
+
if county in section:
|
| 48 |
+
lines = section.strip().split("\n")
|
| 49 |
+
if lines and county in lines[0].lower():
|
| 50 |
+
lines = lines[1:]
|
| 51 |
+
return "\n".join(lines).strip()
|
| 52 |
+
return "Sorry, I couldn't find any clinics for that county. Please check your spelling or try a nearby county."
|
| 53 |
+
|
| 54 |
+
# helper to get clinics by county
|
| 55 |
+
def get_clinics_by_county(county_name, file_path="info.txt", limit=5):
|
| 56 |
clinics = []
|
| 57 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
| 58 |
+
lines = file.readlines()
|
| 59 |
+
|
| 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 |
|
| 76 |
+
|
| 77 |
def chatbot(message, history):
|
| 78 |
+
message = message.strip().lower()
|
| 79 |
+
|
| 80 |
+
# Check if user asks about programs
|
| 81 |
+
for key in program_explanations:
|
| 82 |
+
if key in message:
|
| 83 |
+
return program_explanations[key] + "\n\nAnything else you'd like help with?"
|
| 84 |
+
|
| 85 |
+
response = ""
|
| 86 |
+
|
| 87 |
+
if conversation_state["stage"] == "intro":
|
| 88 |
+
response = "Hi! I can help you find free clinics in your Washington county, or figure out what health programs you may qualify for."
|
| 89 |
+
conversation_state["stage"] = "wait_for_choice"
|
| 90 |
+
|
| 91 |
+
elif conversation_state["stage"] == "wait_for_choice":
|
| 92 |
+
if "eligib" in message or "qualify" in message or "insurance" in message:
|
| 93 |
+
response = "Sure, I can help with that. How old are you and what’s your yearly income?"
|
| 94 |
+
conversation_state["stage"] = "wait_for_eligibility"
|
| 95 |
+
elif "clinic" in message or "help" in message or "doctor" in message:
|
| 96 |
+
response = "What county in Washington are you in?"
|
| 97 |
+
conversation_state["stage"] = "wait_for_county"
|
| 98 |
+
else:
|
| 99 |
+
response = "Would you like help finding a clinic or seeing what programs you're eligible for? Just say 'clinic' or 'eligibility'."
|
| 100 |
+
|
| 101 |
+
elif conversation_state["stage"] == "wait_for_eligibility":
|
| 102 |
+
numbers = [int(w) for w in message.split() if w.isdigit()]
|
| 103 |
+
if len(numbers) >= 2:
|
| 104 |
+
age, income = numbers[0], numbers[1]
|
| 105 |
+
conversation_state["age"] = age
|
| 106 |
+
conversation_state["income"] = income
|
| 107 |
+
elif len(numbers) == 1:
|
| 108 |
+
num = numbers[0]
|
| 109 |
+
if num < 120:
|
| 110 |
+
conversation_state["age"] = num
|
| 111 |
+
response = "Got it — now how much do you make each year?"
|
| 112 |
+
return response
|
| 113 |
+
else:
|
| 114 |
+
conversation_state["income"] = num
|
| 115 |
+
response = "Thanks! Now please tell me your age."
|
| 116 |
+
return response
|
| 117 |
+
else:
|
| 118 |
+
return "Could you tell me your age and income? For example: 'I'm 30 and make $25,000'."
|
| 119 |
|
| 120 |
+
age = conversation_state["age"]
|
| 121 |
+
income = conversation_state["income"]
|
| 122 |
+
if income < 20000:
|
| 123 |
+
eligible = "free Medicaid coverage"
|
| 124 |
+
elif income < 40000:
|
| 125 |
+
eligible = "ACA subsidies that reduce the cost of insurance"
|
| 126 |
+
else:
|
| 127 |
+
eligible = "some limited public programs, depending on your needs"
|
| 128 |
|
| 129 |
+
response = f"Based on what you shared, you may qualify for {eligible}. Is there anything else I can help you with?"
|
| 130 |
+
conversation_state["stage"] = "wait_for_choice"
|
|
|
|
| 131 |
|
| 132 |
+
elif conversation_state["stage"] == "wait_for_county":
|
| 133 |
+
county = message.strip()
|
| 134 |
+
clinic_info = find_clinic_by_county(county)
|
| 135 |
+
response = clinic_info + "\n\nNeed help with anything else?"
|
| 136 |
+
conversation_state["stage"] = "wait_for_choice"
|
| 137 |
|
| 138 |
+
else:
|
| 139 |
+
response = "I'm not sure what you meant. Would you like help with free clinics or program eligibility?"
|
| 140 |
|
| 141 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
# Gradio app
|
| 144 |
+
with gr.Blocks() as demo:
|
| 145 |
+
with gr.Row():
|
| 146 |
+
gr.Markdown("# HealthPal.io")
|
| 147 |
+
with gr.Row():
|
| 148 |
+
gr.Markdown("## Helping Washington State residents find free clinics and health programs.")
|
| 149 |
+
gr.ChatInterface(chatbot)
|
| 150 |
|
| 151 |
+
demo.launch()
|
|
|