File size: 8,772 Bytes
07f8c0c
be43fd9
5bab10b
be43fd9
 
 
 
 
07f8c0c
 
ced2a62
 
be43fd9
 
 
1849681
ced2a62
e55c124
5bab10b
 
 
 
 
 
 
57774c2
 
be43fd9
57774c2
ced2a62
 
a71b179
e55c124
5bab10b
e55c124
5bab10b
e55c124
5bab10b
 
e55c124
1849681
5bab10b
1849681
5bab10b
1849681
 
5bab10b
 
1849681
 
 
07f8c0c
5bab10b
8d7e557
5bab10b
 
 
 
 
ced2a62
 
8d7e557
5bab10b
 
 
 
 
 
 
 
 
 
0c48a7c
8d7e557
5bab10b
 
ced2a62
 
5bab10b
b2013a1
5bab10b
8d7e557
5bab10b
 
ced2a62
 
 
5bab10b
 
 
 
 
 
 
 
 
be43fd9
5bab10b
 
 
 
 
 
 
 
be43fd9
5bab10b
57774c2
e55c124
be43fd9
 
5bab10b
e55c124
5bab10b
 
 
 
 
 
 
777fc4d
5bab10b
 
0c48a7c
 
 
5bab10b
 
 
0c48a7c
 
 
 
 
5bab10b
777fc4d
5bab10b
 
 
 
777fc4d
 
5bab10b
 
 
be43fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
777fc4d
5bab10b
 
 
 
be43fd9
5bab10b
777fc4d
be43fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
777fc4d
5bab10b
 
be43fd9
777fc4d
be43fd9
 
 
 
 
 
 
 
777fc4d
5bab10b
 
777fc4d
 
 
5bab10b
 
 
 
 
777fc4d
 
 
 
be43fd9
777fc4d
 
 
0c48a7c
 
 
 
 
5bab10b
0c48a7c
 
 
 
be43fd9
777fc4d
 
5bab10b
 
777fc4d
 
 
 
 
 
 
 
0c48a7c
 
be43fd9
 
0c48a7c
 
 
be43fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c48a7c
777fc4d
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
app.py β€” Step 9
Adds:
- Feedback logger (Helpful / Not helpful) with optional note β†’ data/feedback.csv
- Per-session ID so you can trace usage (no cookies needed)
- Auto-build index on first question (from Step 8 refined)

Everything else (RAG + Extractive/Generative toggle) stays the same.
"""

from pathlib import Path
from typing import List
import secrets
import time

import gradio as gr

from indexer import build_index
from rag_search import (
    RAGSearcher,
    format_sources,
    extract_links,
    split_form_links,
    links_markdown,
)
from evo_inference import synthesize_with_evo
from utils_lang import SUPPORTED, normalize_lang, L
from feedback import log_feedback  # NEW

_searcher = None
DATA_SEED_DIR = Path("data/seed")
ALLOWED_EXTS = {".txt", ".pdf", ".html", ".htm", ".xhtml"}


def ensure_searcher():
    """Create (or return) a global searcher bound to the current index."""
    global _searcher
    if _searcher is None:
        _searcher = RAGSearcher()  # may raise RuntimeError if index missing
    return _searcher


def on_build_index():
    """Build the FAISS index and reset the cached searcher."""
    try:
        msg = build_index()
        global _searcher
        _searcher = None  # force reload next time
    except Exception as e:
        msg = f"Error while building index: {e}"
    return msg


def _save_files(files: List[gr.File]) -> List[str]:
    """Save uploaded files (txt/pdf/html) to data/seed/."""
    saved = []
    DATA_SEED_DIR.mkdir(parents=True, exist_ok=True)
    if not files:
        return saved
    for f in files:
        try:
            path = None
            if isinstance(f, dict) and "name" in f:
                path = Path(f["name"])
            elif hasattr(f, "name"):
                path = Path(getattr(f, "name"))
            elif isinstance(f, str):
                path = Path(f)
            if not path or not path.exists():
                continue
            if path.suffix.lower() not in ALLOWED_EXTS:
                continue
            (DATA_SEED_DIR / path.name).write_bytes(path.read_bytes())
            saved.append(path.name)
        except Exception:
            continue
    return saved


def on_upload_and_reindex(files):
    """Save uploaded files then rebuild the index."""
    saved = _save_files(files or [])
    if not saved:
        return "No valid .txt/.pdf/.html uploaded."
    status = on_build_index()
    return f"Uploaded: {', '.join(saved)}\n{status}"


def on_ask(
    question: str,
    top_k: int,
    lang_code: str,
    mode_label: str,
    temp: float,
    max_tokens: int,
    history: list,
    session_id: str,   # NEW: State input
):
    """
    Handle a user question end-to-end:
    - Ensure index exists (auto-build on first use)
    - Retrieve top-k chunks
    - Synthesize answer (Extractive or Generative)
    - Show sources + extracted links/forms
    - Append to chat history
    - Return a structured "last answer" payload for feedback logging
    """
    lang = normalize_lang(lang_code)
    if not question or len(question.strip()) < 3:
        last_payload = {}
        return history, L(lang, "intro_err"), f"{L(lang, 'sources')}: (none)", "", "", last_payload

    try:
        # Ensure searcher; if index missing, build once automatically then retry
        try:
            searcher = ensure_searcher()
        except RuntimeError:
            on_build_index()
            searcher = ensure_searcher()

        hits = searcher.search(question, k=int(top_k))

        # Map UI label to internal mode flag
        mode = "generative" if mode_label.startswith("Generative") else "extractive"

        answer = synthesize_with_evo(
            question,
            lang,
            hits,
            mode=mode,
            max_new_tokens=int(max_tokens),
            temperature=float(temp),
        )

        # Localized "Sources" header
        srcs = format_sources(hits)
        if srcs.startswith("Sources:"):
            srcs = srcs.replace("Sources:", f"{L(lang, 'sources')}:")

        # Extract URLs and form links
        all_links = extract_links(hits)
        form_links, other_links = split_form_links(all_links)
        links_md = links_markdown(L(lang, "links"), other_links)
        forms_md = links_markdown(L(lang, "forms"), form_links)

        # Build "last answer" payload for feedback
        last_payload = {
            "ts_question": int(time.time()),
            "session": session_id or "",
            "lang": lang,
            "mode": mode,
            "temperature": float(temp),
            "max_tokens": int(max_tokens),
            "top_k": int(top_k),
            "question": question,
            "answer": answer,
            "sources": srcs,
            "links": links_md,
            "forms": forms_md,
        }

    except Exception as e:
        answer = f"Error: {e}\n\n{L(lang, 'tip_build')}"
        srcs = f"{L(lang, 'sources')}: (none)"
        links_md = f"**{L(lang, 'links')}:** (none)"
        forms_md = f"**{L(lang, 'forms')}:** (none)"
        last_payload = {}

    history = history + [(question, answer)]
    return history, answer, srcs, links_md, forms_md, last_payload


def on_feedback_submit(kind: str, note: str, last_payload: dict) -> str:
    """
    Log feedback to CSV. 'kind' is 'helpful' or 'not_helpful'.
    """
    if not last_payload:
        return "Ask a question first, then rate the answer."
    entry = dict(last_payload)  # copy
    entry["feedback"] = kind
    entry["note"] = note or ""
    return log_feedback(entry)


def _new_session_id():
    """Generate a short session id on app load."""
    return secrets.token_hex(8)


with gr.Blocks(title=L("en", "title")) as demo:
    gr.Markdown(f"# πŸ‡²πŸ‡Ί **{L('en','title')}** β€” Step 9")

    # --- Hidden state: session id + last answer payload
    session = gr.State()
    last_answer = gr.State()

    # Generate a session id when the app loads
    demo.load(fn=_new_session_id, inputs=None, outputs=session)

    # --- Admin: build index / upload & reindex
    with gr.Row():
        build_btn = gr.Button(L("en", "build_btn"))
        status = gr.Markdown(L("en", "status_idle"))
    build_btn.click(fn=on_build_index, outputs=status)

    with gr.Accordion("Admin: upload TXT / PDF / HTML and auto-reindex", open=False):
        upload = gr.File(
            file_count="multiple",
            file_types=["file"],
            label="Upload .txt / .pdf / .html",
        )
        save_btn = gr.Button("Save & Reindex")
        upload_status = gr.Markdown()
        save_btn.click(fn=on_upload_and_reindex, inputs=upload, outputs=upload_status)

    # --- User controls
    gr.Markdown("### Language / Langue / Langaz")
    lang = gr.Dropdown(choices=list(SUPPORTED.keys()), value="en", label="EN / FR / MFE")

    gr.Markdown("### Answer mode & controls")
    with gr.Row():
        mode = gr.Radio(
            choices=["Extractive (safe)", "Generative (Evo)"],
            value="Extractive (safe)",
            label="Mode",
        )
        temp = gr.Slider(0.0, 1.2, value=0.4, step=0.05, label="Temperature (gen)")
        max_tokens = gr.Slider(64, 384, value=192, step=16, label="Max new tokens (gen)")

    # --- Q&A
    gr.Markdown("### Ask a question / Posez une question / Poz enn kestyon")
    with gr.Row():
        q = gr.Textbox(placeholder=L("en", "ask_placeholder"), label=L("en", "ask_label"))
        k = gr.Slider(3, 12, step=1, value=6, label=L("en", "k_label"))
        ask_btn = gr.Button("Ask / Demander / Rode")

    chat = gr.Chatbot(label="Assistant", height=360)
    answer_md = gr.Markdown()
    sources_md = gr.Markdown()
    links_md = gr.Markdown()
    forms_md = gr.Markdown()

    ask_btn.click(
        fn=on_ask,
        inputs=[q, k, lang, mode, temp, max_tokens, chat, session],
        outputs=[chat, answer_md, sources_md, links_md, forms_md, last_answer],
    )
    q.submit(
        fn=on_ask,
        inputs=[q, k, lang, mode, temp, max_tokens, chat, session],
        outputs=[chat, answer_md, sources_md, links_md, forms_md, last_answer],
    )

    # --- Feedback UI
    gr.Markdown("### Rate this answer")
    with gr.Row():
        note = gr.Textbox(placeholder="Optional: tell us what was good or missing", label="Your note", lines=2)
    with gr.Row():
        helpful_btn = gr.Button("πŸ‘ Helpful")
        not_helpful_btn = gr.Button("πŸ‘Ž Not helpful")
        fb_status = gr.Markdown()

    helpful_btn.click(
        fn=lambda n, last: on_feedback_submit("helpful", n, last),
        inputs=[note, last_answer],
        outputs=[fb_status],
    )
    not_helpful_btn.click(
        fn=lambda n, last: on_feedback_submit("not_helpful", n, last),
        inputs=[note, last_answer],
        outputs=[fb_status],
    )

demo.launch()