VIATEUR-AI commited on
Commit
7c61dbb
·
verified ·
1 Parent(s): e48ff2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -49
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
- import webbrowser
 
 
6
 
7
  # =========================
8
- # DATABASE YA HOSPITALS MU RWANDA (GPS + specialty)
9
  # =========================
10
  hospitals_data = [
11
- {"name": "King Faisal Hospital", "province": "Kigali City", "district": "Gasabo", "sector": "Kacyiru",
12
- "contact": "+250788123200", "email": "info@kingfaisalhosp.rw", "gps": [-1.949,30.089], "specialty": ["General","Maternity","Surgery"]},
13
- {"name": "CHUK", "province": "Kigali City", "district": "Nyarugenge", "sector": "Nyarugenge",
14
- "contact": "", "email": "", "gps": [-1.950,30.058], "specialty": ["General","Pediatrics","Emergency"]},
15
- {"name": "Gahini District Hospital", "province": "Eastern Province", "district": "Kayonza", "sector": "Gahini",
16
- "contact": "0781403761", "email": "gahini.hospital@moh.gov.rw", "gps": [-2.019,30.576], "specialty": ["General","Orthopedic"]},
17
- {"name": "Rwinkwavu District Hospital", "province": "Eastern Province", "district": "Kayonza", "sector": "Rwinkwavu",
18
- "contact": "0788358810", "email": "rwinkwavu.hospital@moh.gov.rw", "gps": [-2.151,30.565], "specialty": ["General","Maternity"]},
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
- # FUNCTION YO GUSHIRAHO MAP
 
 
 
 
 
 
 
 
 
 
29
  # =========================
30
- def healthcare_map(province="", district="", specialty=""):
31
- # Filter hospitals
32
- results = hospitals_data
33
- if province:
34
- results = [h for h in results if h["province"].lower() == province.lower()]
35
- if district:
36
- results = [h for h in results if h["district"].lower() == district.lower()]
37
- if specialty:
38
- results = [h for h in results if specialty.lower() in [s.lower() for s in h["specialty"]]]
 
 
 
 
 
39
 
40
  if not results:
41
- return "Ntibitaro bibonetse. Ongera ugerageze keyword cyangwa filter."
 
 
42
 
43
- # Create folium map centered in Rwanda
44
- rwanda_map = folium.Map(location=[-1.940, 29.873], zoom_start=7)
45
- marker_cluster = MarkerCluster().add_to(rwanda_map)
 
 
 
 
 
 
46
 
47
  for h in results:
48
- popup_text = f"""
49
  <b>{h['name']}</b><br>
50
- Intara: {h['province']}<br>
51
- Akarere: {h['district']}<br>
52
- Umurenge: {h['sector']}<br>
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=popup_text).add_to(marker_cluster)
58
 
59
- # Save map to temporary HTML file
60
- tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
61
- rwanda_map.save(tmp_file.name)
62
- return tmp_file.name
 
 
 
 
63
 
64
  # =========================
65
  # GRADIO INTERFACE
66
  # =========================
67
  iface = gr.Interface(
68
- fn=healthcare_map,
69
  inputs=[
70
- gr.Dropdown(label="Province (optional)", choices=[""] + [h["province"] for h in hospitals_data], type="value"),
71
- gr.Textbox(label="District (optional)", placeholder="Akarere niba ushaka"),
72
- gr.Dropdown(label="Specialty (optional)", choices=[""] + ["General","Maternity","Pediatrics","Surgery","Orthopedic","Psychiatric","Oncology"], type="value")
 
 
 
 
73
  ],
74
- outputs=gr.File(label="Interactive Map of Hospitals"),
75
- title="Rwanda Healthcare AI Interactive Map",
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()