HemanM commited on
Commit
57774c2
Β·
verified Β·
1 Parent(s): 2a79ea8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -54
app.py CHANGED
@@ -1,98 +1,92 @@
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()
 
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()