Kolumbus Lindh commited on
Commit
0cb7604
·
1 Parent(s): 1c16307

added location filtering

Browse files
Files changed (3) hide show
  1. app.py +4 -1
  2. pinecone_handler.py +14 -3
  3. requirements.txt +1 -0
app.py CHANGED
@@ -184,12 +184,15 @@ def main():
184
  with st.expander("Preview extracted text"):
185
  st.text(clean_text[:500] + "..." if len(clean_text) > 500 else clean_text)
186
 
 
 
 
187
  # Search button
188
  if st.button("Search Jobs"):
189
  with st.spinner("Searching for matching jobs..."):
190
  try:
191
  # Search for similar job ads
192
- results = handler.search_similar_ads(clean_text, top_k=num_results)
193
 
194
  if results:
195
  st.subheader("Matching Jobs")
 
184
  with st.expander("Preview extracted text"):
185
  st.text(clean_text[:500] + "..." if len(clean_text) > 500 else clean_text)
186
 
187
+ # Add a city filter textbox above the search button
188
+ city_filter = st.text_input("Filter by city (optional)", value="", help="Enter a city to filter job results by location")
189
+
190
  # Search button
191
  if st.button("Search Jobs"):
192
  with st.spinner("Searching for matching jobs..."):
193
  try:
194
  # Search for similar job ads
195
+ results = handler.search_similar_ads(clean_text, top_k=num_results, city=city_filter.strip())
196
 
197
  if results:
198
  st.subheader("Matching Jobs")
pinecone_handler.py CHANGED
@@ -6,6 +6,7 @@ from sentence_transformers import SentenceTransformer
6
  from typing import List, Dict, Any
7
  import os
8
  from dotenv import load_dotenv
 
9
 
10
  load_dotenv()
11
  PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
@@ -177,13 +178,23 @@ class PineconeHandler:
177
  except Exception as e:
178
  log.error(f"Error deleting ad {ad_id}: {str(e)}")
179
 
180
- def search_similar_ads(self, query: str, top_k: int = 5) -> List[Dict[str, Any]]:
181
- """Search for similar job ads based on text query"""
182
  query_embedding = self.model.encode(query).tolist()
 
 
 
 
 
 
 
 
 
183
  results = self.index.query(
184
  vector=query_embedding,
185
  top_k=top_k,
186
- include_metadata=True
 
187
  )
188
  return results.matches
189
 
 
6
  from typing import List, Dict, Any
7
  import os
8
  from dotenv import load_dotenv
9
+ from typing import List, Dict, Any, Optional
10
 
11
  load_dotenv()
12
  PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
 
178
  except Exception as e:
179
  log.error(f"Error deleting ad {ad_id}: {str(e)}")
180
 
181
+ def search_similar_ads(self, query: str, top_k: int = 5, city: Optional[str] = None) -> List[Dict[str, Any]]:
182
+ """Search for similar job ads based on text query with optional city filtering."""
183
  query_embedding = self.model.encode(query).tolist()
184
+
185
+ # Build the filter dictionary if city is provided
186
+ metadata_filter = {}
187
+ if city:
188
+ city = city.lower().strip() # Normalize
189
+ city = city[0].upper() + city[1:] # Capitalize first letter
190
+ metadata_filter["city"] = {"$eq": city}
191
+
192
+ # Execute the Pinecone query with optional metadata filtering
193
  results = self.index.query(
194
  vector=query_embedding,
195
  top_k=top_k,
196
+ include_metadata=True,
197
+ filter=metadata_filter if metadata_filter else None
198
  )
199
  return results.matches
200
 
requirements.txt CHANGED
@@ -65,3 +65,4 @@ typing_extensions==4.12.2
65
  tzdata==2024.2
66
  urllib3==2.3.0
67
  watchdog==6.0.0
 
 
65
  tzdata==2024.2
66
  urllib3==2.3.0
67
  watchdog==6.0.0
68
+ typing