HemanM commited on
Commit
0c48a7c
Β·
verified Β·
1 Parent(s): 45789c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -1,7 +1,7 @@
1
  """
2
- app.py β€” Step 7
3
- - Admin can upload .txt/.pdf/.html to data/seed/ and reindex
4
- - Q&A uses retrieval + Evo templated synthesis from earlier steps
5
  """
6
 
7
  from pathlib import Path
@@ -41,8 +41,7 @@ def _save_files(files: List[gr.File]) -> List[str]:
41
  elif isinstance(f, str): path = Path(f)
42
  if not path or not path.exists(): continue
43
  if path.suffix.lower() not in ALLOWED_EXTS: continue
44
- dest = DATA_SEED_DIR / path.name
45
- dest.write_bytes(path.read_bytes())
46
  saved.append(path.name)
47
  except: continue
48
  return saved
@@ -53,14 +52,23 @@ def on_upload_and_reindex(files):
53
  status = on_build_index()
54
  return f"Uploaded: {', '.join(saved)}\n{status}"
55
 
56
- def on_ask(question: str, top_k: int, lang_code: str, history: list):
57
  lang = normalize_lang(lang_code)
58
  if not question or len(question.strip()) < 3:
59
  return history, L(lang,"intro_err"), f"{L(lang,'sources')}: (none)", "", ""
60
  try:
61
  searcher = ensure_searcher()
62
  hits = searcher.search(question, k=int(top_k))
63
- answer = synthesize_with_evo(question, lang, hits) # templated synthesis (Step 5)
 
 
 
 
 
 
 
 
 
64
  srcs = format_sources(hits)
65
  if srcs.startswith("Sources:"): srcs = srcs.replace("Sources:", f"{L(lang,'sources')}:")
66
  all_links = extract_links(hits)
@@ -76,8 +84,9 @@ def on_ask(question: str, top_k: int, lang_code: str, history: list):
76
  return history, answer, srcs, links_md, forms_md
77
 
78
  with gr.Blocks(title=L("en","title")) as demo:
79
- gr.Markdown(f"# πŸ‡²πŸ‡Ί **{L('en','title')}** β€” Step 7")
80
 
 
81
  with gr.Row():
82
  build_btn = gr.Button(L("en","build_btn"))
83
  status = gr.Markdown(L("en","status_idle"))
@@ -89,9 +98,20 @@ with gr.Blocks(title=L("en","title")) as demo:
89
  upload_status = gr.Markdown()
90
  save_btn.click(fn=on_upload_and_reindex, inputs=upload, outputs=upload_status)
91
 
 
92
  gr.Markdown("### Language / Langue / Langaz")
93
  lang = gr.Dropdown(choices=list(SUPPORTED.keys()), value="en", label="EN / FR / MFE")
94
 
 
 
 
 
 
 
 
 
 
 
95
  gr.Markdown("### Ask a question / Posez une question / Poz enn kestyon")
96
  with gr.Row():
97
  q = gr.Textbox(placeholder=L("en","ask_placeholder"), label=L("en","ask_label"))
@@ -104,7 +124,15 @@ with gr.Blocks(title=L("en","title")) as demo:
104
  links_md = gr.Markdown()
105
  forms_md = gr.Markdown()
106
 
107
- ask_btn.click(fn=on_ask, inputs=[q, k, lang, chat], outputs=[chat, answer_md, sources_md, links_md, forms_md])
108
- q.submit(fn=on_ask, inputs=[q, k, lang, chat], outputs=[chat, answer_md, sources_md, links_md, forms_md])
 
 
 
 
 
 
 
 
109
 
110
  demo.launch()
 
1
  """
2
+ app.py β€” Step 8
3
+ Adds a simple toggle to choose Extractive (safe) or Generative (Evo),
4
+ plus temperature and max_tokens controls.
5
  """
6
 
7
  from pathlib import Path
 
41
  elif isinstance(f, str): path = Path(f)
42
  if not path or not path.exists(): continue
43
  if path.suffix.lower() not in ALLOWED_EXTS: continue
44
+ (DATA_SEED_DIR / path.name).write_bytes(path.read_bytes())
 
45
  saved.append(path.name)
46
  except: continue
47
  return saved
 
52
  status = on_build_index()
53
  return f"Uploaded: {', '.join(saved)}\n{status}"
54
 
55
+ def on_ask(question: str, top_k: int, lang_code: str, mode_label: str, temp: float, max_tokens: int, history: list):
56
  lang = normalize_lang(lang_code)
57
  if not question or len(question.strip()) < 3:
58
  return history, L(lang,"intro_err"), f"{L(lang,'sources')}: (none)", "", ""
59
  try:
60
  searcher = ensure_searcher()
61
  hits = searcher.search(question, k=int(top_k))
62
+ # Map UI label to internal arg
63
+ mode = "generative" if mode_label.startswith("Generative") else "extractive"
64
+
65
+ answer = synthesize_with_evo(
66
+ question, lang, hits,
67
+ mode=mode,
68
+ max_new_tokens=int(max_tokens),
69
+ temperature=float(temp),
70
+ )
71
+
72
  srcs = format_sources(hits)
73
  if srcs.startswith("Sources:"): srcs = srcs.replace("Sources:", f"{L(lang,'sources')}:")
74
  all_links = extract_links(hits)
 
84
  return history, answer, srcs, links_md, forms_md
85
 
86
  with gr.Blocks(title=L("en","title")) as demo:
87
+ gr.Markdown(f"# πŸ‡²πŸ‡Ί **{L('en','title')}** β€” Step 8")
88
 
89
+ # Admin
90
  with gr.Row():
91
  build_btn = gr.Button(L("en","build_btn"))
92
  status = gr.Markdown(L("en","status_idle"))
 
98
  upload_status = gr.Markdown()
99
  save_btn.click(fn=on_upload_and_reindex, inputs=upload, outputs=upload_status)
100
 
101
+ # User controls
102
  gr.Markdown("### Language / Langue / Langaz")
103
  lang = gr.Dropdown(choices=list(SUPPORTED.keys()), value="en", label="EN / FR / MFE")
104
 
105
+ gr.Markdown("### Answer mode & controls")
106
+ with gr.Row():
107
+ mode = gr.Radio(
108
+ choices=["Extractive (safe)", "Generative (Evo)"],
109
+ value="Extractive (safe)",
110
+ label="Mode"
111
+ )
112
+ temp = gr.Slider(0.0, 1.2, value=0.4, step=0.05, label="Temperature (gen)")
113
+ max_tokens = gr.Slider(64, 384, value=192, step=16, label="Max new tokens (gen)")
114
+
115
  gr.Markdown("### Ask a question / Posez une question / Poz enn kestyon")
116
  with gr.Row():
117
  q = gr.Textbox(placeholder=L("en","ask_placeholder"), label=L("en","ask_label"))
 
124
  links_md = gr.Markdown()
125
  forms_md = gr.Markdown()
126
 
127
+ ask_btn.click(
128
+ fn=on_ask,
129
+ inputs=[q, k, lang, mode, temp, max_tokens, chat],
130
+ outputs=[chat, answer_md, sources_md, links_md, forms_md],
131
+ )
132
+ q.submit(
133
+ fn=on_ask,
134
+ inputs=[q, k, lang, mode, temp, max_tokens, chat],
135
+ outputs=[chat, answer_md, sources_md, links_md, forms_md],
136
+ )
137
 
138
  demo.launch()