Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,98 @@
|
|
1 |
"""
|
2 |
-
Step
|
3 |
|
4 |
-
What changed
|
5 |
-
- We import
|
6 |
-
- We add a
|
7 |
-
-
|
|
|
|
|
8 |
"""
|
9 |
|
10 |
import gradio as gr
|
11 |
-
from indexer import build_index # (Objective) Our Step 3 function
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def on_build_index():
|
15 |
"""
|
16 |
-
|
17 |
-
(Objective) Called when the user clicks the button.
|
18 |
"""
|
19 |
try:
|
20 |
msg = build_index()
|
|
|
|
|
|
|
21 |
except Exception as e:
|
22 |
msg = f"Error while building index: {e}"
|
23 |
return msg
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
gr.Markdown(
|
27 |
"""
|
28 |
-
# π²πΊ Evo Government Copilot (Mauritius) β Step
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
- We add retrieval and answers in the next step.
|
35 |
"""
|
36 |
)
|
37 |
|
38 |
with gr.Row():
|
39 |
build_btn = gr.Button("π§ Build/Refresh Index (TXT)")
|
40 |
-
|
41 |
-
|
42 |
build_btn.click(fn=on_build_index, outputs=status)
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
demo.launch()
|
|
|
1 |
"""
|
2 |
+
Step 4: Minimal QβA using the FAISS index.
|
3 |
|
4 |
+
What changed vs Step 3:
|
5 |
+
- We import RAGSearcher, summarize_hits, format_sources from rag_search.py
|
6 |
+
- We add a question input + "Ask" button and display:
|
7 |
+
- an extractive-style answer (placeholder)
|
8 |
+
- a "Sources" section showing which files informed the answer
|
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, summarize_hits, format_sources
|
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 |
+
def on_ask(question: str, top_k: int, history: list):
|
44 |
+
"""
|
45 |
+
Handle a user question:
|
46 |
+
- Ensure the index is available
|
47 |
+
- Retrieve top-k chunks
|
48 |
+
- Produce a concise extractive answer + sources (Step 5 will swap in Evo)
|
49 |
+
- Append to the chat history
|
50 |
+
"""
|
51 |
+
if not question or len(question.strip()) < 3:
|
52 |
+
return history, "Please enter a question (at least 3 characters).", "Sources: (none)"
|
53 |
+
|
54 |
+
try:
|
55 |
+
searcher = ensure_searcher()
|
56 |
+
hits = searcher.search(question, k=int(top_k))
|
57 |
+
answer = summarize_hits(hits)
|
58 |
+
sources = format_sources(hits)
|
59 |
+
except Exception as e:
|
60 |
+
# Most common cause: index not built yet
|
61 |
+
answer = f"Error: {e}\n\nTip: Click 'Build/Refresh Index' first."
|
62 |
+
sources = "Sources: (none)"
|
63 |
+
|
64 |
+
# Update the chat UI (user message + assistant reply)
|
65 |
+
history = history + [(question, answer)]
|
66 |
+
return history, answer, sources
|
67 |
+
|
68 |
+
|
69 |
+
with gr.Blocks(title="Evo Government Copilot (MU) - Step 4") as demo:
|
70 |
gr.Markdown(
|
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("π§ Build/Refresh Index (TXT)")
|
83 |
+
status = gr.Markdown("Status: _idle_")
|
|
|
84 |
build_btn.click(fn=on_build_index, outputs=status)
|
85 |
|
86 |
+
gr.Markdown("### Ask a question")
|
87 |
+
with gr.Row():
|
88 |
+
q = gr.Textbox(placeholder="e.g., How do I renew my Mauritius passport?", label="Your question")
|
89 |
+
k = gr.Slider(3, 12, step=1, value=6, label="Context depth (top-k)")
|
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()
|