#!/usr/bin/env python3 """ Gradio web application for semantic search over Bahá'í Writings. """ import os import gradio as gr from pinecone import Pinecone from openai import OpenAI from dotenv import load_dotenv from typing import List, Dict, Any, Optional # Load environment variables load_dotenv() class BahaiSemanticSearch: def __init__(self, openai_api_key: str, pinecone_api_key: str): """Initialize the semantic search engine.""" self.openai_client = OpenAI(api_key=openai_api_key) self.pc = Pinecone(api_key=pinecone_api_key) self.index_name = "bahai-writings" try: self.index = self.pc.Index(self.index_name) except Exception as e: raise RuntimeError(f"Failed to connect to Pinecone index: {e}. Please run the ingestion script first: `python ingest.py`") def generate_query_embedding(self, query: str) -> List[float]: """Generate embedding for search query.""" try: response = self.openai_client.embeddings.create( model="text-embedding-3-large", input=query, encoding_format="float" ) return response.data[0].embedding except Exception as e: raise RuntimeError(f"Error generating query embedding: {e}") def process_journal_entry(self, journal_entry: str) -> str: """Process journal entry through GPT-4o-mini and return response for search.""" prompt = "Here is a journal entry. Provide a compassionate and uplifting response to the user based on the Teachings of the Baha'i Faith. In your response, restate what the user is saying to you." try: response = self.openai_client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": journal_entry} ], max_tokens=500, temperature=0.7 ) return response.choices[0].message.content.strip() if response.choices[0].message.content else "" except Exception as e: raise RuntimeError(f"Error processing journal entry: {e}") def search(self, query: str, n_results: int = 10, author_filter: Optional[List[str]] = None) -> List[Dict[str, Any]]: """Perform semantic search and return results.""" if not query.strip(): return [] # Generate query embedding query_embedding = self.generate_query_embedding(query) if not query_embedding: return [] try: # Prepare filter for Pinecone query pinecone_filter = None if author_filter: pinecone_filter = {"author": {"$in": author_filter}} # Search in Pinecone results = self.index.query( vector=query_embedding, top_k=n_results, include_metadata=True, filter=pinecone_filter ) # Format results formatted_results = [] for match in results['matches']: formatted_results.append({ 'text': match['metadata']['text'], 'source_file': match['metadata']['source_file'], 'paragraph_id': match['metadata']['paragraph_id'], 'score': match['score'] }) return formatted_results except Exception as e: raise RuntimeError(f"Search error: {e}") def get_bahai_library_url(self, source_file: str) -> str: """Convert source filename to Bahá'í library URL.""" base_url = "https://www.bahai.org/library/authoritative-texts" # Remove .docx extension and convert to lowercase filename = source_file.replace('.docx', '').lower() # Mapping of filenames to library URLs url_mappings = { # Bahá'u'lláh 'kitab-i-iqan': f"{base_url}/bahaullah/kitab-i-iqan/", 'hidden-words': f"{base_url}/bahaullah/hidden-words/", 'gleanings-writings-bahaullah': f"{base_url}/bahaullah/gleanings-writings-bahaullah/", 'kitab-i-aqdas-2': f"{base_url}/bahaullah/kitab-i-aqdas/", 'epistle-son-wolf': f"{base_url}/bahaullah/epistle-son-wolf/", 'gems-divine-mysteries': f"{base_url}/bahaullah/gems-divine-mysteries/", 'summons-lord-hosts': f"{base_url}/bahaullah/summons-lord-hosts/", 'tablets-bahaullah': f"{base_url}/bahaullah/tablets-bahaullah/", 'tabernacle-unity': f"{base_url}/bahaullah/tabernacle-unity/", # 'Abdu'l-Bahá 'some-answered-questions': f"{base_url}/abdul-baha/some-answered-questions/", 'paris-talks': f"{base_url}/abdul-baha/paris-talks/", 'promulgation-universal-peace': f"{base_url}/abdul-baha/promulgation-universal-peace/", 'memorials-faithful': f"{base_url}/abdul-baha/memorials-faithful/", 'selections-writings-abdul-baha': f"{base_url}/abdul-baha/selections-writings-abdul-baha/", 'secret-divine-civilization': f"{base_url}/abdul-baha/secret-divine-civilization/", 'travelers-narrative': f"{base_url}/abdul-baha/travelers-narrative/", 'will-testament-abdul-baha': f"{base_url}/abdul-baha/will-testament-abdul-baha/", 'tablets-divine-plan': f"{base_url}/abdul-baha/tablets-divine-plan/", 'tablet-auguste-forel': f"{base_url}/abdul-baha/tablet-auguste-forel/", # The Báb 'selections-writings-bab': f"{base_url}/the-bab/selections-writings-bab/", # Shoghi Effendi 'advent-divine-justice': f"{base_url}/shoghi-effendi/advent-divine-justice/", 'god-passes-by': f"{base_url}/shoghi-effendi/god-passes-by/", 'promised-day-come': f"{base_url}/shoghi-effendi/promised-day-come/", 'world-order-bahaullah': f"{base_url}/shoghi-effendi/world-order-bahaullah/", # Compilations and other works 'prayers-meditations': f"{base_url}/bahaullah/prayers-meditations/", 'days-remembrance': f"{base_url}/compilations/days-remembrance/", 'light-of-the-world': f"{base_url}/compilations/light-of-the-world/", 'turning-point': f"{base_url}/compilations/turning-point/", } # Return specific URL if mapping exists, otherwise return main library page return url_mappings.get(filename, "https://www.bahai.org/library/") def get_index_stats(self) -> Dict[str, Any]: """Get index statistics.""" try: stats = self.index.describe_index_stats() return {"total_vectors": stats['total_vector_count']} except Exception as e: return {"error": str(e)} def format_results_html(results: List[Dict[str, Any]], search_engine: BahaiSemanticSearch) -> str: """Format search results as HTML for Gradio display.""" if not results: return "
{result['text']}