Spaces:
Sleeping
Sleeping
Update app.py
Browse filesName bilgileri de llm'e verildi
app.py
CHANGED
|
@@ -185,6 +185,57 @@ def summarize_pois(gdf, pois):
|
|
| 185 |
|
| 186 |
return summary
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
def build_stats_text(summary: dict) -> str:
|
| 190 |
"""
|
|
@@ -234,6 +285,10 @@ def prepare_comparison(city, district1, neigh1, district2, neigh2):
|
|
| 234 |
msg = "Şehir, ilçe ve iki mahalle de girilmelidir."
|
| 235 |
return (msg, msg, "", "<b>Harita için yeterli veri yok.</b>")
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
# Mahalle 1
|
| 238 |
gdf1 = get_neighborhood_gdf(city, district1, neigh1)
|
| 239 |
if gdf1 is None or len(gdf1) == 0:
|
|
@@ -243,6 +298,8 @@ def prepare_comparison(city, district1, neigh1, district2, neigh2):
|
|
| 243 |
pois1 = get_pois_within(gdf1)
|
| 244 |
summary1 = summarize_pois(gdf1, pois1)
|
| 245 |
stats1 = build_stats_text(summary1)
|
|
|
|
|
|
|
| 246 |
|
| 247 |
# Mahalle 2
|
| 248 |
gdf2 = get_neighborhood_gdf(city, district2, neigh2)
|
|
@@ -253,6 +310,8 @@ def prepare_comparison(city, district1, neigh1, district2, neigh2):
|
|
| 253 |
pois2 = get_pois_within(gdf2)
|
| 254 |
summary2 = summarize_pois(gdf2, pois2)
|
| 255 |
stats2 = build_stats_text(summary2)
|
|
|
|
|
|
|
| 256 |
|
| 257 |
# LLM bağlamı
|
| 258 |
compare_context_parts = [
|
|
@@ -261,11 +320,19 @@ def prepare_comparison(city, district1, neigh1, district2, neigh2):
|
|
| 261 |
f"1. Mahalle: {neigh1} (İlçe: {district1})",
|
| 262 |
stats1,
|
| 263 |
"",
|
|
|
|
|
|
|
|
|
|
| 264 |
f"2. Mahalle: {neigh2} (İlçe: {district2})",
|
| 265 |
stats2,
|
| 266 |
"",
|
| 267 |
-
"
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
]
|
| 270 |
compare_context = "\n".join(compare_context_parts)
|
| 271 |
|
|
@@ -276,6 +343,7 @@ def prepare_comparison(city, district1, neigh1, district2, neigh2):
|
|
| 276 |
|
| 277 |
|
| 278 |
|
|
|
|
| 279 |
def add_poi_markers_to_map(pois, m, layer_prefix="POI"):
|
| 280 |
"""
|
| 281 |
POI GeoDataFrame'ini alır, amenity/leisure/shop sütunlarına göre
|
|
@@ -331,7 +399,13 @@ def add_poi_markers_to_map(pois, m, layer_prefix="POI"):
|
|
| 331 |
fg.add_to(m)
|
| 332 |
layer_groups[layer_name] = fg
|
| 333 |
|
|
|
|
|
|
|
| 334 |
popup_items = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
if isinstance(amenity, str):
|
| 336 |
popup_items.append(f"amenity={amenity}")
|
| 337 |
if isinstance(leisure, str):
|
|
@@ -351,6 +425,7 @@ def add_poi_markers_to_map(pois, m, layer_prefix="POI"):
|
|
| 351 |
).add_to(layer_groups[layer_name])
|
| 352 |
|
| 353 |
|
|
|
|
| 354 |
def create_comparison_map(gdf1, gdf2):
|
| 355 |
"""
|
| 356 |
İki mahalle poligonunu tek bir Folium haritasında gösterir.
|
|
|
|
| 185 |
|
| 186 |
return summary
|
| 187 |
|
| 188 |
+
def build_poi_names_text(pois, max_per_category=15) -> str:
|
| 189 |
+
"""
|
| 190 |
+
POI GeoDataFrame'inden okul, eczane, kafe, restoran, market vb.
|
| 191 |
+
için isim listeleri çıkarır. LLM bağlamında kullanılacak metni döndürür.
|
| 192 |
+
"""
|
| 193 |
+
if pois is None or len(pois) == 0:
|
| 194 |
+
return "Bu mahalle için isim verisi olan POI bulunamadı.\n"
|
| 195 |
+
|
| 196 |
+
if "name" not in pois.columns:
|
| 197 |
+
return "Bu mahallede POI'ler için 'name' alanı bulunamadı.\n"
|
| 198 |
+
|
| 199 |
+
lines = []
|
| 200 |
+
|
| 201 |
+
def add_category(title, mask):
|
| 202 |
+
sub = pois[mask]
|
| 203 |
+
if sub is None or len(sub) == 0:
|
| 204 |
+
return
|
| 205 |
+
names = (
|
| 206 |
+
sub["name"]
|
| 207 |
+
.dropna()
|
| 208 |
+
.astype(str)
|
| 209 |
+
.str.strip()
|
| 210 |
+
)
|
| 211 |
+
names = [n for n in names if n]
|
| 212 |
+
if not names:
|
| 213 |
+
return
|
| 214 |
+
unique_names = sorted(set(names))[:max_per_category]
|
| 215 |
+
lines.append(f"{title}:")
|
| 216 |
+
for n in unique_names:
|
| 217 |
+
lines.append(f" - {n}")
|
| 218 |
+
lines.append("") # kategori arası boş satır
|
| 219 |
+
|
| 220 |
+
# Kategoriler
|
| 221 |
+
if "amenity" in pois.columns:
|
| 222 |
+
amenity = pois["amenity"]
|
| 223 |
+
add_category("Okullar", amenity == "school")
|
| 224 |
+
add_category("Eczaneler", amenity == "pharmacy")
|
| 225 |
+
add_category("Kafeler", amenity == "cafe")
|
| 226 |
+
add_category("Restoranlar", amenity == "restaurant")
|
| 227 |
+
|
| 228 |
+
if "shop" in pois.columns:
|
| 229 |
+
shop = pois["shop"]
|
| 230 |
+
# İstersen buraya başka shop türleri de ekleyebilirsin
|
| 231 |
+
add_category("Marketler", shop.isin(["supermarket", "convenience", "mall", "department_store"]))
|
| 232 |
+
|
| 233 |
+
if not lines:
|
| 234 |
+
return "Bu mahallede adı bilinen POI listesi çıkarılamadı.\n"
|
| 235 |
+
|
| 236 |
+
return "\n".join(lines)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
|
| 240 |
def build_stats_text(summary: dict) -> str:
|
| 241 |
"""
|
|
|
|
| 285 |
msg = "Şehir, ilçe ve iki mahalle de girilmelidir."
|
| 286 |
return (msg, msg, "", "<b>Harita için yeterli veri yok.</b>")
|
| 287 |
|
| 288 |
+
# Varsayılan olarak boş metinler
|
| 289 |
+
labels1 = "Bu mahalle için isim verisi çıkarılamadı."
|
| 290 |
+
labels2 = "Bu mahalle için isim verisi çıkarılamadı."
|
| 291 |
+
|
| 292 |
# Mahalle 1
|
| 293 |
gdf1 = get_neighborhood_gdf(city, district1, neigh1)
|
| 294 |
if gdf1 is None or len(gdf1) == 0:
|
|
|
|
| 298 |
pois1 = get_pois_within(gdf1)
|
| 299 |
summary1 = summarize_pois(gdf1, pois1)
|
| 300 |
stats1 = build_stats_text(summary1)
|
| 301 |
+
# >>> POI isim metni
|
| 302 |
+
labels1 = build_poi_names_text(pois1)
|
| 303 |
|
| 304 |
# Mahalle 2
|
| 305 |
gdf2 = get_neighborhood_gdf(city, district2, neigh2)
|
|
|
|
| 310 |
pois2 = get_pois_within(gdf2)
|
| 311 |
summary2 = summarize_pois(gdf2, pois2)
|
| 312 |
stats2 = build_stats_text(summary2)
|
| 313 |
+
# >>> POI isim metni
|
| 314 |
+
labels2 = build_poi_names_text(pois2)
|
| 315 |
|
| 316 |
# LLM bağlamı
|
| 317 |
compare_context_parts = [
|
|
|
|
| 320 |
f"1. Mahalle: {neigh1} (İlçe: {district1})",
|
| 321 |
stats1,
|
| 322 |
"",
|
| 323 |
+
"1. mahalledeki önemli POI isimleri (okullar, eczaneler, marketler, kafeler vb.):",
|
| 324 |
+
labels1,
|
| 325 |
+
"",
|
| 326 |
f"2. Mahalle: {neigh2} (İlçe: {district2})",
|
| 327 |
stats2,
|
| 328 |
"",
|
| 329 |
+
"2. mahalledeki önemli POI isimleri (okullar, eczaneler, marketler, kafeler vb.):",
|
| 330 |
+
labels2,
|
| 331 |
+
"",
|
| 332 |
+
"Bu iki mahalleyi alan, toplam POI sayısı, park, okul, kafe, restoran, eczane sayıları"
|
| 333 |
+
" ve verilen POI isimleri açısından karşılaştır."
|
| 334 |
+
" Kullanıcı soru sorarsa, hem sayısal verilere hem de POI isimlerine dayanarak"
|
| 335 |
+
" açıklayıcı ve dengeli bir karşılaştırma yap."
|
| 336 |
]
|
| 337 |
compare_context = "\n".join(compare_context_parts)
|
| 338 |
|
|
|
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
+
|
| 347 |
def add_poi_markers_to_map(pois, m, layer_prefix="POI"):
|
| 348 |
"""
|
| 349 |
POI GeoDataFrame'ini alır, amenity/leisure/shop sütunlarına göre
|
|
|
|
| 399 |
fg.add_to(m)
|
| 400 |
layer_groups[layer_name] = fg
|
| 401 |
|
| 402 |
+
name = row.get("name")
|
| 403 |
+
|
| 404 |
popup_items = []
|
| 405 |
+
# Önce isim
|
| 406 |
+
if isinstance(name, str) and name.strip():
|
| 407 |
+
popup_items.append(name.strip())
|
| 408 |
+
|
| 409 |
if isinstance(amenity, str):
|
| 410 |
popup_items.append(f"amenity={amenity}")
|
| 411 |
if isinstance(leisure, str):
|
|
|
|
| 425 |
).add_to(layer_groups[layer_name])
|
| 426 |
|
| 427 |
|
| 428 |
+
|
| 429 |
def create_comparison_map(gdf1, gdf2):
|
| 430 |
"""
|
| 431 |
İki mahalle poligonunu tek bir Folium haritasında gösterir.
|