Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,150 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script>
|
| 2 |
+
/* ======================================
|
| 3 |
+
CONFIG
|
| 4 |
+
====================================== */
|
| 5 |
+
const BACKEND = "https://sivakumar330-location.hf.space";
|
| 6 |
+
let currentSort = "rating";
|
| 7 |
+
|
| 8 |
+
/* ======================================
|
| 9 |
+
TAMIL NADU DISTRICT FALLBACK (FRONTEND)
|
| 10 |
+
(Backend fail ஆனாலும் Unknown வராது)
|
| 11 |
+
====================================== */
|
| 12 |
+
function detectDistrictByLatLon(lat, lon) {
|
| 13 |
+
|
| 14 |
+
// SALEM
|
| 15 |
+
if (lat >= 11.4 && lat <= 11.9 && lon >= 77.9 && lon <= 78.4)
|
| 16 |
+
return "Salem";
|
| 17 |
+
|
| 18 |
+
// DHARMAPURI
|
| 19 |
+
if (lat >= 11.9 && lat <= 12.3 && lon >= 77.8 && lon <= 78.4)
|
| 20 |
+
return "Dharmapuri";
|
| 21 |
+
|
| 22 |
+
// KRISHNAGIRI
|
| 23 |
+
if (lat >= 12.2 && lat <= 12.7 && lon >= 77.8 && lon <= 78.4)
|
| 24 |
+
return "Krishnagiri";
|
| 25 |
+
|
| 26 |
+
// COIMBATORE
|
| 27 |
+
if (lat >= 10.9 && lat <= 11.2 && lon >= 76.7 && lon <= 77.1)
|
| 28 |
+
return "Coimbatore";
|
| 29 |
+
|
| 30 |
+
// CHENNAI
|
| 31 |
+
if (lat >= 12.9 && lat <= 13.2 && lon >= 80.1 && lon <= 80.4)
|
| 32 |
+
return "Chennai";
|
| 33 |
+
|
| 34 |
+
return "Unknown";
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
/* ======================================
|
| 38 |
+
MAIN DISTRICT DETECTION
|
| 39 |
+
====================================== */
|
| 40 |
+
function detectDistrict() {
|
| 41 |
+
const span = document.getElementById("current-district");
|
| 42 |
+
span.innerText = "Detecting...";
|
| 43 |
+
|
| 44 |
+
if (!navigator.geolocation) {
|
| 45 |
+
span.innerText = "Location not supported";
|
| 46 |
+
return;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
navigator.geolocation.getCurrentPosition(
|
| 50 |
+
(pos) => {
|
| 51 |
+
const lat = pos.coords.latitude;
|
| 52 |
+
const lon = pos.coords.longitude;
|
| 53 |
+
|
| 54 |
+
console.log("GPS:", lat, lon);
|
| 55 |
+
|
| 56 |
+
fetch(`${BACKEND}/reverse-geocode?lat=${lat}&lon=${lon}`)
|
| 57 |
+
.then(res => res.json())
|
| 58 |
+
.then(data => {
|
| 59 |
+
let district = data.district;
|
| 60 |
+
|
| 61 |
+
// 🔥 BACKEND FAILED → FRONTEND FALLBACK
|
| 62 |
+
if (!district || district === "Unknown") {
|
| 63 |
+
district = detectDistrictByLatLon(lat, lon);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// 🔥 FINAL SAFETY (never show Unknown)
|
| 67 |
+
if (!district || district === "Unknown") {
|
| 68 |
+
district = "Salem";
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
span.innerText = district;
|
| 72 |
+
loadDoctors(district, lat, lon);
|
| 73 |
+
})
|
| 74 |
+
.catch(() => {
|
| 75 |
+
// 🔥 Network error fallback
|
| 76 |
+
const fallbackDistrict = detectDistrictByLatLon(lat, lon) || "Salem";
|
| 77 |
+
span.innerText = fallbackDistrict;
|
| 78 |
+
loadDoctors(fallbackDistrict, lat, lon);
|
| 79 |
+
});
|
| 80 |
+
},
|
| 81 |
+
() => {
|
| 82 |
+
span.innerText = "Permission denied";
|
| 83 |
+
}
|
| 84 |
+
);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/* ======================================
|
| 88 |
+
LOAD DOCTORS
|
| 89 |
+
====================================== */
|
| 90 |
+
function loadDoctors(district, lat, lon) {
|
| 91 |
+
const disease = document.getElementById("disease").value;
|
| 92 |
+
|
| 93 |
+
let url = `${BACKEND}/doctors?district=${district}&lat=${lat}&lon=${lon}&sort=${currentSort}`;
|
| 94 |
+
if (disease) url += `&disease=${disease}`;
|
| 95 |
+
|
| 96 |
+
fetch(url)
|
| 97 |
+
.then(res => res.json())
|
| 98 |
+
.then(data => displayDoctors(data.doctors || []))
|
| 99 |
+
.catch(() => displayDoctors([]));
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/* ======================================
|
| 103 |
+
SORT HANDLER
|
| 104 |
+
====================================== */
|
| 105 |
+
function sortDoctors(type) {
|
| 106 |
+
currentSort = type;
|
| 107 |
+
detectDistrict();
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
/* ======================================
|
| 111 |
+
DISPLAY DOCTORS
|
| 112 |
+
====================================== */
|
| 113 |
+
function displayDoctors(doctors) {
|
| 114 |
+
const result = document.getElementById("result");
|
| 115 |
+
const count = document.getElementById("doctor-count");
|
| 116 |
+
|
| 117 |
+
count.innerText = doctors.length;
|
| 118 |
+
result.innerHTML = "";
|
| 119 |
+
|
| 120 |
+
if (doctors.length === 0) {
|
| 121 |
+
result.innerHTML = `
|
| 122 |
+
<div class="no-results">
|
| 123 |
+
<h3>No Dermatologists Found</h3>
|
| 124 |
+
<p>No doctors available in this district.</p>
|
| 125 |
+
</div>
|
| 126 |
+
`;
|
| 127 |
+
return;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
doctors.forEach(d => {
|
| 131 |
+
result.innerHTML += `
|
| 132 |
+
<div class="card">
|
| 133 |
+
<h3>${d.name}</h3>
|
| 134 |
+
<p>${d.hospital}</p>
|
| 135 |
+
<p>${d.address}</p>
|
| 136 |
+
<p>⭐ ${d.rating} | ${d.experience} yrs</p>
|
| 137 |
+
<p>📍 ${d.distance ?? "-"} km</p>
|
| 138 |
+
<a href="tel:${d.phone}">📞 Call</a>
|
| 139 |
+
</div>
|
| 140 |
+
`;
|
| 141 |
+
});
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/* ======================================
|
| 145 |
+
INIT
|
| 146 |
+
====================================== */
|
| 147 |
+
window.onload = () => {
|
| 148 |
+
detectDistrict();
|
| 149 |
+
};
|
| 150 |
+
</script>
|