celikn commited on
Commit
1bc28b6
·
verified ·
1 Parent(s): c36e688

Overpass stateleri eklendi

Browse files
Files changed (1) hide show
  1. app.py +49 -45
app.py CHANGED
@@ -606,15 +606,11 @@ def create_comparison_map(gdf1, gdf2):
606
 
607
 
608
 
609
- def run_overpass_to_map(query: str):
610
- """
611
- Overpass QL sorgusunu çalıştırır, Folium haritası ve LLM bağlamı için
612
- metinsel bir özet döndürür.
613
- """
614
  query = normalize_overpass_query(query) if 'normalize_overpass_query' in globals() else query
615
 
616
  if not query or not query.strip():
617
- return "<b>Overpass sorgusu boş.</b>", "Geçerli bir Overpass sorgusu sağlanmadı."
618
 
619
  url = "https://overpass-api.de/api/interpreter"
620
 
@@ -628,32 +624,37 @@ def run_overpass_to_map(query: str):
628
  print("Overpass response text:", resp.text[:500])
629
  except Exception:
630
  pass
631
- return f"<b>Overpass isteği hatası:</b> {e}", "Overpass isteğinde hata oluştu, veri yok."
632
 
633
- elements = data.get("elements", [])
634
- if not elements:
635
- return "<b>Overpass sonucu: veri bulunamadı.</b>", "Overpass sonucu: hiç element bulunamadı."
636
 
637
- # Merkez için ilk noktanın koordinatlarını bulalım
 
 
 
 
 
 
638
  center_lat, center_lon = None, None
639
- for el in elements:
640
  if "lat" in el and "lon" in el:
641
  center_lat, center_lon = el["lat"], el["lon"]
642
  break
643
  if center_lat is None:
644
- return "<b>Overpass sonucu: nokta verisi yok.</b>", "Overpass sonucu: nokta verisi bulunamadı."
645
 
646
  m = folium.Map(location=[center_lat, center_lon], zoom_start=14)
647
 
648
- # Katman: node'lar
649
  fg_nodes = folium.FeatureGroup(name="Noktalar")
650
  fg_nodes.add_to(m)
651
-
652
- # Katman: yollar
653
  fg_ways = folium.FeatureGroup(name="Yollar")
654
  fg_ways.add_to(m)
655
 
656
- for el in elements:
 
657
  etype = el.get("type")
658
  tags = el.get("tags", {})
659
  name = tags.get("name", "")
@@ -666,7 +667,6 @@ def run_overpass_to_map(query: str):
666
  popup_items.append(f"{k}={v}")
667
  popup_text = "<br>".join(popup_items) if popup_items else etype
668
 
669
- # Node → nokta işaretle
670
  if etype == "node" and "lat" in el and "lon" in el:
671
  folium.CircleMarker(
672
  location=[el["lat"], el["lon"]],
@@ -678,7 +678,6 @@ def run_overpass_to_map(query: str):
678
  fill_opacity=0.7,
679
  ).add_to(fg_nodes)
680
 
681
- # Way → polyline çiz
682
  elif etype == "way" and "geometry" in el:
683
  coords = [(p["lat"], p["lon"]) for p in el["geometry"]]
684
  if len(coords) >= 2:
@@ -691,25 +690,30 @@ def run_overpass_to_map(query: str):
691
  folium.LayerControl().add_to(m)
692
 
693
  map_html = m._repr_html_()
694
- summary_text = summarize_overpass_data(data)
695
 
696
- return map_html, summary_text
 
 
 
 
 
 
 
 
697
 
 
 
698
 
699
 
700
 
701
 
702
- def llm_overpass_to_map(natural_prompt: str, model_name: str):
703
- """
704
- 1) Doğal dil prompt'tan LLM ile Overpass QL üretir
705
- 2) Overpass QL'i çalıştırır, folium haritası ve özet döndürür
706
- 3) Hem sorguyu, hem haritayı, hem de spatial özetini return eder
707
- """
708
  if not natural_prompt or not natural_prompt.strip():
709
  return (
710
  "Doğal dil sorgu boş.",
711
  "<b>Overpass sonucu: sorgu üretilemedi.</b>",
712
- "Overpass sonucu: sorgu üretilemedi."
 
713
  )
714
 
715
  try:
@@ -719,17 +723,17 @@ def llm_overpass_to_map(natural_prompt: str, model_name: str):
719
  return (
720
  f"LLM Overpass üretim hatası: {e}",
721
  "<b>Overpass sonucu: LLM hatası.</b>",
722
- "Overpass sonucu: LLM hatası."
 
723
  )
724
 
725
- # run_overpass_to_map zaten (map_html, summary_text) döndürüyor
726
- map_html, summary_text = run_overpass_to_map(query)
727
 
728
- # 3 değer döndürüyoruz:
729
- # - overpass_box içine yazılacak query
730
- # - map_html (harita)
731
- # - spatial_state (LLM için özet)
732
- return query, map_html, summary_text
733
 
734
 
735
  def summarize_overpass_data(data: dict, max_examples: int = 30) -> str:
@@ -937,7 +941,9 @@ with gr.Blocks() as demo:
937
  gr.Markdown("## Mahalle Karşılaştırmalı Chat Botu")
938
 
939
  compare_state = gr.State("")
940
- spatial_state = gr.State("") # 👈 yeni: son Overpass sonuç özeti
 
 
941
 
942
 
943
  with gr.Row():
@@ -1084,22 +1090,20 @@ with gr.Blocks() as demo:
1084
  run_overpass_btn = gr.Button("Sorguyu Çalıştır ve Haritayı Güncelle")
1085
 
1086
  # LLM ile üret + çalıştır
 
1087
  gen_and_run_btn.click(
1088
  fn=llm_overpass_to_map,
1089
- inputs=[osm_nl_prompt, model_dropdown],
1090
- outputs=[overpass_box, map_html, spatial_state], # 👈 artık spatial_state de güncelleniyor
1091
  )
1092
 
1093
-
1094
- # Manuel Overpass çalıştırma
1095
  run_overpass_btn.click(
1096
  fn=run_overpass_to_map,
1097
- inputs=[overpass_box],
1098
- outputs=[map_html, spatial_state], # 👈 harita + LLM için özet
1099
  )
1100
 
1101
-
1102
-
1103
  compare_btn.click(
1104
  fn=prepare_comparison,
1105
  inputs=[city_in, district1_in, neigh1_in, district2_in, neigh2_in],
 
606
 
607
 
608
 
609
+ def run_overpass_to_map(query: str, previous_elements: list | None):
 
 
 
 
610
  query = normalize_overpass_query(query) if 'normalize_overpass_query' in globals() else query
611
 
612
  if not query or not query.strip():
613
+ return "<b>Overpass sorgusu boş.</b>", "Geçerli bir Overpass sorgusu sağlanmadı.", previous_elements
614
 
615
  url = "https://overpass-api.de/api/interpreter"
616
 
 
624
  print("Overpass response text:", resp.text[:500])
625
  except Exception:
626
  pass
627
+ return f"<b>Overpass isteği hatası:</b> {e}", "Overpass isteğinde hata oluştu, veri yok.", previous_elements
628
 
629
+ new_elements = data.get("elements", [])
630
+ if not new_elements:
631
+ return "<b>Overpass sonucu: veri bulunamadı.</b>", "Overpass sonucu: hiç element bulunamadı.", previous_elements
632
 
633
+ # 👇 Öncekilerle birleştir
634
+ if previous_elements is None:
635
+ all_elements = new_elements
636
+ else:
637
+ all_elements = previous_elements + new_elements
638
+
639
+ # Merkez için ilk noktanın koordinatlarını bulalım (tüm elemanlardan)
640
  center_lat, center_lon = None, None
641
+ for el in all_elements:
642
  if "lat" in el and "lon" in el:
643
  center_lat, center_lon = el["lat"], el["lon"]
644
  break
645
  if center_lat is None:
646
+ return "<b>Overpass sonucu: nokta verisi yok.</b>", "Overpass sonucu: nokta verisi bulunamadı.", all_elements
647
 
648
  m = folium.Map(location=[center_lat, center_lon], zoom_start=14)
649
 
650
+ # Katmanlar
651
  fg_nodes = folium.FeatureGroup(name="Noktalar")
652
  fg_nodes.add_to(m)
 
 
653
  fg_ways = folium.FeatureGroup(name="Yollar")
654
  fg_ways.add_to(m)
655
 
656
+ # 👇 Artık sadece yeni değil, tüm elementleri çiziyoruz
657
+ for el in all_elements:
658
  etype = el.get("type")
659
  tags = el.get("tags", {})
660
  name = tags.get("name", "")
 
667
  popup_items.append(f"{k}={v}")
668
  popup_text = "<br>".join(popup_items) if popup_items else etype
669
 
 
670
  if etype == "node" and "lat" in el and "lon" in el:
671
  folium.CircleMarker(
672
  location=[el["lat"], el["lon"]],
 
678
  fill_opacity=0.7,
679
  ).add_to(fg_nodes)
680
 
 
681
  elif etype == "way" and "geometry" in el:
682
  coords = [(p["lat"], p["lon"]) for p in el["geometry"]]
683
  if len(coords) >= 2:
 
690
  folium.LayerControl().add_to(m)
691
 
692
  map_html = m._repr_html_()
 
693
 
694
+ # Özet sadece SON sorgunun datasından
695
+ summary_text = summarize_overpass_data({"elements": new_elements})
696
+
697
+ if previous_spatial:
698
+ new_spatial = previous_spatial + "\n\n--- Yeni Sorgu ---\n" + summary_text
699
+ else:
700
+ new_spatial = summary_text
701
+
702
+ return map_html, new_spatial, all_elements
703
 
704
+ # 👇 hem haritayı, hem özeti hem de birikmiş element listesini döndür
705
+ return map_html, summary_text, all_elements
706
 
707
 
708
 
709
 
710
+ def llm_overpass_to_map(natural_prompt: str, model_name: str, previous_elements: list | None):
 
 
 
 
 
711
  if not natural_prompt or not natural_prompt.strip():
712
  return (
713
  "Doğal dil sorgu boş.",
714
  "<b>Overpass sonucu: sorgu üretilemedi.</b>",
715
+ "Overpass sonucu: sorgu üretilemedi.",
716
+ previous_elements,
717
  )
718
 
719
  try:
 
723
  return (
724
  f"LLM Overpass üretim hatası: {e}",
725
  "<b>Overpass sonucu: LLM hatası.</b>",
726
+ "Overpass sonucu: LLM hatası.",
727
+ previous_elements,
728
  )
729
 
730
+ # 👇 run_overpass_to_map artık previous_elements alıyor
731
+ map_html, summary_text, all_elements = run_overpass_to_map(query, previous_elements)
732
 
733
+ return query, map_html, summary_text, all_elements
734
+
735
+
736
+
 
737
 
738
 
739
  def summarize_overpass_data(data: dict, max_examples: int = 30) -> str:
 
941
  gr.Markdown("## Mahalle Karşılaştırmalı Chat Botu")
942
 
943
  compare_state = gr.State("")
944
+ spatial_state = gr.State("") # son Overpass özetleri
945
+ overpass_elements_state = gr.State([]) # 👈 tüm Overpass sonuçlarını biriktireceğimiz liste
946
+
947
 
948
 
949
  with gr.Row():
 
1090
  run_overpass_btn = gr.Button("Sorguyu Çalıştır ve Haritayı Güncelle")
1091
 
1092
  # LLM ile üret + çalıştır
1093
+ # LLM ile üret + çalıştır (birikimli)
1094
  gen_and_run_btn.click(
1095
  fn=llm_overpass_to_map,
1096
+ inputs=[osm_nl_prompt, model_dropdown, overpass_elements_state],
1097
+ outputs=[overpass_box, map_html, spatial_state, overpass_elements_state],
1098
  )
1099
 
1100
+ # Manuel Overpass çalıştırma (birikimli)
 
1101
  run_overpass_btn.click(
1102
  fn=run_overpass_to_map,
1103
+ inputs=[overpass_box, overpass_elements_state, spatial_state],
1104
+ outputs=[map_html, spatial_state, overpass_elements_state],
1105
  )
1106
 
 
 
1107
  compare_btn.click(
1108
  fn=prepare_comparison,
1109
  inputs=[city_in, district1_in, neigh1_in, district2_in, neigh2_in],