Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -610,115 +610,6 @@ def remove_incomplete_sentence(text):
|
|
| 610 |
return text
|
| 611 |
|
| 612 |
|
| 613 |
-
import traceback
|
| 614 |
-
|
| 615 |
-
language_code = 1
|
| 616 |
-
query_text = "recipes and meals for vegan diabetes headache fatigue"
|
| 617 |
-
print(f"Generated query text: {query_text}")
|
| 618 |
-
|
| 619 |
-
try:
|
| 620 |
-
# Generate the query embedding
|
| 621 |
-
print("Generating query embedding...")
|
| 622 |
-
query_embedding = embed_query_text(query_text)
|
| 623 |
-
if query_embedding is None:
|
| 624 |
-
raise ValueError("Failed to generate query embedding.")
|
| 625 |
-
print(f"Query embedding generated: {query_embedding}")
|
| 626 |
-
|
| 627 |
-
# Load embeddings and retrieve initial results
|
| 628 |
-
print("Loading recipe embeddings...")
|
| 629 |
-
embeddings_data = load_recipes_embeddings()
|
| 630 |
-
print("Embeddings loaded. Retrieving initial results...")
|
| 631 |
-
initial_results = query_recipes_embeddings(query_embedding, embeddings_data, n_results=10)
|
| 632 |
-
if not initial_results:
|
| 633 |
-
raise ValueError("No relevant recipes found.")
|
| 634 |
-
print(f"Initial results: {initial_results}")
|
| 635 |
-
|
| 636 |
-
# Extract document IDs
|
| 637 |
-
document_ids = [doc_id for doc_id, _ in initial_results]
|
| 638 |
-
print(f"Document IDs: {document_ids}")
|
| 639 |
-
|
| 640 |
-
# Retrieve document texts
|
| 641 |
-
folder_path = 'downloaded_articles/downloaded_articles'
|
| 642 |
-
print("Retrieving document texts...")
|
| 643 |
-
document_texts = retrieve_rec_texts(document_ids, folder_path)
|
| 644 |
-
if not document_texts:
|
| 645 |
-
raise ValueError("Failed to retrieve document texts.")
|
| 646 |
-
print(f"Document texts retrieved: {document_texts}")
|
| 647 |
-
|
| 648 |
-
# Load recipe metadata from DataFrame
|
| 649 |
-
file_path = 'recipes_metadata.xlsx'
|
| 650 |
-
print("Loading metadata from Excel...")
|
| 651 |
-
metadata_df = pd.read_excel(file_path)
|
| 652 |
-
print(f"Metadata loaded: {metadata_df.head()}")
|
| 653 |
-
|
| 654 |
-
# Extract relevant portions
|
| 655 |
-
print("Extracting relevant portions...")
|
| 656 |
-
relevant_portions = extract_relevant_portions(
|
| 657 |
-
document_texts, query_text, max_portions=3, portion_size=1, min_query_words=1
|
| 658 |
-
)
|
| 659 |
-
print(f"Relevant portions: {relevant_portions}")
|
| 660 |
-
|
| 661 |
-
# Flatten portions
|
| 662 |
-
print("Flattening relevant portions...")
|
| 663 |
-
flattened_relevant_portions = []
|
| 664 |
-
for doc_id, portions in relevant_portions.items():
|
| 665 |
-
flattened_relevant_portions.extend(portions)
|
| 666 |
-
unique_selected_parts = remove_duplicates(flattened_relevant_portions)
|
| 667 |
-
print(f"Unique selected parts: {unique_selected_parts}")
|
| 668 |
-
|
| 669 |
-
# Combine parts into a single context
|
| 670 |
-
combined_parts = " ".join(unique_selected_parts)
|
| 671 |
-
print(f"Combined parts: {combined_parts}")
|
| 672 |
-
context = [query_text] + unique_selected_parts
|
| 673 |
-
print(f"Context: {context}")
|
| 674 |
-
|
| 675 |
-
# Extract entities
|
| 676 |
-
print("Extracting entities...")
|
| 677 |
-
entities = extract_entities(query_text)
|
| 678 |
-
print(f"Entities: {entities}")
|
| 679 |
-
|
| 680 |
-
# Enhance passage with entities
|
| 681 |
-
print("Enhancing passage with entities...")
|
| 682 |
-
passage = enhance_passage_with_entities(combined_parts, entities)
|
| 683 |
-
print(f"Enhanced passage: {passage}")
|
| 684 |
-
|
| 685 |
-
# Create the prompt
|
| 686 |
-
print("Creating prompt...")
|
| 687 |
-
prompt = create_prompt(query_text, passage)
|
| 688 |
-
print(f"Prompt: {prompt}")
|
| 689 |
-
|
| 690 |
-
# Generate the answer
|
| 691 |
-
print("Generating answer...")
|
| 692 |
-
answer = generate_answer(prompt)
|
| 693 |
-
print(f"Answer: {answer}")
|
| 694 |
-
answer_part = answer.split("Answer:")[-1].strip()
|
| 695 |
-
print(f"Answer part: {answer_part}")
|
| 696 |
-
|
| 697 |
-
# Clean and finalize the answer
|
| 698 |
-
print("Cleaning answer...")
|
| 699 |
-
cleaned_answer = remove_answer_prefix(answer_part)
|
| 700 |
-
print(f"Cleaned answer: {cleaned_answer}")
|
| 701 |
-
final_answer = remove_incomplete_sentence(cleaned_answer)
|
| 702 |
-
print(f"Final answer: {final_answer}")
|
| 703 |
-
|
| 704 |
-
# Translate if needed
|
| 705 |
-
if language_code == 0:
|
| 706 |
-
print("Translating answer to Arabic...")
|
| 707 |
-
final_answer = translate_en_to_ar(final_answer)
|
| 708 |
-
|
| 709 |
-
# Display the answer
|
| 710 |
-
if final_answer:
|
| 711 |
-
print("Final Answer:")
|
| 712 |
-
print(final_answer)
|
| 713 |
-
else:
|
| 714 |
-
print("Sorry, I can't help with that.")
|
| 715 |
-
except Exception as e:
|
| 716 |
-
print("An error occurred:")
|
| 717 |
-
print(traceback.format_exc())
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
@app.get("/")
|
| 723 |
async def root():
|
| 724 |
return {"message": "Welcome to the FastAPI application! Use the /health endpoint to check health, and /api/query for processing queries."}
|
|
|
|
| 610 |
return text
|
| 611 |
|
| 612 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
@app.get("/")
|
| 614 |
async def root():
|
| 615 |
return {"message": "Welcome to the FastAPI application! Use the /health endpoint to check health, and /api/query for processing queries."}
|