Shreyas094 commited on
Commit
d8b711d
·
verified ·
1 Parent(s): a6df94d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -255,6 +255,10 @@ def summarize_content(content, model):
255
  return summary
256
 
257
  def rank_search_results(titles, summaries, model):
 
 
 
 
258
  ranking_prompt = (
259
  "Rank the following search results from a financial analyst perspective. "
260
  f"Assign a rank from 1 to {len(titles)} based on relevance, with 1 being the most relevant. "
@@ -273,12 +277,13 @@ def rank_search_results(titles, summaries, model):
273
 
274
  # Check if we have the correct number of ranks
275
  if len(ranks) != len(titles):
276
- raise ValueError("Number of ranks does not match number of titles")
 
 
277
 
278
  return ranks
279
  except Exception as e:
280
  print(f"Error in ranking: {str(e)}. Using fallback ranking method.")
281
- # Fallback: assign ranks based on original order
282
  return list(range(1, len(titles) + 1))
283
 
284
  def ask_question(question, temperature, top_p, repetition_penalty, web_search):
@@ -417,20 +422,26 @@ def update_vector_db_with_search_results(search_results, summaries, ranks):
417
 
418
  current_date = datetime.now().strftime("%Y-%m-%d")
419
 
 
420
  for result, summary, rank in zip(search_results, summaries, ranks):
421
- doc = Document(
422
- page_content=summary,
423
- metadata={
424
- "search_date": current_date,
425
- "search_title": result["title"],
426
- "search_content": result["text"],
427
- "search_summary": summary,
428
- "rank": rank
429
- }
430
- )
431
- database.add_documents([doc])
 
432
 
433
- database.save_local("faiss_database")
 
 
 
 
434
 
435
  def export_vector_db_to_excel():
436
  embed = get_embeddings()
 
255
  return summary
256
 
257
  def rank_search_results(titles, summaries, model):
258
+ if not titles or not summaries:
259
+ print("No titles or summaries to rank.")
260
+ return list(range(1, len(titles) + 1))
261
+
262
  ranking_prompt = (
263
  "Rank the following search results from a financial analyst perspective. "
264
  f"Assign a rank from 1 to {len(titles)} based on relevance, with 1 being the most relevant. "
 
277
 
278
  # Check if we have the correct number of ranks
279
  if len(ranks) != len(titles):
280
+ print(f"Warning: Number of ranks ({len(ranks)}) does not match number of titles ({len(titles)})")
281
+ print(f"Model output: {ranks_str}")
282
+ return list(range(1, len(titles) + 1))
283
 
284
  return ranks
285
  except Exception as e:
286
  print(f"Error in ranking: {str(e)}. Using fallback ranking method.")
 
287
  return list(range(1, len(titles) + 1))
288
 
289
  def ask_question(question, temperature, top_p, repetition_penalty, web_search):
 
422
 
423
  current_date = datetime.now().strftime("%Y-%m-%d")
424
 
425
+ documents = []
426
  for result, summary, rank in zip(search_results, summaries, ranks):
427
+ if summary: # Only create a document if there's a summary
428
+ doc = Document(
429
+ page_content=summary,
430
+ metadata={
431
+ "search_date": current_date,
432
+ "search_title": result["title"],
433
+ "search_content": result["text"],
434
+ "search_summary": summary,
435
+ "rank": rank
436
+ }
437
+ )
438
+ documents.append(doc)
439
 
440
+ if documents: # Only update the database if there are documents to add
441
+ database.add_documents(documents)
442
+ database.save_local("faiss_database")
443
+ else:
444
+ print("No valid documents to add to the database.")
445
 
446
  def export_vector_db_to_excel():
447
  embed = get_embeddings()