Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -60,28 +60,17 @@ def init_embedding_model():
|
|
60 |
|
61 |
@st.cache_resource
|
62 |
def init_vector_search() -> MongoDBAtlasVectorSearch:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
67 |
-
# model_name = "thenlper/gte-small"
|
68 |
|
69 |
try:
|
70 |
-
|
71 |
-
# embedding_model = HuggingFaceInferenceAPIEmbeddings(
|
72 |
-
# api_key=HF_TOKEN,
|
73 |
-
# model_name=model_name
|
74 |
-
# )
|
75 |
-
|
76 |
-
embedding_model=init_embedding_model()
|
77 |
-
# Test if embedding works
|
78 |
test_vector = embedding_model.embed_query("Test query for Grant Buddy")
|
79 |
-
st.success(f"β
|
80 |
-
|
81 |
except Exception as e:
|
82 |
-
st.error("β Failed to
|
83 |
st.error(f"Error: {e}")
|
84 |
-
raise e
|
85 |
|
86 |
# MongoDB setup
|
87 |
user = quote_plus(os.getenv("MONGO_USERNAME", "").strip())
|
@@ -93,7 +82,6 @@ def init_vector_search() -> MongoDBAtlasVectorSearch:
|
|
93 |
|
94 |
MONGO_URI = f"mongodb+srv://{user}:{password}@{cluster}/{db_name}?retryWrites=true&w=majority"
|
95 |
|
96 |
-
# Connect to vector search
|
97 |
try:
|
98 |
vector_store = MongoDBAtlasVectorSearch.from_connection_string(
|
99 |
connection_string=MONGO_URI,
|
@@ -103,12 +91,21 @@ def init_vector_search() -> MongoDBAtlasVectorSearch:
|
|
103 |
)
|
104 |
st.success("β
Connected to MongoDB Vector Search")
|
105 |
return vector_store
|
106 |
-
|
107 |
except Exception as e:
|
108 |
st.error("β Failed to connect to MongoDB Atlas Vector Search")
|
109 |
st.error(f"Error: {e}")
|
110 |
raise e
|
|
|
|
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
# =================== Format Retrieved Chunks ===================
|
114 |
def format_docs(docs: List[Document]) -> str:
|
|
|
60 |
|
61 |
@st.cache_resource
|
62 |
def init_vector_search() -> MongoDBAtlasVectorSearch:
|
63 |
+
# Load local embedding model
|
64 |
+
embedding_model = init_embedding_model()
|
|
|
|
|
|
|
65 |
|
66 |
try:
|
67 |
+
# Test embedding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
test_vector = embedding_model.embed_query("Test query for Grant Buddy")
|
69 |
+
st.success(f"β
Local embedding model loaded. Vector length: {len(test_vector)}")
|
|
|
70 |
except Exception as e:
|
71 |
+
st.error("β Failed to compute embedding locally")
|
72 |
st.error(f"Error: {e}")
|
73 |
+
raise e
|
74 |
|
75 |
# MongoDB setup
|
76 |
user = quote_plus(os.getenv("MONGO_USERNAME", "").strip())
|
|
|
82 |
|
83 |
MONGO_URI = f"mongodb+srv://{user}:{password}@{cluster}/{db_name}?retryWrites=true&w=majority"
|
84 |
|
|
|
85 |
try:
|
86 |
vector_store = MongoDBAtlasVectorSearch.from_connection_string(
|
87 |
connection_string=MONGO_URI,
|
|
|
91 |
)
|
92 |
st.success("β
Connected to MongoDB Vector Search")
|
93 |
return vector_store
|
|
|
94 |
except Exception as e:
|
95 |
st.error("β Failed to connect to MongoDB Atlas Vector Search")
|
96 |
st.error(f"Error: {e}")
|
97 |
raise e
|
98 |
+
β
Optional: Clean Up init_embedding_model() Too
|
99 |
+
Just to be safe, you can explicitly name the model inside init_embedding_model():
|
100 |
|
101 |
+
python
|
102 |
+
Copy
|
103 |
+
Edit
|
104 |
+
@st.cache_resource
|
105 |
+
def init_embedding_model():
|
106 |
+
model_name = "sentence-transformers/all-mpnet-base-v2"
|
107 |
+
st.write(f"π¦ Loading local embedding model: `{model_name}`")
|
108 |
+
return HuggingFaceEmbeddings(model_name=model_name)
|
109 |
|
110 |
# =================== Format Retrieved Chunks ===================
|
111 |
def format_docs(docs: List[Document]) -> str:
|