Spaces:
Sleeping
Sleeping
Commit ·
bde046e
1
Parent(s): 67f3250
clear
Browse files
app.py
CHANGED
|
@@ -136,6 +136,14 @@ def answer(question: str, user_id="demo", history="None"):
|
|
| 136 |
finally:
|
| 137 |
torch.cuda.empty_cache()
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# ---- UI layout (feel free to tweak cosmetics) -----------------------------
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
gr.Markdown("### Tiny-RAG playground – 1) paste a passage → store 2) ask a question")
|
|
@@ -143,7 +151,13 @@ with gr.Blocks() as demo:
|
|
| 143 |
# ---- passage ingestion ----
|
| 144 |
with gr.Row():
|
| 145 |
passage_box = gr.Textbox(lines=6, label="Reference passage")
|
|
|
|
| 146 |
store_btn = gr.Button("Store passage")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
status_box = gr.Markdown()
|
| 148 |
store_btn.click(fn=store_doc,
|
| 149 |
inputs=passage_box,
|
|
@@ -151,7 +165,6 @@ with gr.Blocks() as demo:
|
|
| 151 |
|
| 152 |
# ---- Q & A ----
|
| 153 |
question_box = gr.Textbox(lines=2, label="Ask a question")
|
| 154 |
-
user_id_box = gr.Textbox(value="demo", label="User ID")
|
| 155 |
history_cb = gr.Textbox(value="None", label="Use chat history")
|
| 156 |
|
| 157 |
answer_btn = gr.Button("Answer")
|
|
@@ -203,15 +216,6 @@ def rag(req:QueryReq):
|
|
| 203 |
ans = full.split("<|im_start|>assistant")[-1].strip()
|
| 204 |
return {"answer": ans}
|
| 205 |
|
| 206 |
-
@api.post("/clear")
|
| 207 |
-
def clear_kb(user_id: str = "demo"):
|
| 208 |
-
if user_id in kb:
|
| 209 |
-
kb[user_id]["texts"].clear()
|
| 210 |
-
kb[user_id]["vecs"] = torch.empty((0, 4096))
|
| 211 |
-
return {"success": True, "message": f"Cleared KB for user '{user_id}'."}
|
| 212 |
-
else:
|
| 213 |
-
return {"success": False, "message": "User ID not found."}
|
| 214 |
-
|
| 215 |
# ---------- 5. run both (FastAPI + Gradio) -----------------------------------
|
| 216 |
if __name__ == "__main__":
|
| 217 |
# launch Gradio on a background thread
|
|
|
|
| 136 |
finally:
|
| 137 |
torch.cuda.empty_cache()
|
| 138 |
|
| 139 |
+
def clear_kb(user_id="demo"):
|
| 140 |
+
if user_id in kb:
|
| 141 |
+
kb[user_id]["texts"].clear()
|
| 142 |
+
kb[user_id]["vecs"] = torch.empty((0, 4096))
|
| 143 |
+
return f"Cleared KB for user '{user_id}'."
|
| 144 |
+
else:
|
| 145 |
+
return f"User ID '{user_id}' not found."
|
| 146 |
+
|
| 147 |
# ---- UI layout (feel free to tweak cosmetics) -----------------------------
|
| 148 |
with gr.Blocks() as demo:
|
| 149 |
gr.Markdown("### Tiny-RAG playground – 1) paste a passage → store 2) ask a question")
|
|
|
|
| 151 |
# ---- passage ingestion ----
|
| 152 |
with gr.Row():
|
| 153 |
passage_box = gr.Textbox(lines=6, label="Reference passage")
|
| 154 |
+
user_id_box = gr.Textbox(value="demo", label="User ID")
|
| 155 |
store_btn = gr.Button("Store passage")
|
| 156 |
+
clear_btn = gr.Button("Clear KB")
|
| 157 |
+
clear_status = gr.Markdown()
|
| 158 |
+
|
| 159 |
+
clear_btn.click(fn=clear_kb, inputs=user_id_box, outputs=clear_status)
|
| 160 |
+
|
| 161 |
status_box = gr.Markdown()
|
| 162 |
store_btn.click(fn=store_doc,
|
| 163 |
inputs=passage_box,
|
|
|
|
| 165 |
|
| 166 |
# ---- Q & A ----
|
| 167 |
question_box = gr.Textbox(lines=2, label="Ask a question")
|
|
|
|
| 168 |
history_cb = gr.Textbox(value="None", label="Use chat history")
|
| 169 |
|
| 170 |
answer_btn = gr.Button("Answer")
|
|
|
|
| 216 |
ans = full.split("<|im_start|>assistant")[-1].strip()
|
| 217 |
return {"answer": ans}
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
# ---------- 5. run both (FastAPI + Gradio) -----------------------------------
|
| 220 |
if __name__ == "__main__":
|
| 221 |
# launch Gradio on a background thread
|