Spaces:
Sleeping
Sleeping
Integrated more Overpass
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import geopandas as gpd
|
| 4 |
import folium
|
|
|
|
| 5 |
|
| 6 |
# osmnx opsiyonel (fallback için), yoksa sorun değil
|
| 7 |
try:
|
|
@@ -309,6 +310,40 @@ Use [out:json][timeout:25]; at top.
|
|
| 309 |
End with: out center;
|
| 310 |
"""
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
# ==========================================
|
| 314 |
# MAHALLE KARŞILAŞTIRMA BAĞLAMI
|
|
@@ -551,8 +586,7 @@ def create_comparison_map(gdf1, gdf2):
|
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
-
|
| 555 |
-
import folium
|
| 556 |
|
| 557 |
def run_overpass_to_map(query: str):
|
| 558 |
"""
|
|
@@ -633,6 +667,25 @@ def run_overpass_to_map(query: str):
|
|
| 633 |
folium.LayerControl().add_to(m)
|
| 634 |
return m._repr_html_()
|
| 635 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
|
| 637 |
# ==========================================
|
| 638 |
# LLM SOHBET FONKSİYONU
|
|
@@ -738,60 +791,70 @@ with gr.Blocks() as demo:
|
|
| 738 |
with gr.Row():
|
| 739 |
# SOL SÜTUN: CHAT
|
| 740 |
with gr.Column(scale=2):
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
|
| 796 |
# SAĞ SÜTUN: MAHALLE KARŞILAŞTIRMA PANELİ
|
| 797 |
with gr.Column(scale=1):
|
|
@@ -844,8 +907,19 @@ with gr.Blocks() as demo:
|
|
| 844 |
lines=6,
|
| 845 |
)
|
| 846 |
map_html = gr.HTML(label="Mahalle Haritası")
|
| 847 |
-
gr.Markdown("### Spatial Query (Overpass)")
|
| 848 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
overpass_box = gr.Textbox(
|
| 850 |
label="Overpass Sorgusu",
|
| 851 |
lines=6,
|
|
@@ -854,10 +928,18 @@ with gr.Blocks() as demo:
|
|
| 854 |
|
| 855 |
run_overpass_btn = gr.Button("Sorguyu Çalıştır ve Haritayı Güncelle")
|
| 856 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 857 |
run_overpass_btn.click(
|
| 858 |
fn=run_overpass_to_map,
|
| 859 |
inputs=[overpass_box],
|
| 860 |
-
outputs=[map_html],
|
| 861 |
)
|
| 862 |
|
| 863 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import geopandas as gpd
|
| 4 |
import folium
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
# osmnx opsiyonel (fallback için), yoksa sorun değil
|
| 8 |
try:
|
|
|
|
| 310 |
End with: out center;
|
| 311 |
"""
|
| 312 |
|
| 313 |
+
def generate_overpass_query_from_llm(prompt: str, model_name: str) -> str:
|
| 314 |
+
"""
|
| 315 |
+
Doğal dilde verilen prompt'u kullanarak Overpass QL sorgusu üretir.
|
| 316 |
+
Sadece geçerli Overpass QL döndürmeye çalışır, markdown vs. temizler.
|
| 317 |
+
"""
|
| 318 |
+
prompt = (prompt or "").strip()
|
| 319 |
+
if not prompt:
|
| 320 |
+
prompt = "Generate an Overpass QL query for my request."
|
| 321 |
+
|
| 322 |
+
client = InferenceClient(model=model_name, token=HF_TOKEN if HF_TOKEN else None)
|
| 323 |
+
|
| 324 |
+
messages = [
|
| 325 |
+
{"role": "system", "content": OSM_QUERY_SYSTEM},
|
| 326 |
+
{"role": "user", "content": prompt},
|
| 327 |
+
]
|
| 328 |
+
|
| 329 |
+
# Streaming'e gerek yok, tek seferde alalım
|
| 330 |
+
result = client.chat_completion(
|
| 331 |
+
messages=messages,
|
| 332 |
+
max_tokens=400,
|
| 333 |
+
temperature=0.2,
|
| 334 |
+
top_p=0.9,
|
| 335 |
+
stream=False,
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
# HF InferenceClient sonucu
|
| 339 |
+
query_text = result.choices[0].message.content
|
| 340 |
+
|
| 341 |
+
# Olası ```ql ``` bloklarını temizle
|
| 342 |
+
query_text = query_text.replace("```ql", "").replace("```QL", "").replace("```", "")
|
| 343 |
+
return query_text.strip()
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
|
| 347 |
|
| 348 |
# ==========================================
|
| 349 |
# MAHALLE KARŞILAŞTIRMA BAĞLAMI
|
|
|
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
+
|
|
|
|
| 590 |
|
| 591 |
def run_overpass_to_map(query: str):
|
| 592 |
"""
|
|
|
|
| 667 |
folium.LayerControl().add_to(m)
|
| 668 |
return m._repr_html_()
|
| 669 |
|
| 670 |
+
def llm_overpass_to_map(natural_prompt: str, model_name: str):
|
| 671 |
+
"""
|
| 672 |
+
1) Doğal dil prompt'tan LLM ile Overpass QL üretir
|
| 673 |
+
2) Overpass QL'i çalıştırır, folium haritası döndürür
|
| 674 |
+
3) Hem sorguyu (kullanıcıya gösterilecek) hem harita HTML'ini return eder
|
| 675 |
+
"""
|
| 676 |
+
if not natural_prompt or not natural_prompt.strip():
|
| 677 |
+
return "Doğal dil sorgu boş.", "<b>Overpass sonucu: sorgu üretilemedi.</b>"
|
| 678 |
+
|
| 679 |
+
try:
|
| 680 |
+
query = generate_overpass_query_from_llm(natural_prompt, model_name)
|
| 681 |
+
except Exception as e:
|
| 682 |
+
print("LLM Overpass üretim hatası:", e)
|
| 683 |
+
return f"LLM Overpass üretim hatası: {e}", "<b>Overpass sonucu: LLM hatası.</b>"
|
| 684 |
+
|
| 685 |
+
# Burada kullanıcıya gösterilecek sorgu (fonksiyon gibi görecek)
|
| 686 |
+
map_html = run_overpass_to_map(query)
|
| 687 |
+
return query, map_html
|
| 688 |
+
|
| 689 |
|
| 690 |
# ==========================================
|
| 691 |
# LLM SOHBET FONKSİYONU
|
|
|
|
| 791 |
with gr.Row():
|
| 792 |
# SOL SÜTUN: CHAT
|
| 793 |
with gr.Column(scale=2):
|
| 794 |
+
chatbox = gr.Chatbot(height=800, scale=1)
|
| 795 |
+
|
| 796 |
+
model_dropdown = gr.Dropdown(
|
| 797 |
+
choices=[
|
| 798 |
+
# Küçük modeller
|
| 799 |
+
"google/gemma-2-2b-it", # 2B
|
| 800 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct", # 8B
|
| 801 |
+
|
| 802 |
+
# Büyük modeller
|
| 803 |
+
"abacusai/Dracarys-72B-Instruct", # 72B
|
| 804 |
+
"Qwen/Qwen2.5-72B-Instruct", # 72B
|
| 805 |
+
|
| 806 |
+
# Çok büyük model
|
| 807 |
+
"openai/gpt-oss-120b", # 120B
|
| 808 |
+
],
|
| 809 |
+
label="Model Seç (Bu listedeki modeller Hugging Face Inference API chat_completion ile uyumludur)",
|
| 810 |
+
value="abacusai/Dracarys-72B-Instruct"
|
| 811 |
+
)
|
| 812 |
+
|
| 813 |
+
system_box = gr.Textbox(
|
| 814 |
+
value="Sen şehir planlama ve mahalleler hakkında bilgi veren yardımsever bir asistansın.",
|
| 815 |
+
label="System message",
|
| 816 |
+
)
|
| 817 |
+
|
| 818 |
+
max_tokens_slider = gr.Slider(
|
| 819 |
+
minimum=1,
|
| 820 |
+
maximum=2048,
|
| 821 |
+
value=512,
|
| 822 |
+
step=1,
|
| 823 |
+
label="Max new tokens",
|
| 824 |
+
)
|
| 825 |
+
|
| 826 |
+
temperature_slider = gr.Slider(
|
| 827 |
+
minimum=0.0,
|
| 828 |
+
maximum=2.0, # model max 2
|
| 829 |
+
value=0.7,
|
| 830 |
+
step=0.1,
|
| 831 |
+
label="Temperature",
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
top_p_slider = gr.Slider(
|
| 835 |
+
minimum=0.1,
|
| 836 |
+
maximum=1.0,
|
| 837 |
+
value=0.95,
|
| 838 |
+
step=0.05,
|
| 839 |
+
label="Top-p (nucleus sampling)",
|
| 840 |
+
)
|
| 841 |
+
|
| 842 |
+
chatbot = gr.ChatInterface(
|
| 843 |
+
respond,
|
| 844 |
+
chatbot=chatbox,
|
| 845 |
+
type="messages",
|
| 846 |
+
title="Basit Chat Botu",
|
| 847 |
+
description="Küçük bir sohbet botu, HF Inference API ve OSM verisi ile çalışıyor.",
|
| 848 |
+
additional_inputs=[
|
| 849 |
+
model_dropdown,
|
| 850 |
+
system_box,
|
| 851 |
+
max_tokens_slider,
|
| 852 |
+
temperature_slider,
|
| 853 |
+
top_p_slider,
|
| 854 |
+
compare_state, # >>> mahalle karşılaştırma bağlamı
|
| 855 |
+
],
|
| 856 |
+
)
|
| 857 |
+
|
| 858 |
|
| 859 |
# SAĞ SÜTUN: MAHALLE KARŞILAŞTIRMA PANELİ
|
| 860 |
with gr.Column(scale=1):
|
|
|
|
| 907 |
lines=6,
|
| 908 |
)
|
| 909 |
map_html = gr.HTML(label="Mahalle Haritası")
|
|
|
|
| 910 |
|
| 911 |
+
gr.Markdown("### Spatial Query (Overpass)")
|
| 912 |
+
|
| 913 |
+
# 1) LLM'e doğal dil prompt'u
|
| 914 |
+
osm_nl_prompt = gr.Textbox(
|
| 915 |
+
label="LLM ile Overpass Sorgusu (Doğal Dil)",
|
| 916 |
+
lines=3,
|
| 917 |
+
placeholder="Örn: İncek ve Kızılcaşar çevresindeki tüm park ve okulları getir"
|
| 918 |
+
)
|
| 919 |
+
|
| 920 |
+
gen_and_run_btn = gr.Button("LLM ile Sorguyu Üret ve Çalıştır")
|
| 921 |
+
|
| 922 |
+
# 2) Üretilen veya manuel Overpass sorgusu
|
| 923 |
overpass_box = gr.Textbox(
|
| 924 |
label="Overpass Sorgusu",
|
| 925 |
lines=6,
|
|
|
|
| 928 |
|
| 929 |
run_overpass_btn = gr.Button("Sorguyu Çalıştır ve Haritayı Güncelle")
|
| 930 |
|
| 931 |
+
# LLM ile üret + çalıştır
|
| 932 |
+
gen_and_run_btn.click(
|
| 933 |
+
fn=llm_overpass_to_map,
|
| 934 |
+
inputs=[osm_nl_prompt, model_dropdown], # 👈 modelDropdown'u da kullanıyoruz
|
| 935 |
+
outputs=[overpass_box, map_html], # 👈 kullanıcının göreceği sorgu + harita
|
| 936 |
+
)
|
| 937 |
+
|
| 938 |
+
# Manuel Overpass çalıştırma
|
| 939 |
run_overpass_btn.click(
|
| 940 |
fn=run_overpass_to_map,
|
| 941 |
inputs=[overpass_box],
|
| 942 |
+
outputs=[map_html],
|
| 943 |
)
|
| 944 |
|
| 945 |
|