Tesneem commited on
Commit
7392916
Β·
verified Β·
1 Parent(s): f478447

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -60,28 +60,17 @@ def init_embedding_model():
60
 
61
  @st.cache_resource
62
  def init_vector_search() -> MongoDBAtlasVectorSearch:
63
- from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
64
- from langchain_community.vectorstores import MongoDBAtlasVectorSearch
65
-
66
- HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
67
- # model_name = "thenlper/gte-small"
68
 
69
  try:
70
- st.write(f"πŸ”Œ Connecting to Hugging Face model: `{model_name}`")
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"βœ… HF embedding model connected. Vector length: {len(test_vector)}")
80
-
81
  except Exception as e:
82
- st.error("❌ Failed to connect to Hugging Face Embedding API")
83
  st.error(f"Error: {e}")
84
- raise e # Stop app here if embedding fails
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: