Cyantist8208 commited on
Commit
e53b387
·
1 Parent(s): 905b1f8
Files changed (2) hide show
  1. app.py +9 -4
  2. requirements.txt +2 -1
app.py CHANGED
@@ -109,6 +109,7 @@ def store_doc(doc_text: str, user_id="demo"):
109
  except Exception as e:
110
  return f"Error during storing: {e}"
111
 
 
112
  def answer(system: str, context: str, question: str, user_id="demo", history="None"):
113
  """UI callback: retrieve, build prompt with Qwen tags, generate answer."""
114
  try:
@@ -122,10 +123,13 @@ def answer(system: str, context: str, question: str, user_id="demo", history="No
122
  if history == "Some":
123
  q_vec = embed(question).view(-1).cpu()
124
  store = kb[user_id]
125
- sims = torch.matmul(store["vecs"], q_vec) # [N]
126
- if sims.shape[1] == 1:
 
 
 
127
  sims = sims.squeeze(1)
128
- k = min(4, store["vecs"].size(0))
129
  idxs = torch.topk(sims, k=k, dim=0).indices.tolist()
130
  context_list += [store["texts"][i] for i in idxs]
131
  elif history == "All":
@@ -160,7 +164,8 @@ def answer(system: str, context: str, question: str, user_id="demo", history="No
160
 
161
 
162
  except Exception as e:
163
- return f"Error in app.py: {e}, k={k}, sims.numel()={sims.numel()}, sims.shape={sims.shape if 'q_vec' in locals() else 'N/A'}"
 
164
  finally:
165
  torch.cuda.empty_cache()
166
 
 
109
  except Exception as e:
110
  return f"Error during storing: {e}"
111
 
112
+ import traceback
113
  def answer(system: str, context: str, question: str, user_id="demo", history="None"):
114
  """UI callback: retrieve, build prompt with Qwen tags, generate answer."""
115
  try:
 
123
  if history == "Some":
124
  q_vec = embed(question).view(-1).cpu()
125
  store = kb[user_id]
126
+ vecs = store["vecs"]
127
+ if vecs is None or vecs.size(0) == 0:
128
+ return "Knowledge base is empty or corrupted."
129
+ sims = torch.matmul(vecs, q_vec) # [N]
130
+ if sims.dim() > 1:
131
  sims = sims.squeeze(1)
132
+ k = min(4, sims.size(0))
133
  idxs = torch.topk(sims, k=k, dim=0).indices.tolist()
134
  context_list += [store["texts"][i] for i in idxs]
135
  elif history == "All":
 
164
 
165
 
166
  except Exception as e:
167
+ tb = traceback.format_exc()
168
+ return f"Error in app.py: {tb}, k={k}, sims.numel()={sims.numel()}, sims.shape={sims.shape if 'q_vec' in locals() else 'N/A'}"
169
  finally:
170
  torch.cuda.empty_cache()
171
 
requirements.txt CHANGED
@@ -4,4 +4,5 @@ sentencepiece
4
  accelerate
5
  uvicorn
6
  fastapi
7
- pydantic
 
 
4
  accelerate
5
  uvicorn
6
  fastapi
7
+ pydantic
8
+ traceback