thechaiexperiment commited on
Commit
e756bb2
·
1 Parent(s): a8681f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -275,9 +275,9 @@ async def process_query(request: QueryRequest):
275
  if language_code == 0:
276
  query_text = translate_text(query_text, 'ar_to_en')
277
 
278
- query_embedding = models['embedding'].encode([query_text])
279
  relevant_docs = query_embeddings(query_embedding)
280
-
281
  if not relevant_docs:
282
  return {
283
  'answer': 'No relevant information found. Please try a different query.',
@@ -286,7 +286,7 @@ async def process_query(request: QueryRequest):
286
 
287
  doc_texts = [retrieve_document_text(doc_id) for doc_id, _ in relevant_docs]
288
  doc_texts = [text for text in doc_texts if text.strip()]
289
-
290
  if not doc_texts:
291
  return {
292
  'answer': 'Unable to retrieve relevant documents. Please try again.',
@@ -295,15 +295,16 @@ async def process_query(request: QueryRequest):
295
 
296
  rerank_scores = rerank_documents(query_text, doc_texts)
297
  ranked_texts = [text for _, text in sorted(zip(rerank_scores, doc_texts), reverse=True)]
298
-
299
  context = " ".join(ranked_texts[:3])
300
  answer = generate_answer(query_text, context)
301
-
302
  if language_code == 0:
303
  answer = translate_text(answer, 'en_to_ar')
304
 
305
  return {
306
  'answer': answer,
 
307
  'success': True
308
  }
309
 
@@ -323,7 +324,7 @@ async def process_query(request: QueryRequest):
323
 
324
  # Initialize application
325
  print("Initializing application...")
326
- init_success = init_nltk() and load_models() and load_data()
327
 
328
  if not init_success:
329
  print("Warning: Application initialized with partial functionality")
 
275
  if language_code == 0:
276
  query_text = translate_text(query_text, 'ar_to_en')
277
 
278
+ query_embedding = models['embedding'].encode([query_text]) # Ensure the model has been loaded and is available.
279
  relevant_docs = query_embeddings(query_embedding)
280
+
281
  if not relevant_docs:
282
  return {
283
  'answer': 'No relevant information found. Please try a different query.',
 
286
 
287
  doc_texts = [retrieve_document_text(doc_id) for doc_id, _ in relevant_docs]
288
  doc_texts = [text for text in doc_texts if text.strip()]
289
+
290
  if not doc_texts:
291
  return {
292
  'answer': 'Unable to retrieve relevant documents. Please try again.',
 
295
 
296
  rerank_scores = rerank_documents(query_text, doc_texts)
297
  ranked_texts = [text for _, text in sorted(zip(rerank_scores, doc_texts), reverse=True)]
298
+
299
  context = " ".join(ranked_texts[:3])
300
  answer = generate_answer(query_text, context)
301
+
302
  if language_code == 0:
303
  answer = translate_text(answer, 'en_to_ar')
304
 
305
  return {
306
  'answer': answer,
307
+ 'reranked_documents': ranked_texts,
308
  'success': True
309
  }
310
 
 
324
 
325
  # Initialize application
326
  print("Initializing application...")
327
+ init_success = load_models() and load_data()
328
 
329
  if not init_success:
330
  print("Warning: Application initialized with partial functionality")