Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 5 |
from langchain_community.vectorstores import FAISS
|
|
@@ -9,10 +10,28 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 9 |
# --- Configuration ---
|
| 10 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
|
| 11 |
|
| 12 |
-
#
|
|
|
|
| 13 |
FAISS_INDEX_PATH = "faiss_index"
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def get_conversational_chain(api_key):
|
| 17 |
"""Create the QA chain with strict context-only answering."""
|
| 18 |
prompt_template = """
|
|
@@ -57,6 +76,9 @@ def user_input(user_question, vector_store, api_key):
|
|
| 57 |
@st.cache_resource
|
| 58 |
def load_vector_store(_api_key):
|
| 59 |
"""Load pre-built FAISS vector store."""
|
|
|
|
|
|
|
|
|
|
| 60 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 61 |
model="models/embedding-001",
|
| 62 |
google_api_key=_api_key
|
|
@@ -188,11 +210,11 @@ def main():
|
|
| 188 |
st.info("Please add GOOGLE_API_KEY to your Hugging Face Space secrets.")
|
| 189 |
st.stop()
|
| 190 |
|
| 191 |
-
# Check if FAISS index exists
|
| 192 |
index_file = os.path.join(FAISS_INDEX_PATH, "index.faiss")
|
| 193 |
-
if not os.path.exists(index_file):
|
| 194 |
-
st.error(
|
| 195 |
-
st.info("Please upload
|
| 196 |
st.stop()
|
| 197 |
|
| 198 |
# Load vector store (cached)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
import zipfile
|
| 4 |
|
| 5 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 6 |
from langchain_community.vectorstores import FAISS
|
|
|
|
| 10 |
# --- Configuration ---
|
| 11 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
|
| 12 |
|
| 13 |
+
# Paths
|
| 14 |
+
FAISS_ZIP_PATH = "faiss_index.zip"
|
| 15 |
FAISS_INDEX_PATH = "faiss_index"
|
| 16 |
|
| 17 |
|
| 18 |
+
def extract_faiss_index():
|
| 19 |
+
"""Extract FAISS index from zip file if needed."""
|
| 20 |
+
index_file = os.path.join(FAISS_INDEX_PATH, "index.faiss")
|
| 21 |
+
|
| 22 |
+
# Already extracted
|
| 23 |
+
if os.path.exists(index_file):
|
| 24 |
+
return True
|
| 25 |
+
|
| 26 |
+
# Extract from zip
|
| 27 |
+
if os.path.exists(FAISS_ZIP_PATH):
|
| 28 |
+
with zipfile.ZipFile(FAISS_ZIP_PATH, 'r') as zip_ref:
|
| 29 |
+
zip_ref.extractall(".")
|
| 30 |
+
return True
|
| 31 |
+
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
|
| 35 |
def get_conversational_chain(api_key):
|
| 36 |
"""Create the QA chain with strict context-only answering."""
|
| 37 |
prompt_template = """
|
|
|
|
| 76 |
@st.cache_resource
|
| 77 |
def load_vector_store(_api_key):
|
| 78 |
"""Load pre-built FAISS vector store."""
|
| 79 |
+
# Extract zip if needed
|
| 80 |
+
extract_faiss_index()
|
| 81 |
+
|
| 82 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 83 |
model="models/embedding-001",
|
| 84 |
google_api_key=_api_key
|
|
|
|
| 210 |
st.info("Please add GOOGLE_API_KEY to your Hugging Face Space secrets.")
|
| 211 |
st.stop()
|
| 212 |
|
| 213 |
+
# Check if FAISS index or zip exists
|
| 214 |
index_file = os.path.join(FAISS_INDEX_PATH, "index.faiss")
|
| 215 |
+
if not os.path.exists(index_file) and not os.path.exists(FAISS_ZIP_PATH):
|
| 216 |
+
st.error("FAISS index not found!")
|
| 217 |
+
st.info("Please upload faiss_index.zip or the faiss_index folder to your Space.")
|
| 218 |
st.stop()
|
| 219 |
|
| 220 |
# Load vector store (cached)
|