Spaces:
Sleeping
Sleeping
Update src/app.py
Browse files- src/app.py +27 -15
src/app.py
CHANGED
|
@@ -4,11 +4,7 @@ import sys
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# --- 1.
|
| 8 |
-
INDEX_NAME = "branham-index"
|
| 9 |
-
CHUNKS_FILE = "sermon_chunks.pkl"
|
| 10 |
-
|
| 11 |
-
# --- 2. IMPORTS ---
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 13 |
from langchain_community.retrievers import BM25Retriever
|
| 14 |
from langchain_pinecone import PineconeVectorStore
|
|
@@ -18,19 +14,35 @@ from langchain.retrievers import EnsembleRetriever
|
|
| 18 |
|
| 19 |
load_dotenv()
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def get_rag_chain():
|
| 22 |
-
# A.
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if not pinecone_key or not google_key:
|
| 27 |
-
raise ValueError("β Missing API Keys.
|
| 28 |
|
| 29 |
-
# Set environment variables for the libraries to
|
| 30 |
os.environ["PINECONE_API_KEY"] = pinecone_key
|
| 31 |
os.environ["GOOGLE_API_KEY"] = google_key
|
| 32 |
|
| 33 |
-
# B.
|
| 34 |
print("π Connecting to Pinecone...")
|
| 35 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
|
| 36 |
|
|
@@ -40,7 +52,7 @@ def get_rag_chain():
|
|
| 40 |
)
|
| 41 |
vector_retriever = vector_store.as_retriever(search_kwargs={"k": 5})
|
| 42 |
|
| 43 |
-
# C.
|
| 44 |
print("π Loading Keyword Search...")
|
| 45 |
keyword_retriever = None
|
| 46 |
|
|
@@ -55,7 +67,7 @@ def get_rag_chain():
|
|
| 55 |
except Exception as e:
|
| 56 |
print(f"β Failed to load keyword file: {e}")
|
| 57 |
|
| 58 |
-
# D.
|
| 59 |
if keyword_retriever:
|
| 60 |
print("π Linking Hybrid System...")
|
| 61 |
final_retriever = EnsembleRetriever(
|
|
@@ -65,14 +77,14 @@ def get_rag_chain():
|
|
| 65 |
else:
|
| 66 |
final_retriever = vector_retriever
|
| 67 |
|
| 68 |
-
# E.
|
| 69 |
llm = ChatGoogleGenerativeAI(
|
| 70 |
model="gemini-1.5-flash",
|
| 71 |
temperature=0.3,
|
| 72 |
convert_system_message_to_human=True
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# F.
|
| 76 |
template = """You are William Marion Branham.
|
| 77 |
|
| 78 |
INSTRUCTIONS:
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# --- 1. IMPORTS ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 9 |
from langchain_community.retrievers import BM25Retriever
|
| 10 |
from langchain_pinecone import PineconeVectorStore
|
|
|
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
+
# --- 2. CONFIGURATION ---
|
| 18 |
+
INDEX_NAME = "branham-index"
|
| 19 |
+
CHUNKS_FILE = "sermon_chunks.pkl"
|
| 20 |
+
|
| 21 |
def get_rag_chain():
|
| 22 |
+
# --- A. ROBUST AUTHENTICATION ---
|
| 23 |
+
# 1. Try Hugging Face Environment Variables FIRST (This is where HF stores Settings)
|
| 24 |
+
pinecone_key = os.environ.get("PINECONE_API_KEY")
|
| 25 |
+
google_key = os.environ.get("GOOGLE_API_KEY")
|
| 26 |
|
| 27 |
+
# 2. If not found in Env, try Streamlit Secrets (Local fallback)
|
| 28 |
+
if not pinecone_key or not google_key:
|
| 29 |
+
try:
|
| 30 |
+
if not pinecone_key:
|
| 31 |
+
pinecone_key = st.secrets.get("PINECONE_API_KEY")
|
| 32 |
+
if not google_key:
|
| 33 |
+
google_key = st.secrets.get("GOOGLE_API_KEY")
|
| 34 |
+
except Exception:
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
# 3. Final Check
|
| 38 |
if not pinecone_key or not google_key:
|
| 39 |
+
raise ValueError("β Missing API Keys. Please add PINECONE_API_KEY and GOOGLE_API_KEY to Settings > Secrets.")
|
| 40 |
|
| 41 |
+
# Set environment variables for the libraries to use
|
| 42 |
os.environ["PINECONE_API_KEY"] = pinecone_key
|
| 43 |
os.environ["GOOGLE_API_KEY"] = google_key
|
| 44 |
|
| 45 |
+
# --- B. CLOUD VECTOR SEARCH (Pinecone) ---
|
| 46 |
print("π Connecting to Pinecone...")
|
| 47 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
|
| 48 |
|
|
|
|
| 52 |
)
|
| 53 |
vector_retriever = vector_store.as_retriever(search_kwargs={"k": 5})
|
| 54 |
|
| 55 |
+
# --- C. LOCAL KEYWORD SEARCH (Direct .pkl Load) ---
|
| 56 |
print("π Loading Keyword Search...")
|
| 57 |
keyword_retriever = None
|
| 58 |
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
print(f"β Failed to load keyword file: {e}")
|
| 69 |
|
| 70 |
+
# --- D. HYBRID MERGE ---
|
| 71 |
if keyword_retriever:
|
| 72 |
print("π Linking Hybrid System...")
|
| 73 |
final_retriever = EnsembleRetriever(
|
|
|
|
| 77 |
else:
|
| 78 |
final_retriever = vector_retriever
|
| 79 |
|
| 80 |
+
# --- E. MODEL ---
|
| 81 |
llm = ChatGoogleGenerativeAI(
|
| 82 |
model="gemini-1.5-flash",
|
| 83 |
temperature=0.3,
|
| 84 |
convert_system_message_to_human=True
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# --- F. PROMPT ---
|
| 88 |
template = """You are William Marion Branham.
|
| 89 |
|
| 90 |
INSTRUCTIONS:
|