File size: 2,213 Bytes
8c390e0
5f511f1
8c390e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from agent.src.vector_store.configs import INDEX_DIR


def _index_exists() -> bool:
    return (INDEX_DIR / "index.faiss").exists()


def _status_message() -> str:
    if _index_exists():
        return "Index found at: " + str(INDEX_DIR)
    return "No index found. Click 'Build Index' to create one."


def build_index_action():
    try:
        from src.vector_store.builder import get_data, build_index
    except ImportError as e:
        yield f"Missing dependency: {e}"
        return

    yield "Loading files...\n"
    docs = get_data()
    yield f"Loaded {len(docs)} files.\n\nChunking and embedding (this may take a while)...\n"
    build_index()
    yield f"Done! Index saved to:\n{INDEX_DIR}"


def test_search_action(query: str):
    if not query.strip():
        return "Type a query above first."
    if not _index_exists():
        return "Index not found. Build it first."

    try:
        from src.vector_store.builder import load_index
    except ImportError as e:
        return f"Missing dependency: {e}"

    vs = load_index()
    results = vs.similarity_search(query, k=5)

    lines = []
    for i, doc in enumerate(results, 1):
        source = doc.metadata.get("source", "?")
        context = doc.metadata.get("context", "")
        lines.append(f"### Result {i} — `{context}/{source}`\n{doc.page_content}")

    return "\n\n---\n\n".join(lines)


def create_tab():
    with gr.Tab("Vector Store"):
        gr.Markdown("## Vector Store\nBuild the FAISS index from your lore files, then test retrieval.")

        status = gr.Textbox(
            label="Status",
            value=_status_message,
            interactive=False,
            lines=1,
        )

        build_btn = gr.Button("Build Index", variant="primary")
        log = gr.Textbox(label="Log", lines=8, interactive=False)

        build_btn.click(fn=build_index_action, outputs=log)

        gr.Markdown("---\n### Test Retrieval")
        query_box = gr.Textbox(label="Query", placeholder="e.g. Quais guildas existem em Ploya?")
        search_btn = gr.Button("Search")
        results_box = gr.Markdown()

        search_btn.click(fn=test_search_action, inputs=query_box, outputs=results_box)