Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -536,67 +536,67 @@ def remove_incomplete_sentence(text):
|
|
536 |
return text
|
537 |
|
538 |
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
|
601 |
|
602 |
|
|
|
536 |
return text
|
537 |
|
538 |
|
539 |
+
language_code = 1
|
540 |
+
query_text = "recipes and meals for vegan diabetes headache fatigue"
|
541 |
+
print(f"Generated query text: {query_text}")
|
542 |
+
|
543 |
+
# Generate the query embedding
|
544 |
+
query_embedding = embed_query_text(query_text)
|
545 |
+
if query_embedding is None:
|
546 |
+
raise ValueError("Failed to generate query embedding.")
|
547 |
+
|
548 |
+
# Load embeddings and retrieve initial results
|
549 |
+
embeddings_data = load_recipes_embeddings()
|
550 |
+
folder_path = 'downloaded_articles/downloaded_articles'
|
551 |
+
initial_results = query_recipes_embeddings(query_embedding, embeddings_data, n_results=10)
|
552 |
+
if not initial_results:
|
553 |
+
raise ValueError("No relevant recipes found.")
|
554 |
+
print(initial_results)
|
555 |
+
# Extract document IDs
|
556 |
+
document_ids = [doc_id for doc_id, _ in initial_results]
|
557 |
+
print(document_ids)
|
558 |
+
# Retrieve document texts
|
559 |
+
document_texts = retrieve_rec_texts(document_ids, folder_path)
|
560 |
+
if not document_texts:
|
561 |
+
raise ValueError("Failed to retrieve document texts.")
|
562 |
+
print(document_texts)
|
563 |
+
# Load recipe metadata from DataFrame
|
564 |
+
folder_path='downloaded_articles/downloaded_articles'
|
565 |
+
file_path = 'recipes_metadata.xlsx'
|
566 |
+
metadata_path = 'recipes_metadata.xlsx'
|
567 |
+
metadata_df = pd.read_excel(file_path)
|
568 |
+
relevant_portions = extract_relevant_portions(document_texts, query_text, max_portions=3, portion_size=1, min_query_words=1)
|
569 |
+
print(relevant_portions)
|
570 |
+
flattened_relevant_portions = []
|
571 |
+
for doc_id, portions in relevant_portions.items():
|
572 |
+
flattened_relevant_portions.extend(portions)
|
573 |
+
unique_selected_parts = remove_duplicates(flattened_relevant_portions)
|
574 |
+
print(unique_selected_parts)
|
575 |
+
combined_parts = " ".join(unique_selected_parts)
|
576 |
+
print(combined_parts)
|
577 |
+
context = [query_text] + unique_selected_parts
|
578 |
+
print(context)
|
579 |
+
entities = extract_entities(query_text)
|
580 |
+
print(entities)
|
581 |
+
passage = enhance_passage_with_entities(combined_parts, entities)
|
582 |
+
print(passage)
|
583 |
+
prompt = create_prompt(query_text, passage)
|
584 |
+
print(prompt)
|
585 |
+
answer = generate_answer(prompt)
|
586 |
+
print(answer)
|
587 |
+
answer_part = answer.split("Answer:")[-1].strip()
|
588 |
+
print(answer_part)
|
589 |
+
cleaned_answer = remove_answer_prefix(answer_part)
|
590 |
+
print(cleaned_answer)
|
591 |
+
final_answer = remove_incomplete_sentence(cleaned_answer)
|
592 |
+
print(final_answer )
|
593 |
+
if language_code == 0:
|
594 |
+
final_answer = translate_en_to_ar(final_answer)
|
595 |
+
if final_answer:
|
596 |
+
print("Answer:")
|
597 |
+
print(final_answer)
|
598 |
+
else:
|
599 |
+
print("Sorry, I can't help with that.")
|
600 |
|
601 |
|
602 |
|