File size: 2,759 Bytes
e2765b3
 
 
 
 
2bb50b8
e2765b3
 
 
 
 
2bb50b8
e2765b3
 
2bb50b8
e2765b3
 
 
 
 
 
 
 
 
 
2bb50b8
 
e2765b3
 
 
2bb50b8
 
e2765b3
 
 
 
 
2bb50b8
 
 
 
 
 
 
 
 
945ebd4
 
2bb50b8
e2765b3
 
2bb50b8
e2765b3
 
 
 
 
 
 
 
 
 
 
 
 
2bb50b8
e2765b3
 
 
945ebd4
 
e2765b3
 
2bb50b8
 
 
 
 
 
 
 
 
 
 
 
945ebd4
 
2bb50b8
 
 
 
 
 
 
e2765b3
 
 
 
 
2bb50b8
 
 
 
 
 
 
 
 
945ebd4
 
2bb50b8
e2765b3
 
 
2bb50b8
 
1
2
3
4
5
6
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import json
from typing import List, Dict, Any

import gradio as gr
from dotenv import load_dotenv

from ui.components import build_ui
from rag.ingest import build_corpus
from rag.retriever import RagStore
from services.weather import get_weather_summary
from services.geocoding import geocode_city
from services.planner import generate_plan

load_dotenv()

INDEX_DIR = "data/index"
CORPUS_DIRS = {
    "blogs": "data/blogs",
    "reviews": "data/reviews",
    "events": "data/events",
}

rag_store = RagStore(index_dir=INDEX_DIR)

# ------------- Handlers -------------

def seed_sample_data() -> str:
    from sample_data_seed import seed
    n = seed()
    return f"Seeded sample data: {n} files."

def handle_build_index(progress=gr.Progress(track_tqdm=True)) -> str:
    docs = build_corpus(CORPUS_DIRS)
    rag_store.build(docs)
    return f"Built index with {len(docs)} documents."

def handle_generate_plan(
    city: str,
    hours: float,
    traveler: str,
    interests: List[str],
    budget: str,
    pace: str,
    date: str,
    force_weather: str | None,
    mode_pref: str,
    show_costs: bool,
):
    if not rag_store.available():
        return None, None, "Index not built yet. Click 'Build/Update RAG Index'."

    loc = geocode_city(city)
    if not loc:
        return None, None, f"Failed to geocode: {city}"
    lat, lon = loc["lat"], loc["lon"]

    weather = get_weather_summary(lat, lon, date=date, override=force_weather or None)

    plan = generate_plan(
        rag=rag_store,
        center=(lat, lon),
        hours=hours,
        traveler=traveler,
        interests=interests,
        budget=budget,
        pace=pace,
        weather=weather,
        date=date,
        mode_pref=mode_pref,
        include_costs=show_costs,
    )

    return plan["map_html"], plan["table"], plan["narrative"]

with gr.Blocks(css=".small {font-size: 12px}") as demo:
    (
        city_in,
        hours_in,
        traveler_in,
        interests_in,
        budget_in,
        pace_in,
        date_in,
        weather_in,
        mode_in,
        show_costs_in,
        build_btn,
        seed_btn,
        gen_btn,
        map_out,
        table_out,
        text_out,
    ) = build_ui()

    seed_btn.click(fn=seed_sample_data, outputs=[text_out])
    build_btn.click(fn=handle_build_index, outputs=[text_out])
    gen_btn.click(
        fn=handle_generate_plan,
        inputs=[
            city_in,
            hours_in,
            traveler_in,
            interests_in,
            budget_in,
            pace_in,
            date_in,
            weather_in,
            mode_in,
            show_costs_in,
        ],
        outputs=[map_out, table_out, text_out],
    )

if __name__ == "__main__":
    demo.launch()