Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,104 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import folium
|
| 3 |
from folium.plugins import MarkerCluster
|
|
|
|
|
|
|
| 4 |
import tempfile
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# =========================
|
| 8 |
-
# DATABASE YA HOSPITALS
|
| 9 |
# =========================
|
| 10 |
hospitals_data = [
|
| 11 |
-
{"name": "King Faisal Hospital", "province": "Kigali City", "district": "Gasabo",
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
{"name": "
|
| 18 |
-
"
|
| 19 |
-
{"name": "CHUB (Butare University Teaching Hospital)", "province": "Southern Province", "district": "Huye", "sector": "Ngoma",
|
| 20 |
-
"contact": "", "email": "", "gps": [-2.590,29.739], "specialty": ["General","Pediatrics","Surgery"]},
|
| 21 |
-
{"name": "Shyira Hospital", "province": "Western Province", "district": "Nyamasheke", "sector": "Bushenge",
|
| 22 |
-
"contact": "", "email": "", "gps": [-2.482,29.155], "specialty": ["General","Maternity"]},
|
| 23 |
-
{"name": "Butaro Hospital", "province": "Northern Province", "district": "Burera", "sector": "Butaro",
|
| 24 |
-
"contact": "", "email": "", "gps": [-1.641,29.893], "specialty": ["General","Oncology","Pediatrics"]}
|
| 25 |
]
|
| 26 |
|
| 27 |
# =========================
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# =========================
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if not results:
|
| 41 |
-
return "
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# Create
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
for h in results:
|
| 48 |
-
|
| 49 |
<b>{h['name']}</b><br>
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
Contact: {h['contact'] if h['contact'] else 'N/A'}<br>
|
| 54 |
-
Email: {h['email'] if h['email'] else 'N/A'}<br>
|
| 55 |
-
Specialty: {', '.join(h['specialty'])}
|
| 56 |
"""
|
| 57 |
-
folium.Marker(location=h["gps"], popup=
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# =========================
|
| 65 |
# GRADIO INTERFACE
|
| 66 |
# =========================
|
| 67 |
iface = gr.Interface(
|
| 68 |
-
fn=
|
| 69 |
inputs=[
|
| 70 |
-
gr.
|
| 71 |
-
gr.
|
| 72 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
],
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
description="Shaka ibitaro mu Rwanda, urabona hospitals kuri interactive map. Ushobora filter province, district, specialty."
|
| 77 |
)
|
| 78 |
|
| 79 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import folium
|
| 3 |
from folium.plugins import MarkerCluster
|
| 4 |
+
from geopy.distance import geodesic
|
| 5 |
+
import spacy
|
| 6 |
import tempfile
|
| 7 |
+
|
| 8 |
+
# Load NLP model
|
| 9 |
+
nlp = spacy.load("en_core_web_sm")
|
| 10 |
|
| 11 |
# =========================
|
| 12 |
+
# DATABASE YA HOSPITALS
|
| 13 |
# =========================
|
| 14 |
hospitals_data = [
|
| 15 |
+
{"name": "King Faisal Hospital", "province": "Kigali City", "district": "Gasabo",
|
| 16 |
+
"gps": (-1.949,30.089), "specialty": ["general","maternity","surgery"]},
|
| 17 |
+
|
| 18 |
+
{"name": "CHUK", "province": "Kigali City", "district": "Nyarugenge",
|
| 19 |
+
"gps": (-1.950,30.058), "specialty": ["general","pediatrics","emergency"]},
|
| 20 |
+
|
| 21 |
+
{"name": "Butaro Hospital", "province": "Northern Province", "district": "Burera",
|
| 22 |
+
"gps": (-1.641,29.893), "specialty": ["general","oncology","pediatrics"]},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
]
|
| 24 |
|
| 25 |
# =========================
|
| 26 |
+
# NLP FUNCTION
|
| 27 |
+
# =========================
|
| 28 |
+
def extract_specialty(text):
|
| 29 |
+
text = text.lower()
|
| 30 |
+
for sp in ["maternity","pediatrics","oncology","surgery","emergency","general"]:
|
| 31 |
+
if sp in text:
|
| 32 |
+
return sp
|
| 33 |
+
return ""
|
| 34 |
+
|
| 35 |
+
# =========================
|
| 36 |
+
# MAIN AI FUNCTION
|
| 37 |
# =========================
|
| 38 |
+
def ai_healthcare_assistant(user_text, user_lat, user_lon):
|
| 39 |
+
specialty = extract_specialty(user_text)
|
| 40 |
+
|
| 41 |
+
results = []
|
| 42 |
+
user_location = (user_lat, user_lon)
|
| 43 |
+
|
| 44 |
+
for h in hospitals_data:
|
| 45 |
+
if specialty and specialty not in h["specialty"]:
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
distance = geodesic(user_location, h["gps"]).km
|
| 49 |
+
h_copy = h.copy()
|
| 50 |
+
h_copy["distance"] = round(distance, 2)
|
| 51 |
+
results.append(h_copy)
|
| 52 |
|
| 53 |
if not results:
|
| 54 |
+
return "Nta bitaro bibonetse", None
|
| 55 |
+
|
| 56 |
+
results = sorted(results, key=lambda x: x["distance"])
|
| 57 |
|
| 58 |
+
# Create map
|
| 59 |
+
m = folium.Map(location=[user_lat, user_lon], zoom_start=8)
|
| 60 |
+
cluster = MarkerCluster().add_to(m)
|
| 61 |
+
|
| 62 |
+
folium.Marker(
|
| 63 |
+
location=[user_lat, user_lon],
|
| 64 |
+
popup="YOU ARE HERE",
|
| 65 |
+
icon=folium.Icon(color="blue")
|
| 66 |
+
).add_to(m)
|
| 67 |
|
| 68 |
for h in results:
|
| 69 |
+
popup = f"""
|
| 70 |
<b>{h['name']}</b><br>
|
| 71 |
+
District: {h['district']}<br>
|
| 72 |
+
Specialty: {', '.join(h['specialty'])}<br>
|
| 73 |
+
Distance: {h['distance']} km
|
|
|
|
|
|
|
|
|
|
| 74 |
"""
|
| 75 |
+
folium.Marker(location=h["gps"], popup=popup).add_to(cluster)
|
| 76 |
|
| 77 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".html")
|
| 78 |
+
m.save(tmp.name)
|
| 79 |
+
|
| 80 |
+
text_response = "🏥 Ibitaro byegereye aho uri:\n"
|
| 81 |
+
for h in results:
|
| 82 |
+
text_response += f"- {h['name']} ({h['distance']} km)\n"
|
| 83 |
+
|
| 84 |
+
return text_response, tmp.name
|
| 85 |
|
| 86 |
# =========================
|
| 87 |
# GRADIO INTERFACE
|
| 88 |
# =========================
|
| 89 |
iface = gr.Interface(
|
| 90 |
+
fn=ai_healthcare_assistant,
|
| 91 |
inputs=[
|
| 92 |
+
gr.Textbox(label="Vuga ikibazo cyawe (ex: I need pediatric hospital near me)"),
|
| 93 |
+
gr.Number(label="Latitude (aho uri)"),
|
| 94 |
+
gr.Number(label="Longitude (aho uri)")
|
| 95 |
+
],
|
| 96 |
+
outputs=[
|
| 97 |
+
gr.Textbox(label="AI Recommendation"),
|
| 98 |
+
gr.File(label="Hospital Map")
|
| 99 |
],
|
| 100 |
+
title="AI Healthcare Assistant – Rwanda (Advanced)",
|
| 101 |
+
description="AI ikumva icyo ushaka, ikamenya ibitaro byegereye aho uri hakurikijwe specialty."
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
iface.launch()
|