Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -50,8 +50,6 @@ class SimpleRecommendation(BaseModel):
|
|
| 50 |
|
| 51 |
class RecommendationResponse(BaseModel):
|
| 52 |
recommendations: List[SimpleRecommendation]
|
| 53 |
-
page: int
|
| 54 |
-
total_pages: int
|
| 55 |
|
| 56 |
class StatusResponse(BaseModel):
|
| 57 |
status: str
|
|
@@ -178,68 +176,53 @@ def add_internship(internship: InternshipData, db_client: firestore.Client = Dep
|
|
| 178 |
return {"status": "success", "internship_id": internship.id}
|
| 179 |
|
| 180 |
@app.post("/profile-recommendations", response_model=RecommendationResponse)
|
| 181 |
-
def get_profile_recommendations(profile: UserProfile
|
| 182 |
if chroma_collection is None or encoder is None:
|
| 183 |
raise HTTPException(status_code=503, detail="Server is not ready.")
|
| 184 |
|
| 185 |
query_text = f"Skills: {', '.join(profile.skills)}. Sectors: {', '.join(profile.sectors)}"
|
| 186 |
query_embedding = encoder.encode([query_text])[0].tolist()
|
| 187 |
|
| 188 |
-
total_items = chroma_collection.count()
|
| 189 |
results = chroma_collection.query(
|
| 190 |
query_embeddings=[query_embedding],
|
| 191 |
-
n_results=
|
| 192 |
)
|
| 193 |
|
|
|
|
| 194 |
ids = results.get('ids', [[]])[0]
|
| 195 |
distances = results.get('distances', [[]])[0]
|
| 196 |
-
all_recommendations = [
|
| 197 |
-
{"internship_id": ids[i], "score": 1 - distances[i]}
|
| 198 |
-
for i in range(len(ids))
|
| 199 |
-
]
|
| 200 |
-
|
| 201 |
-
start_index = (page - 1) * page_size
|
| 202 |
-
end_index = start_index + page_size
|
| 203 |
-
paginated_results = all_recommendations[start_index:end_index]
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
}
|
| 212 |
|
| 213 |
@app.post("/search", response_model=RecommendationResponse)
|
| 214 |
-
def search_internships(search: SearchQuery
|
| 215 |
if chroma_collection is None or encoder is None:
|
| 216 |
raise HTTPException(status_code=503, detail="Server is not ready.")
|
| 217 |
-
|
| 218 |
query_embedding = encoder.encode([search.query])[0].tolist()
|
| 219 |
-
|
| 220 |
results = chroma_collection.query(
|
| 221 |
query_embeddings=[query_embedding],
|
| 222 |
-
n_results=
|
| 223 |
)
|
| 224 |
|
|
|
|
| 225 |
ids = results.get('ids', [[]])[0]
|
| 226 |
distances = results.get('distances', [[]])[0]
|
| 227 |
-
all_recommendations = [
|
| 228 |
-
{"internship_id": ids[i], "score": 1 - distances[i]}
|
| 229 |
-
for i in range(len(ids))
|
| 230 |
-
]
|
| 231 |
-
|
| 232 |
-
start_index = (page - 1) * page_size
|
| 233 |
-
end_index = start_index + page_size
|
| 234 |
-
paginated_results = all_recommendations[start_index:end_index]
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
}
|
| 243 |
|
| 244 |
# --------------------------------------------------------------------
|
| 245 |
# ✅ NEW CHAT ENDPOINTS WITH MEMORY
|
|
|
|
| 50 |
|
| 51 |
class RecommendationResponse(BaseModel):
|
| 52 |
recommendations: List[SimpleRecommendation]
|
|
|
|
|
|
|
| 53 |
|
| 54 |
class StatusResponse(BaseModel):
|
| 55 |
status: str
|
|
|
|
| 176 |
return {"status": "success", "internship_id": internship.id}
|
| 177 |
|
| 178 |
@app.post("/profile-recommendations", response_model=RecommendationResponse)
|
| 179 |
+
def get_profile_recommendations(profile: UserProfile):
|
| 180 |
if chroma_collection is None or encoder is None:
|
| 181 |
raise HTTPException(status_code=503, detail="Server is not ready.")
|
| 182 |
|
| 183 |
query_text = f"Skills: {', '.join(profile.skills)}. Sectors: {', '.join(profile.sectors)}"
|
| 184 |
query_embedding = encoder.encode([query_text])[0].tolist()
|
| 185 |
|
|
|
|
| 186 |
results = chroma_collection.query(
|
| 187 |
query_embeddings=[query_embedding],
|
| 188 |
+
n_results=random.randint(3, 5) # Get 3 to 5 results
|
| 189 |
)
|
| 190 |
|
| 191 |
+
recommendations = []
|
| 192 |
ids = results.get('ids', [[]])[0]
|
| 193 |
distances = results.get('distances', [[]])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
for i, internship_id in enumerate(ids):
|
| 196 |
+
recommendations.append({
|
| 197 |
+
"internship_id": internship_id,
|
| 198 |
+
"score": 1 - distances[i]
|
| 199 |
+
})
|
| 200 |
+
|
| 201 |
+
return {"recommendations": recommendations}
|
| 202 |
|
| 203 |
@app.post("/search", response_model=RecommendationResponse)
|
| 204 |
+
def search_internships(search: SearchQuery):
|
| 205 |
if chroma_collection is None or encoder is None:
|
| 206 |
raise HTTPException(status_code=503, detail="Server is not ready.")
|
| 207 |
+
|
| 208 |
query_embedding = encoder.encode([search.query])[0].tolist()
|
| 209 |
+
|
| 210 |
results = chroma_collection.query(
|
| 211 |
query_embeddings=[query_embedding],
|
| 212 |
+
n_results=random.randint(3, 5) # Get 3 to 5 results
|
| 213 |
)
|
| 214 |
|
| 215 |
+
recommendations = []
|
| 216 |
ids = results.get('ids', [[]])[0]
|
| 217 |
distances = results.get('distances', [[]])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
for i, internship_id in enumerate(ids):
|
| 220 |
+
recommendations.append({
|
| 221 |
+
"internship_id": internship_id,
|
| 222 |
+
"score": 1 - distances[i]
|
| 223 |
+
})
|
| 224 |
+
|
| 225 |
+
return {"recommendations": recommendations}
|
| 226 |
|
| 227 |
# --------------------------------------------------------------------
|
| 228 |
# ✅ NEW CHAT ENDPOINTS WITH MEMORY
|