thechaiexperiment commited on
Commit
c883301
·
1 Parent(s): a4f9a20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -16
app.py CHANGED
@@ -566,34 +566,68 @@ async def chat_endpoint(chat_query: ChatQuery):
566
 
567
  @app.post("/api/resources")
568
  async def resources_endpoint(profile: MedicalProfile):
569
- query_text = profile.conditions + profile.daily_symptoms
570
- query_embedding = embed_query_text(query_text) # Embed the query text
571
- embeddings_data = load_embeddings ()
 
 
 
 
 
 
 
 
572
  folder_path = 'downloaded_articles/downloaded_articles'
573
  initial_results = query_embeddings(query_embedding, embeddings_data, n_results=6)
 
 
 
 
574
  document_ids = [doc_id for doc_id, _ in initial_results]
 
 
575
  file_path = 'finalcleaned_excel_file.xlsx'
576
  df = pd.read_excel(file_path)
577
  file_name_to_url = {f"article_{index}.html": url for index, url in enumerate(df['Unnamed: 0'])}
578
- file_names = document_ids
579
-
580
- # Retrieve original URLs
581
- for file_name in file_names:
582
  original_url = file_name_to_url.get(file_name, None)
583
  if original_url:
584
- title = get_page_title(original_url)
585
- if title:
586
- print(f"Title: {title},URL: {original_url}")
587
- else:
588
- print(f"Name: {file_name}")
589
  else:
590
- print(f"Name: {file_name}")
 
 
591
  document_texts = retrieve_document_texts(document_ids, folder_path)
 
 
 
 
592
  cross_encoder = models['cross_encoder']
593
  scores = cross_encoder.predict([(query_text, doc) for doc in document_texts])
594
- scored_documents = list(zip(scores, document_ids, document_texts))
595
- scored_documents.sort(key=lambda x: x[0], reverse=True)
596
- return {"resources": scored_documents, "success": True}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
 
598
 
599
  @app.post("/api/recipes")
 
566
 
567
  @app.post("/api/resources")
568
  async def resources_endpoint(profile: MedicalProfile):
569
+ try:
570
+ # Build the query text
571
+ query_text = profile.conditions + " " + profile.daily_symptoms
572
+
573
+ # Generate the query embedding
574
+ query_embedding = embed_query_text(query_text)
575
+ if query_embedding is None:
576
+ raise ValueError("Failed to generate query embedding.")
577
+
578
+ # Load embeddings and retrieve initial results
579
+ embeddings_data = load_embeddings()
580
  folder_path = 'downloaded_articles/downloaded_articles'
581
  initial_results = query_embeddings(query_embedding, embeddings_data, n_results=6)
582
+ if not initial_results:
583
+ raise ValueError("No relevant documents found.")
584
+
585
+ # Extract document IDs
586
  document_ids = [doc_id for doc_id, _ in initial_results]
587
+
588
+ # Load document metadata (URL mappings)
589
  file_path = 'finalcleaned_excel_file.xlsx'
590
  df = pd.read_excel(file_path)
591
  file_name_to_url = {f"article_{index}.html": url for index, url in enumerate(df['Unnamed: 0'])}
592
+
593
+ # Map file names to original URLs
594
+ resources = []
595
+ for file_name in document_ids:
596
  original_url = file_name_to_url.get(file_name, None)
597
  if original_url:
598
+ title = get_page_title(original_url) or "Unknown Title"
599
+ resources.append({"file_name": file_name, "title": title, "url": original_url})
 
 
 
600
  else:
601
+ resources.append({"file_name": file_name, "title": "Unknown", "url": None})
602
+
603
+ # Retrieve document texts
604
  document_texts = retrieve_document_texts(document_ids, folder_path)
605
+ if not document_texts:
606
+ raise ValueError("Failed to retrieve document texts.")
607
+
608
+ # Perform re-ranking
609
  cross_encoder = models['cross_encoder']
610
  scores = cross_encoder.predict([(query_text, doc) for doc in document_texts])
611
+ scores = [float(score) for score in scores] # Convert to native Python float
612
+
613
+ # Combine scores with resources
614
+ for i, resource in enumerate(resources):
615
+ resource["score"] = scores[i] if i < len(scores) else 0.0
616
+
617
+ # Sort resources by score
618
+ resources.sort(key=lambda x: x["score"], reverse=True)
619
+
620
+ # Limit response to top 5 resources
621
+ return {"resources": resources[:5], "success": True}
622
+
623
+ except ValueError as ve:
624
+ # Handle expected errors
625
+ raise HTTPException(status_code=400, detail=str(ve))
626
+ except Exception as e:
627
+ # Handle unexpected errors
628
+ print(f"Unexpected error: {e}")
629
+ raise HTTPException(status_code=500, detail="An unexpected error occurred.")
630
+
631
 
632
 
633
  @app.post("/api/recipes")