Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
-
from typing import List
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
|
|
|
| 7 |
from ui.components import build_ui
|
| 8 |
-
from rag.ingest import build_corpus
|
| 9 |
-
from rag.retriever import RagStore
|
| 10 |
from services.weather import get_weather_summary
|
| 11 |
from services.geocoding import geocode_city
|
| 12 |
from services.planner import generate_plan
|
|
@@ -16,17 +15,71 @@ load_dotenv()
|
|
| 16 |
INDEX_DIR = "data/index"
|
| 17 |
CORPUS_DIRS = {"blogs": "data/blogs", "reviews": "data/reviews", "events": "data/events"}
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
rag_store = RagStore(index_dir=INDEX_DIR)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def seed_sample_data() -> str:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def handle_build_index(progress=gr.Progress(track_tqdm=True)) -> str:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def handle_generate_plan(
|
| 32 |
city: str,
|
|
@@ -37,13 +90,19 @@ def handle_generate_plan(
|
|
| 37 |
pace: str,
|
| 38 |
date: str,
|
| 39 |
start_time: str,
|
| 40 |
-
force_weather: str
|
| 41 |
mode_pref: str,
|
| 42 |
show_costs: bool,
|
| 43 |
data_source: str,
|
| 44 |
):
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
loc = geocode_city(city)
|
| 49 |
if not loc:
|
|
@@ -70,26 +129,29 @@ def handle_generate_plan(
|
|
| 70 |
|
| 71 |
return plan["map_html"], plan["table"], plan["narrative"]
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
with gr.Blocks(css=".small {font-size: 12px}") as demo:
|
| 74 |
(
|
| 75 |
-
city_in,
|
| 76 |
-
hours_in,
|
| 77 |
-
traveler_in,
|
| 78 |
-
interests_in,
|
| 79 |
-
budget_in,
|
| 80 |
-
pace_in,
|
| 81 |
-
date_in,
|
| 82 |
-
start_time_in,
|
| 83 |
-
weather_in,
|
| 84 |
-
mode_in,
|
| 85 |
-
show_costs_in,
|
| 86 |
-
data_source_in,
|
| 87 |
-
build_btn,
|
| 88 |
-
seed_btn,
|
| 89 |
-
gen_btn,
|
| 90 |
-
map_out,
|
| 91 |
-
table_out,
|
| 92 |
-
text_out,
|
| 93 |
) = build_ui()
|
| 94 |
|
| 95 |
seed_btn.click(fn=seed_sample_data, outputs=[text_out])
|
|
@@ -98,7 +160,7 @@ with gr.Blocks(css=".small {font-size: 12px}") as demo:
|
|
| 98 |
fn=handle_generate_plan,
|
| 99 |
inputs=[
|
| 100 |
city_in, hours_in, traveler_in, interests_in, budget_in, pace_in,
|
| 101 |
-
date_in, start_time_in, weather_in, mode_in, show_costs_in, data_source_in
|
| 102 |
],
|
| 103 |
outputs=[map_out, table_out, text_out],
|
| 104 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
+
from typing import List, Optional
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# UI & services
|
| 8 |
from ui.components import build_ui
|
|
|
|
|
|
|
| 9 |
from services.weather import get_weather_summary
|
| 10 |
from services.geocoding import geocode_city
|
| 11 |
from services.planner import generate_plan
|
|
|
|
| 15 |
INDEX_DIR = "data/index"
|
| 16 |
CORPUS_DIRS = {"blogs": "data/blogs", "reviews": "data/reviews", "events": "data/events"}
|
| 17 |
|
| 18 |
+
# ------------------------------
|
| 19 |
+
# Optional RAG imports (fallback)
|
| 20 |
+
# ------------------------------
|
| 21 |
+
RAG_IMPORT_ERROR: Optional[str] = None
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
from rag.ingest import build_corpus as _build_corpus
|
| 25 |
+
from rag.retriever import RagStore as _RagStore
|
| 26 |
+
|
| 27 |
+
class _SafeRagStore(_RagStore):
|
| 28 |
+
def available(self) -> bool:
|
| 29 |
+
try:
|
| 30 |
+
return super().available()
|
| 31 |
+
except Exception:
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
build_corpus = _build_corpus # type: ignore
|
| 35 |
+
RagStore = _SafeRagStore # type: ignore
|
| 36 |
+
except Exception as e:
|
| 37 |
+
# RAG が存在しない/壊れている場合でも起動できるよう、ダミーにフォールバック
|
| 38 |
+
RAG_IMPORT_ERROR = f"{type(e).__name__}: {e}"
|
| 39 |
+
|
| 40 |
+
def build_corpus(*args, **kwargs):
|
| 41 |
+
return []
|
| 42 |
+
|
| 43 |
+
class RagStore: # type: ignore
|
| 44 |
+
def __init__(self, *args, **kwargs):
|
| 45 |
+
pass
|
| 46 |
+
def available(self) -> bool:
|
| 47 |
+
return False
|
| 48 |
+
def build(self, docs):
|
| 49 |
+
return None
|
| 50 |
+
def search(self, q, k=10):
|
| 51 |
+
return []
|
| 52 |
+
|
| 53 |
rag_store = RagStore(index_dir=INDEX_DIR)
|
| 54 |
|
| 55 |
+
# ------------------------------
|
| 56 |
+
# Handlers
|
| 57 |
+
# ------------------------------
|
| 58 |
+
|
| 59 |
def seed_sample_data() -> str:
|
| 60 |
+
# サンプルデータは任意。存在しない場合でも壊れないようにする
|
| 61 |
+
try:
|
| 62 |
+
from sample_data_seed import seed
|
| 63 |
+
except Exception:
|
| 64 |
+
return "Seed script not found. Skipped."
|
| 65 |
+
try:
|
| 66 |
+
n = seed()
|
| 67 |
+
return f"Seeded sample data: {n} files."
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Seeding failed: {e}"
|
| 70 |
|
| 71 |
def handle_build_index(progress=gr.Progress(track_tqdm=True)) -> str:
|
| 72 |
+
if RAG_IMPORT_ERROR:
|
| 73 |
+
return (
|
| 74 |
+
"RAG modules are not available "
|
| 75 |
+
f"(running without local index). Details: {RAG_IMPORT_ERROR}"
|
| 76 |
+
)
|
| 77 |
+
try:
|
| 78 |
+
docs = build_corpus(CORPUS_DIRS)
|
| 79 |
+
rag_store.build(docs)
|
| 80 |
+
return f"Built index with {len(docs)} documents."
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"Index build failed: {e}"
|
| 83 |
|
| 84 |
def handle_generate_plan(
|
| 85 |
city: str,
|
|
|
|
| 90 |
pace: str,
|
| 91 |
date: str,
|
| 92 |
start_time: str,
|
| 93 |
+
force_weather: Optional[str],
|
| 94 |
mode_pref: str,
|
| 95 |
show_costs: bool,
|
| 96 |
data_source: str,
|
| 97 |
):
|
| 98 |
+
# RAG未利用モードの案内
|
| 99 |
+
if data_source in ("Hybrid", "Local only") and not rag_store.available():
|
| 100 |
+
tip = (
|
| 101 |
+
"Local RAG index is not available. "
|
| 102 |
+
"Click 'Build/Update RAG Index' or switch Data source to 'Web only'."
|
| 103 |
+
)
|
| 104 |
+
# Web only で進めたい場合は UI から切替可能
|
| 105 |
+
return None, None, tip
|
| 106 |
|
| 107 |
loc = geocode_city(city)
|
| 108 |
if not loc:
|
|
|
|
| 129 |
|
| 130 |
return plan["map_html"], plan["table"], plan["narrative"]
|
| 131 |
|
| 132 |
+
# ------------------------------
|
| 133 |
+
# UI wiring
|
| 134 |
+
# ------------------------------
|
| 135 |
with gr.Blocks(css=".small {font-size: 12px}") as demo:
|
| 136 |
(
|
| 137 |
+
city_in, # 0
|
| 138 |
+
hours_in, # 1
|
| 139 |
+
traveler_in, # 2
|
| 140 |
+
interests_in, # 3
|
| 141 |
+
budget_in, # 4
|
| 142 |
+
pace_in, # 5
|
| 143 |
+
date_in, # 6
|
| 144 |
+
start_time_in, # 7
|
| 145 |
+
weather_in, # 8
|
| 146 |
+
mode_in, # 9
|
| 147 |
+
show_costs_in, # 10
|
| 148 |
+
data_source_in, # 11
|
| 149 |
+
build_btn, # 12
|
| 150 |
+
seed_btn, # 13
|
| 151 |
+
gen_btn, # 14
|
| 152 |
+
map_out, # 15
|
| 153 |
+
table_out, # 16
|
| 154 |
+
text_out, # 17
|
| 155 |
) = build_ui()
|
| 156 |
|
| 157 |
seed_btn.click(fn=seed_sample_data, outputs=[text_out])
|
|
|
|
| 160 |
fn=handle_generate_plan,
|
| 161 |
inputs=[
|
| 162 |
city_in, hours_in, traveler_in, interests_in, budget_in, pace_in,
|
| 163 |
+
date_in, start_time_in, weather_in, mode_in, show_costs_in, data_source_in,
|
| 164 |
],
|
| 165 |
outputs=[map_out, table_out, text_out],
|
| 166 |
)
|