Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -259,89 +259,89 @@ class RAGPipeline:
|
|
259 |
# return message
|
260 |
|
261 |
def process_query(self, query: str, placeholder) -> str:
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
|
280 |
-
|
281 |
|
282 |
-
|
283 |
-
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
|
300 |
-
|
301 |
|
302 |
-
|
303 |
-
|
304 |
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
|
319 |
-
|
320 |
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
|
328 |
-
|
329 |
-
|
330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
|
332 |
except Exception as e:
|
333 |
-
logging.error(f"
|
334 |
logging.error(f"Full error details: ", exc_info=True)
|
335 |
-
message = f"
|
336 |
-
|
337 |
return message
|
338 |
-
|
339 |
-
except Exception as e:
|
340 |
-
logging.error(f"Process error: {str(e)}")
|
341 |
-
logging.error(f"Full error details: ", exc_info=True)
|
342 |
-
message = f"Something went wrong: {str(e)}"
|
343 |
-
placeholder.warning(message)
|
344 |
-
return message
|
345 |
|
346 |
@st.cache_resource(show_spinner=False)
|
347 |
def initialize_rag_pipeline():
|
|
|
259 |
# return message
|
260 |
|
261 |
def process_query(self, query: str, placeholder) -> str:
|
262 |
+
try:
|
263 |
+
# Preprocess query
|
264 |
+
query = self.preprocess_query(query)
|
265 |
+
logging.info(f"Processing query: {query}")
|
266 |
|
267 |
+
# Show retrieval status
|
268 |
+
status = placeholder.empty()
|
269 |
+
status.write("π Finding relevant information...")
|
270 |
|
271 |
+
# Get embeddings and search
|
272 |
+
query_embedding = self.retriever.encode([query])
|
273 |
+
similarities = F.cosine_similarity(query_embedding, self.retriever.doc_embeddings)
|
274 |
+
scores, indices = torch.topk(similarities, k=min(self.k, len(self.documents)))
|
275 |
|
276 |
+
# Log similarity scores
|
277 |
+
for idx, score in zip(indices.tolist(), scores.tolist()):
|
278 |
+
logging.info(f"Score: {score:.4f} | Document: {self.documents[idx][:100]}...")
|
279 |
|
280 |
+
relevant_docs = [self.documents[idx] for idx in indices.tolist()]
|
281 |
|
282 |
+
# Update status
|
283 |
+
status.write("π Generating response...")
|
284 |
|
285 |
+
# Prepare context and prompt
|
286 |
+
context = "\n".join(relevant_docs[:3])
|
287 |
+
prompt = f"""Context information is below:
|
288 |
+
{context}
|
289 |
|
290 |
+
Given the context above, please answer the following question:
|
291 |
+
{query}
|
292 |
+
|
293 |
+
Guidelines:
|
294 |
+
- If you cannot answer based on the context, say so politely
|
295 |
+
- Keep the response concise and focused
|
296 |
+
- Only include sports-related information
|
297 |
+
- No dates or timestamps in the response
|
298 |
+
- Use clear, natural language
|
299 |
|
300 |
+
Answer:"""
|
301 |
|
302 |
+
# Generate response
|
303 |
+
response_placeholder = placeholder.empty()
|
304 |
|
305 |
+
try:
|
306 |
+
# Add logging for model state
|
307 |
+
logging.info("Model state check - Is None?: " + str(self.llm is None))
|
308 |
|
309 |
+
# Directly use Llama model
|
310 |
+
response = self.llm(
|
311 |
+
prompt,
|
312 |
+
max_tokens=512,
|
313 |
+
temperature=0.4,
|
314 |
+
top_p=0.95,
|
315 |
+
echo=False,
|
316 |
+
stop=["Question:", "\n\n"]
|
317 |
+
)
|
318 |
|
319 |
+
logging.info(f"Raw model response: {response}")
|
320 |
|
321 |
+
if response and isinstance(response, dict) and 'choices' in response:
|
322 |
+
generated_text = response['choices'][0].get('text', '').strip()
|
323 |
+
if generated_text:
|
324 |
+
final_response = self.postprocess_response(generated_text)
|
325 |
+
response_placeholder.markdown(final_response)
|
326 |
+
return final_response
|
327 |
|
328 |
+
message = "No relevant answer found. Please try rephrasing your question."
|
329 |
+
response_placeholder.warning(message)
|
330 |
+
return message
|
331 |
+
|
332 |
+
except Exception as e:
|
333 |
+
logging.error(f"Generation error: {str(e)}")
|
334 |
+
logging.error(f"Full error details: ", exc_info=True)
|
335 |
+
message = f"Had some trouble generating the response: {str(e)}"
|
336 |
+
response_placeholder.warning(message)
|
337 |
+
return message
|
338 |
|
339 |
except Exception as e:
|
340 |
+
logging.error(f"Process error: {str(e)}")
|
341 |
logging.error(f"Full error details: ", exc_info=True)
|
342 |
+
message = f"Something went wrong: {str(e)}"
|
343 |
+
placeholder.warning(message)
|
344 |
return message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
345 |
|
346 |
@st.cache_resource(show_spinner=False)
|
347 |
def initialize_rag_pipeline():
|