Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,98 +1,92 @@
|
|
1 |
"""
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
-
|
7 |
-
|
8 |
-
|
9 |
-
- We lazy-load the searcher when the user first asks a question
|
10 |
"""
|
11 |
|
12 |
import gradio as gr
|
13 |
-
|
14 |
from indexer import build_index
|
15 |
-
from rag_search import RAGSearcher,
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
# We'll instantiate the searcher lazily so the app can boot even if the index isn't built yet.
|
18 |
-
_searcher = None
|
19 |
|
20 |
def ensure_searcher():
|
21 |
-
"""
|
22 |
-
Create the global RAGSearcher if not present.
|
23 |
-
If the index doesn't exist yet, raise a clear exception to show in the UI.
|
24 |
-
"""
|
25 |
global _searcher
|
26 |
if _searcher is None:
|
27 |
_searcher = RAGSearcher()
|
28 |
return _searcher
|
29 |
|
|
|
30 |
def on_build_index():
|
31 |
-
"""
|
32 |
-
Build/refresh the FAISS index from TXT files.
|
33 |
-
"""
|
34 |
try:
|
35 |
msg = build_index()
|
36 |
-
# Reset searcher so a new one is created against the fresh index
|
37 |
global _searcher
|
38 |
-
_searcher = None
|
39 |
except Exception as e:
|
40 |
msg = f"Error while building index: {e}"
|
41 |
return msg
|
42 |
|
43 |
-
|
|
|
44 |
"""
|
45 |
-
Handle
|
46 |
-
-
|
47 |
-
-
|
48 |
-
-
|
49 |
-
-
|
50 |
"""
|
|
|
51 |
if not question or len(question.strip()) < 3:
|
52 |
-
return history,
|
53 |
|
54 |
try:
|
55 |
searcher = ensure_searcher()
|
56 |
hits = searcher.search(question, k=int(top_k))
|
57 |
-
answer =
|
58 |
-
|
|
|
|
|
|
|
59 |
except Exception as e:
|
60 |
-
|
61 |
-
|
62 |
-
sources = "Sources: (none)"
|
63 |
|
64 |
-
# Update the chat UI (user message + assistant reply)
|
65 |
history = history + [(question, answer)]
|
66 |
-
return history, answer,
|
67 |
|
68 |
|
69 |
-
with gr.Blocks(title="
|
70 |
-
|
71 |
-
|
72 |
-
# π²πΊ Evo Government Copilot (Mauritius) β Step 4
|
73 |
-
**Now live:** Ask a question β we retrieve top chunks β show a concise answer + sources.
|
74 |
-
|
75 |
-
β
TXT indexing from Step 3
|
76 |
-
β
Retrieval + QβA (extractive)
|
77 |
-
βοΈ Next: Evo synthesis + languages (EN/FR/Kreol), links & forms
|
78 |
-
"""
|
79 |
-
)
|
80 |
|
81 |
with gr.Row():
|
82 |
-
build_btn = gr.Button("
|
83 |
-
status = gr.Markdown("
|
84 |
build_btn.click(fn=on_build_index, outputs=status)
|
85 |
|
86 |
-
gr.Markdown("###
|
|
|
|
|
|
|
87 |
with gr.Row():
|
88 |
-
q = gr.Textbox(placeholder="
|
89 |
-
k = gr.Slider(3, 12, step=1, value=6, label="
|
90 |
-
ask_btn = gr.Button("Ask")
|
|
|
91 |
chat = gr.Chatbot(label="Assistant", height=360)
|
92 |
answer_md = gr.Markdown()
|
93 |
sources_md = gr.Markdown()
|
94 |
|
95 |
-
ask_btn.click(fn=on_ask, inputs=[q, k, chat], outputs=[chat, answer_md, sources_md])
|
96 |
-
q.submit(fn=on_ask, inputs=[q, k, chat], outputs=[chat, answer_md, sources_md])
|
97 |
|
98 |
demo.launch()
|
|
|
1 |
"""
|
2 |
+
app.py
|
3 |
+
Step 5: Language selector + Evo synthesis hook.
|
4 |
+
|
5 |
+
What changed (Objective):
|
6 |
+
- Added a language dropdown (EN/FR/Kreol)
|
7 |
+
- Now we call `synthesize_with_evo()` to build the answer
|
8 |
+
- Localized labels via utils_lang.L()
|
|
|
9 |
"""
|
10 |
|
11 |
import gradio as gr
|
|
|
12 |
from indexer import build_index
|
13 |
+
from rag_search import RAGSearcher, format_sources
|
14 |
+
from evo_inference import synthesize_with_evo
|
15 |
+
from utils_lang import SUPPORTED, normalize_lang, L
|
16 |
+
|
17 |
+
_searcher = None # (Objective) Lazy global searcher
|
18 |
|
|
|
|
|
19 |
|
20 |
def ensure_searcher():
|
21 |
+
"""(Objective) Create the global RAGSearcher if needed."""
|
|
|
|
|
|
|
22 |
global _searcher
|
23 |
if _searcher is None:
|
24 |
_searcher = RAGSearcher()
|
25 |
return _searcher
|
26 |
|
27 |
+
|
28 |
def on_build_index():
|
29 |
+
"""(Objective) Build FAISS index, reset the searcher."""
|
|
|
|
|
30 |
try:
|
31 |
msg = build_index()
|
|
|
32 |
global _searcher
|
33 |
+
_searcher = None # force reload with fresh index
|
34 |
except Exception as e:
|
35 |
msg = f"Error while building index: {e}"
|
36 |
return msg
|
37 |
|
38 |
+
|
39 |
+
def on_ask(question: str, top_k: int, lang_code: str, history: list):
|
40 |
"""
|
41 |
+
(Objective) Handle user question:
|
42 |
+
- ensure index
|
43 |
+
- retrieve top-k hits
|
44 |
+
- synthesize Evo-style answer (templated for now)
|
45 |
+
- show localized Sources label
|
46 |
"""
|
47 |
+
lang = normalize_lang(lang_code)
|
48 |
if not question or len(question.strip()) < 3:
|
49 |
+
return history, L(lang, "intro_err"), f"{L(lang, 'sources')}: (none)"
|
50 |
|
51 |
try:
|
52 |
searcher = ensure_searcher()
|
53 |
hits = searcher.search(question, k=int(top_k))
|
54 |
+
answer = synthesize_with_evo(question, lang, hits)
|
55 |
+
srcs = format_sources(hits)
|
56 |
+
# Localize the "Sources" header by replacing it if needed
|
57 |
+
if srcs.startswith("Sources:"):
|
58 |
+
srcs = srcs.replace("Sources:", f"{L(lang, 'sources')}:")
|
59 |
except Exception as e:
|
60 |
+
answer = f"Error: {e}\n\n{L(lang, 'tip_build')}"
|
61 |
+
srcs = f"{L(lang, 'sources')}: (none)"
|
|
|
62 |
|
|
|
63 |
history = history + [(question, answer)]
|
64 |
+
return history, answer, srcs
|
65 |
|
66 |
|
67 |
+
with gr.Blocks(title=L("en", "title")) as demo:
|
68 |
+
# Note: We render static English title in metadata; inside the page we localize fully.
|
69 |
+
gr.Markdown(f"# π²πΊ **{L('en','title')}** β Step 5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Row():
|
72 |
+
build_btn = gr.Button(L("en", "build_btn"))
|
73 |
+
status = gr.Markdown(L("en", "status_idle"))
|
74 |
build_btn.click(fn=on_build_index, outputs=status)
|
75 |
|
76 |
+
gr.Markdown("### Language / Langue / Langaz")
|
77 |
+
lang = gr.Dropdown(choices=list(SUPPORTED.keys()), value="en", label="EN / FR / MFE")
|
78 |
+
|
79 |
+
gr.Markdown("### Ask a question / Posez une question / Poz enn kestyon")
|
80 |
with gr.Row():
|
81 |
+
q = gr.Textbox(placeholder=L("en", "ask_placeholder"), label=L("en", "ask_label"))
|
82 |
+
k = gr.Slider(3, 12, step=1, value=6, label=L("en", "k_label"))
|
83 |
+
ask_btn = gr.Button("Ask / Demander / Rode")
|
84 |
+
|
85 |
chat = gr.Chatbot(label="Assistant", height=360)
|
86 |
answer_md = gr.Markdown()
|
87 |
sources_md = gr.Markdown()
|
88 |
|
89 |
+
ask_btn.click(fn=on_ask, inputs=[q, k, lang, chat], outputs=[chat, answer_md, sources_md])
|
90 |
+
q.submit(fn=on_ask, inputs=[q, k, lang, chat], outputs=[chat, answer_md, sources_md])
|
91 |
|
92 |
demo.launch()
|