Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,8 @@ import re
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# APIの基本URL(安定版)
|
| 6 |
BASE_URL = "https://laws.e-gov.go.jp/api/2/laws"
|
| 7 |
|
| 8 |
-
# よく使う法令をキャッシュ
|
| 9 |
LAW_IDS = {
|
| 10 |
"民法": "429AC0000000089",
|
| 11 |
"会社法": "429AC0000000086",
|
|
@@ -14,7 +12,6 @@ LAW_IDS = {
|
|
| 14 |
"不動産登記法": "429AC0000000092",
|
| 15 |
}
|
| 16 |
|
| 17 |
-
# 数字→漢数字変換(第3条など)
|
| 18 |
KANJI = "零一二三四五六七八九十百"
|
| 19 |
def int_to_kanji(num):
|
| 20 |
if num <= 10:
|
|
@@ -29,54 +26,36 @@ def int_to_kanji(num):
|
|
| 29 |
|
| 30 |
def parse_query(q):
|
| 31 |
q = q.strip().replace(" ", "")
|
| 32 |
-
q = re.sub(r"(?<=\d)上$", "条", q)
|
| 33 |
m = re.match(r"(?P<law>[^0-9\s条]+)\s*(第?(?P<num>\d+)条?)", q)
|
| 34 |
if not m:
|
| 35 |
raise ValueError("「民法3条」「会社法10条」みたいに入力して。")
|
| 36 |
return m.group("law"), int(m.group("num"))
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
if isinstance(v, str) and target_label in v:
|
| 50 |
-
return obj
|
| 51 |
-
for v in obj.values():
|
| 52 |
-
result = search_dict(v)
|
| 53 |
-
if result:
|
| 54 |
-
return result
|
| 55 |
-
elif isinstance(obj, list):
|
| 56 |
-
for v in obj:
|
| 57 |
-
result = search_dict(v)
|
| 58 |
-
if result:
|
| 59 |
-
return result
|
| 60 |
-
return None
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if isinstance(o, str):
|
| 70 |
-
paras.append(o)
|
| 71 |
-
elif isinstance(o, dict):
|
| 72 |
-
for val in o.values():
|
| 73 |
-
collect_text(val)
|
| 74 |
-
elif isinstance(o, list):
|
| 75 |
-
for i in o:
|
| 76 |
-
collect_text(i)
|
| 77 |
-
|
| 78 |
-
collect_text(block)
|
| 79 |
-
return "\n".join(paras)
|
| 80 |
|
| 81 |
def run(q):
|
| 82 |
try:
|
|
@@ -84,11 +63,9 @@ def run(q):
|
|
| 84 |
law_id = LAW_IDS.get(law)
|
| 85 |
if not law_id:
|
| 86 |
return f"法令が登録されていません:「{law}」"
|
| 87 |
-
data = get_law_json(law_id)
|
| 88 |
target = f"第{int_to_kanji(num)}条"
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
return f"【{law_name}】{target}\n\n{text.strip()}"
|
| 92 |
except Exception as e:
|
| 93 |
return f"❌ エラー: {e}"
|
| 94 |
|
|
@@ -97,7 +74,7 @@ demo = gr.Interface(
|
|
| 97 |
inputs=gr.Textbox(label="例)民法3条 / 会社法10条 / 不動産登記法47条"),
|
| 98 |
outputs=gr.Textbox(label="結果", lines=20),
|
| 99 |
title="e-Gov 法令API v2|条文検索",
|
| 100 |
-
description="法令名+条番号を入
|
| 101 |
)
|
| 102 |
|
| 103 |
demo.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
| 5 |
BASE_URL = "https://laws.e-gov.go.jp/api/2/laws"
|
| 6 |
|
|
|
|
| 7 |
LAW_IDS = {
|
| 8 |
"民法": "429AC0000000089",
|
| 9 |
"会社法": "429AC0000000086",
|
|
|
|
| 12 |
"不動産登記法": "429AC0000000092",
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
KANJI = "零一二三四五六七八九十百"
|
| 16 |
def int_to_kanji(num):
|
| 17 |
if num <= 10:
|
|
|
|
| 26 |
|
| 27 |
def parse_query(q):
|
| 28 |
q = q.strip().replace(" ", "")
|
| 29 |
+
q = re.sub(r"(?<=\d)上$", "条", q)
|
| 30 |
m = re.match(r"(?P<law>[^0-9\s条]+)\s*(第?(?P<num>\d+)条?)", q)
|
| 31 |
if not m:
|
| 32 |
raise ValueError("「民法3条」「会社法10条」みたいに入力して。")
|
| 33 |
return m.group("law"), int(m.group("num"))
|
| 34 |
|
| 35 |
+
def flatten_text(obj):
|
| 36 |
+
"""JSON全体から文字列だけを抽出"""
|
| 37 |
+
if isinstance(obj, str):
|
| 38 |
+
return obj
|
| 39 |
+
elif isinstance(obj, dict):
|
| 40 |
+
return "".join(flatten_text(v) for v in obj.values())
|
| 41 |
+
elif isinstance(obj, list):
|
| 42 |
+
return "".join(flatten_text(i) for i in obj)
|
| 43 |
+
return ""
|
| 44 |
|
| 45 |
+
def get_article(law_id, article_label):
|
| 46 |
+
url = f"{BASE_URL}?lawId={law_id}"
|
| 47 |
+
r = requests.get(url, timeout=30)
|
| 48 |
+
r.raise_for_status()
|
| 49 |
+
data = r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
text = flatten_text(data)
|
| 52 |
+
idx = text.find(article_label)
|
| 53 |
+
if idx == -1:
|
| 54 |
+
return f"{article_label} が見つかりません。"
|
| 55 |
|
| 56 |
+
snippet = text[idx : idx + 500]
|
| 57 |
+
snippet = re.split(r"第[一二三四五六七八九十百]+条", snippet)[0]
|
| 58 |
+
return snippet.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def run(q):
|
| 61 |
try:
|
|
|
|
| 63 |
law_id = LAW_IDS.get(law)
|
| 64 |
if not law_id:
|
| 65 |
return f"法令が登録されていません:「{law}」"
|
|
|
|
| 66 |
target = f"第{int_to_kanji(num)}条"
|
| 67 |
+
text = get_article(law_id, target)
|
| 68 |
+
return f"【{law}】{target}\n\n{text}"
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"❌ エラー: {e}"
|
| 71 |
|
|
|
|
| 74 |
inputs=gr.Textbox(label="例)民法3条 / 会社法10条 / 不動産登記法47条"),
|
| 75 |
outputs=gr.Textbox(label="結果", lines=20),
|
| 76 |
title="e-Gov 法令API v2|条文検索",
|
| 77 |
+
description="法令名+条番号を入力して該当条文を取得します(安定版API・全文走査)"
|
| 78 |
)
|
| 79 |
|
| 80 |
demo.launch()
|