Cyantist8208 commited on
Commit
eeca4c9
·
1 Parent(s): c4e0442

debug "answer()"

Browse files
Files changed (1) hide show
  1. app.py +31 -28
app.py CHANGED
@@ -132,43 +132,46 @@ def store_doc(doc_text: str, user_id="demo"):
132
  """UI callback: take the textbox content and shove it into the KB."""
133
  n = add_docs(user_id, [doc_text])
134
  if n == 0:
135
- return "⚠️ Nothing stored (empty input)."
136
- return f"📚 Stored — KB now has {len(kb[user_id]['texts'])} passage(s)."
137
 
138
  def answer(question: str, user_id="demo"):
139
  """UI callback: retrieve, build prompt with Qwen tags, generate answer."""
140
- if not question.strip():
141
- return "⚠️ Please ask a question."
142
- if not kb[user_id]["texts"]:
143
- return "⚠️ No reference passage yet. Add one first."
144
-
145
- # 1️⃣ Retrieve top-k similar passages
146
- q_vec = embed(question)
147
- store = kb[user_id]
148
- sims = torch.matmul(store["vecs"], q_vec) # [N]
149
- k = min(4, sims.numel())
150
- idxs = torch.topk(sims, k=k).indices.tolist()
151
- context = "\n".join(store["texts"][i] for i in idxs)
152
-
153
- # 2️⃣ Build a Qwen-chat prompt (helper defined earlier)
154
- prompt = build_qwen_prompt(context, question)
155
-
156
- # 3️⃣ Generate and strip everything before the assistant tag
157
- load_chat()
158
- inputs = tokenizer(prompt, return_tensors="pt").to(chat_model.device)
159
- output = chat_model.generate(**inputs, max_new_tokens=512)
160
- full = tokenizer.decode(output[0], skip_special_tokens=True)
161
- reply = full.split("<|im_start|>assistant")[-1].strip()
162
- return reply
 
 
 
163
 
164
  # ---- UI layout (feel free to tweak cosmetics) -----------------------------
165
  with gr.Blocks() as demo:
166
- gr.Markdown("### 📥 Tiny-RAG playground &nbsp;–&nbsp; 1) paste a passage → store&nbsp;&nbsp; 2) ask a question")
167
 
168
  # ---- passage ingestion ----
169
  with gr.Row():
170
  passage_box = gr.Textbox(lines=6, label="Reference passage")
171
- store_btn = gr.Button("Store passage")
172
  status_box = gr.Markdown()
173
  store_btn.click(fn=store_doc,
174
  inputs=passage_box,
@@ -176,7 +179,7 @@ with gr.Blocks() as demo:
176
 
177
  # ---- Q & A ----
178
  question_box = gr.Textbox(lines=2, label="Ask a question")
179
- answer_btn = gr.Button("🤖 Answer")
180
  answer_box = gr.Textbox(lines=6, label="Assistant reply")
181
 
182
  answer_btn.click(fn=answer,
 
132
  """UI callback: take the textbox content and shove it into the KB."""
133
  n = add_docs(user_id, [doc_text])
134
  if n == 0:
135
+ return "Nothing stored (empty input)."
136
+ return f"Stored — KB now has {len(kb[user_id]['texts'])} passage(s)."
137
 
138
  def answer(question: str, user_id="demo"):
139
  """UI callback: retrieve, build prompt with Qwen tags, generate answer."""
140
+ try:
141
+ if not question.strip():
142
+ return "Please ask a question."
143
+ if not kb[user_id]["texts"]:
144
+ return "No reference passage yet. Add one first."
145
+
146
+ # 1️⃣ Retrieve top-k similar passages
147
+ q_vec = embed(question)
148
+ store = kb[user_id]
149
+ sims = torch.matmul(store["vecs"], q_vec) # [N]
150
+ k = min(4, sims.numel())
151
+ idxs = torch.topk(sims, k=k).indices.tolist()
152
+ context = "\n".join(store["texts"][i] for i in idxs)
153
+
154
+ # 2️⃣ Build a Qwen-chat prompt (helper defined earlier)
155
+ prompt = build_qwen_prompt(context, question)
156
+
157
+ # 3️⃣ Generate and strip everything before the assistant tag
158
+ load_chat()
159
+ inputs = tokenizer(prompt, return_tensors="pt").to(chat_model.device)
160
+ output = chat_model.generate(**inputs, max_new_tokens=512)
161
+ full = tokenizer.decode(output[0], skip_special_tokens=True)
162
+ reply = full.split("<|im_start|>assistant")[-1].strip()
163
+ return reply
164
+ except Exception as e:
165
+ return f"Error in app.py: {e}"
166
 
167
  # ---- UI layout (feel free to tweak cosmetics) -----------------------------
168
  with gr.Blocks() as demo:
169
+ gr.Markdown("### Tiny-RAG playground &nbsp;–&nbsp; 1) paste a passage → store&nbsp;&nbsp; 2) ask a question")
170
 
171
  # ---- passage ingestion ----
172
  with gr.Row():
173
  passage_box = gr.Textbox(lines=6, label="Reference passage")
174
+ store_btn = gr.Button("Store passage")
175
  status_box = gr.Markdown()
176
  store_btn.click(fn=store_doc,
177
  inputs=passage_box,
 
179
 
180
  # ---- Q & A ----
181
  question_box = gr.Textbox(lines=2, label="Ask a question")
182
+ answer_btn = gr.Button("Answer")
183
  answer_box = gr.Textbox(lines=6, label="Assistant reply")
184
 
185
  answer_btn.click(fn=answer,