Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
"""
|
2 |
app.py
|
3 |
-
Step
|
4 |
|
5 |
-
(Objective)
|
6 |
-
-
|
7 |
-
-
|
8 |
-
-
|
9 |
"""
|
10 |
|
11 |
from pathlib import Path
|
@@ -26,7 +26,7 @@ from utils_lang import SUPPORTED, normalize_lang, L
|
|
26 |
|
27 |
_searcher = None
|
28 |
DATA_SEED_DIR = Path("data/seed")
|
29 |
-
ALLOWED_EXTS = {".txt", ".pdf", ".html", ".htm", ".xhtml"}
|
30 |
|
31 |
|
32 |
def ensure_searcher():
|
@@ -47,10 +47,6 @@ def on_build_index():
|
|
47 |
|
48 |
|
49 |
def _save_files(files: List[gr.File]) -> List[str]:
|
50 |
-
"""
|
51 |
-
(Objective) Save uploaded files with allowed extensions into data/seed/.
|
52 |
-
Supports different Gradio file object shapes.
|
53 |
-
"""
|
54 |
saved = []
|
55 |
DATA_SEED_DIR.mkdir(parents=True, exist_ok=True)
|
56 |
if not files:
|
@@ -58,7 +54,6 @@ def _save_files(files: List[gr.File]) -> List[str]:
|
|
58 |
|
59 |
for f in files:
|
60 |
try:
|
61 |
-
# Resolve a path from the gradio file object
|
62 |
path = None
|
63 |
if isinstance(f, dict) and "name" in f:
|
64 |
path = Path(f["name"])
|
@@ -68,10 +63,8 @@ def _save_files(files: List[gr.File]) -> List[str]:
|
|
68 |
path = Path(f)
|
69 |
if not path or not path.exists():
|
70 |
continue
|
71 |
-
|
72 |
if path.suffix.lower() not in ALLOWED_EXTS:
|
73 |
continue
|
74 |
-
|
75 |
dest = DATA_SEED_DIR / path.name
|
76 |
dest.write_bytes(path.read_bytes())
|
77 |
saved.append(path.name)
|
@@ -88,78 +81,11 @@ def on_upload_and_reindex(files) -> str:
|
|
88 |
return f"Uploaded: {', '.join(saved)}\n{status}"
|
89 |
|
90 |
|
91 |
-
def on_ask(question: str, top_k: int, lang_code: str, history: list):
|
92 |
lang = normalize_lang(lang_code)
|
93 |
if not question or len(question.strip()) < 3:
|
94 |
return history, L(lang, "intro_err"), f"{L(lang, 'sources')}: (none)", "", ""
|
95 |
|
96 |
try:
|
97 |
searcher = ensure_searcher()
|
98 |
-
hits = searcher.search(
|
99 |
-
answer = synthesize_with_evo(question, lang, hits)
|
100 |
-
|
101 |
-
srcs = format_sources(hits)
|
102 |
-
if srcs.startswith("Sources:"):
|
103 |
-
srcs = srcs.replace("Sources:", f"{L(lang, 'sources')}:")
|
104 |
-
|
105 |
-
all_links = extract_links(hits)
|
106 |
-
form_links, other_links = split_form_links(all_links)
|
107 |
-
links_md = links_markdown(L(lang, "links"), other_links)
|
108 |
-
forms_md = links_markdown(L(lang, "forms"), form_links)
|
109 |
-
|
110 |
-
except Exception as e:
|
111 |
-
answer = f"Error: {e}\n\n{L(lang, 'tip_build')}"
|
112 |
-
srcs = f"{L(lang, 'sources')}: (none)"
|
113 |
-
links_md = f"**{L(lang, 'links')}:** (none)"
|
114 |
-
forms_md = f"**{L(lang, 'forms')}:** (none)"
|
115 |
-
|
116 |
-
history = history + [(question, answer)]
|
117 |
-
return history, answer, srcs, links_md, forms_md
|
118 |
-
|
119 |
-
|
120 |
-
with gr.Blocks(title=L("en", "title")) as demo:
|
121 |
-
gr.Markdown(f"# 🇲🇺 **{L('en','title')}** — Step 7")
|
122 |
-
|
123 |
-
with gr.Row():
|
124 |
-
build_btn = gr.Button(L("en", "build_btn"))
|
125 |
-
status = gr.Markdown(L("en", "status_idle"))
|
126 |
-
build_btn.click(fn=on_build_index, outputs=status)
|
127 |
-
|
128 |
-
# Admin: upload txt/pdf/html, then reindex
|
129 |
-
with gr.Accordion("Admin: upload TXT / PDF / HTML and auto-reindex", open=False):
|
130 |
-
upload = gr.File(
|
131 |
-
file_count="multiple",
|
132 |
-
file_types=["file"], # allow any file; we'll filter by extension
|
133 |
-
label="Upload .txt / .pdf / .html"
|
134 |
-
)
|
135 |
-
save_btn = gr.Button("Save & Reindex")
|
136 |
-
upload_status = gr.Markdown()
|
137 |
-
save_btn.click(fn=on_upload_and_reindex, inputs=upload, outputs=upload_status)
|
138 |
-
|
139 |
-
gr.Markdown("### Language / Langue / Langaz")
|
140 |
-
lang = gr.Dropdown(choices=list(SUPPORTED.keys()), value="en", label="EN / FR / MFE")
|
141 |
-
|
142 |
-
gr.Markdown("### Ask a question / Posez une question / Poz enn kestyon")
|
143 |
-
with gr.Row():
|
144 |
-
q = gr.Textbox(placeholder=L("en", "ask_placeholder"), label=L("en", "ask_label"))
|
145 |
-
k = gr.Slider(3, 12, step=1, value=6, label=L("en", "k_label"))
|
146 |
-
ask_btn = gr.Button("Ask / Demander / Rode")
|
147 |
-
|
148 |
-
chat = gr.Chatbot(label="Assistant", height=360)
|
149 |
-
answer_md = gr.Markdown()
|
150 |
-
sources_md = gr.Markdown()
|
151 |
-
links_md = gr.Markdown()
|
152 |
-
forms_md = gr.Markdown()
|
153 |
-
|
154 |
-
ask_btn.click(
|
155 |
-
fn=on_ask,
|
156 |
-
inputs=[q, k, lang, chat],
|
157 |
-
outputs=[chat, answer_md, sources_md, links_md, forms_md],
|
158 |
-
)
|
159 |
-
q.submit(
|
160 |
-
fn=on_ask,
|
161 |
-
inputs=[q, k, lang, chat],
|
162 |
-
outputs=[chat, answer_md, sources_md, links_md, forms_md],
|
163 |
-
)
|
164 |
-
|
165 |
-
demo.launch()
|
|
|
1 |
"""
|
2 |
app.py
|
3 |
+
Step 8: Add generative mode controls and pass them to Evo synthesis.
|
4 |
|
5 |
+
New (Objective):
|
6 |
+
- Radio to choose "Extractive (safe)" vs "Generative (Evo)".
|
7 |
+
- Sliders for temperature and max tokens.
|
8 |
+
- We pass these to synthesize_with_evo().
|
9 |
"""
|
10 |
|
11 |
from pathlib import Path
|
|
|
26 |
|
27 |
_searcher = None
|
28 |
DATA_SEED_DIR = Path("data/seed")
|
29 |
+
ALLOWED_EXTS = {".txt", ".pdf", ".html", ".htm", ".xhtml"}
|
30 |
|
31 |
|
32 |
def ensure_searcher():
|
|
|
47 |
|
48 |
|
49 |
def _save_files(files: List[gr.File]) -> List[str]:
|
|
|
|
|
|
|
|
|
50 |
saved = []
|
51 |
DATA_SEED_DIR.mkdir(parents=True, exist_ok=True)
|
52 |
if not files:
|
|
|
54 |
|
55 |
for f in files:
|
56 |
try:
|
|
|
57 |
path = None
|
58 |
if isinstance(f, dict) and "name" in f:
|
59 |
path = Path(f["name"])
|
|
|
63 |
path = Path(f)
|
64 |
if not path or not path.exists():
|
65 |
continue
|
|
|
66 |
if path.suffix.lower() not in ALLOWED_EXTS:
|
67 |
continue
|
|
|
68 |
dest = DATA_SEED_DIR / path.name
|
69 |
dest.write_bytes(path.read_bytes())
|
70 |
saved.append(path.name)
|
|
|
81 |
return f"Uploaded: {', '.join(saved)}\n{status}"
|
82 |
|
83 |
|
84 |
+
def on_ask(question: str, top_k: int, lang_code: str, mode: str, temp: float, max_tokens: int, history: list):
|
85 |
lang = normalize_lang(lang_code)
|
86 |
if not question or len(question.strip()) < 3:
|
87 |
return history, L(lang, "intro_err"), f"{L(lang, 'sources')}: (none)", "", ""
|
88 |
|
89 |
try:
|
90 |
searcher = ensure_searcher()
|
91 |
+
hits = searcher.search(qu
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|