Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,81 @@
|
|
| 1 |
import json
|
| 2 |
-
import
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
from shapely.geometry import shape, Point
|
| 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 |
-
return
|
| 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 |
-
{modern}
|
| 87 |
-
|
| 88 |
-
旧地名に最も対応する現在の地名を1つ選び、理由を説明し、
|
| 89 |
-
JSON形式で name, score を出力してください。
|
| 90 |
-
"""
|
| 91 |
-
|
| 92 |
-
gpt = client.chat.completions.create(
|
| 93 |
-
model="gpt-4o",
|
| 94 |
-
messages=[{"role": "user", "content": prompt}]
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
answer = gpt.choices[0].message["content"]
|
| 98 |
-
|
| 99 |
-
return {
|
| 100 |
-
"old_name": q.old_name,
|
| 101 |
-
"lat": new_lat,
|
| 102 |
-
"lng": new_lon,
|
| 103 |
-
"gpt_match": answer,
|
| 104 |
-
"modern_candidates": modern,
|
| 105 |
-
}
|
|
|
|
| 1 |
import json
|
| 2 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 3 |
from shapely.geometry import shape, Point
|
| 4 |
+
|
| 5 |
+
# ===========================
|
| 6 |
+
# データロード
|
| 7 |
+
# ===========================
|
| 8 |
+
with open("data/meiji_village.geojson", "r", encoding="utf-8") as f:
|
| 9 |
+
geo = json.load(f)["features"]
|
| 10 |
+
|
| 11 |
+
# index 作成(高速検索)
|
| 12 |
+
village_index = []
|
| 13 |
+
for feat in geo:
|
| 14 |
+
props = feat["properties"]
|
| 15 |
+
geom = shape(feat["geometry"])
|
| 16 |
+
village_index.append({
|
| 17 |
+
"pref": props.get("pref"),
|
| 18 |
+
"gun": props.get("gun"),
|
| 19 |
+
"mura": props.get("mura"),
|
| 20 |
+
"id": props.get("id"),
|
| 21 |
+
"geometry": geom
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ===========================
|
| 26 |
+
# 古地名検索 API
|
| 27 |
+
# ===========================
|
| 28 |
+
def search_place(name, lat=None, lon=None):
|
| 29 |
+
results = []
|
| 30 |
+
|
| 31 |
+
# ① 地名(村名・郡名)から検索
|
| 32 |
+
if name:
|
| 33 |
+
key = name.strip()
|
| 34 |
+
for v in village_index:
|
| 35 |
+
if key in v["mura"] or key in v["gun"]:
|
| 36 |
+
results.append({
|
| 37 |
+
"都道府県": v["pref"],
|
| 38 |
+
"郡": v["gun"],
|
| 39 |
+
"村": v["mura"],
|
| 40 |
+
"ID": v["id"]
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
# ② 座標を与えられた場合 → 村界にポイントが含まれるか判定
|
| 44 |
+
if lat and lon:
|
| 45 |
+
pt = Point(float(lon), float(lat))
|
| 46 |
+
for v in village_index:
|
| 47 |
+
if v["geometry"].contains(pt):
|
| 48 |
+
results.append({
|
| 49 |
+
"都道府県": v["pref"],
|
| 50 |
+
"郡": v["gun"],
|
| 51 |
+
"村": v["mura"],
|
| 52 |
+
"ID": v["id"]
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
if len(results) == 0:
|
| 56 |
+
return "該当なし"
|
| 57 |
+
|
| 58 |
+
return results
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# ===========================
|
| 62 |
+
# Gradio UI
|
| 63 |
+
# ===========================
|
| 64 |
+
def run_search(name, latitude, longitude):
|
| 65 |
+
return search_place(name, latitude, longitude)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as app:
|
| 69 |
+
gr.Markdown("# 古地名 → 明治22年村界検索(京都+大阪)")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
name = gr.Textbox(label="地名(村名・郡名)")
|
| 73 |
+
lat = gr.Textbox(label="緯度 lat(任意)")
|
| 74 |
+
lon = gr.Textbox(label="経度 lon(任意)")
|
| 75 |
+
|
| 76 |
+
btn = gr.Button("検索")
|
| 77 |
+
output = gr.JSON(label="結果")
|
| 78 |
+
|
| 79 |
+
btn.click(run_search, inputs=[name, lat, lon], outputs=output)
|
| 80 |
+
|
| 81 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|