Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -144,6 +144,57 @@ for column in P:
|
|
| 144 |
print("Loaded Dataset B from Hugging Face")
|
| 145 |
print("Property inventory rows:", len(B))
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
# ============================================================
|
| 149 |
# LOAD DATASET A FOR REPRESENTATIVE INVESTOR DESCRIPTIONS
|
|
@@ -377,6 +428,139 @@ def percentile_rank(series, ascending=True):
|
|
| 377 |
return ranks if ascending else 1 - ranks
|
| 378 |
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
def recommend_top_3(
|
| 381 |
budget_level,
|
| 382 |
max_budget_usd,
|
|
@@ -405,8 +589,11 @@ def recommend_top_3(
|
|
| 405 |
max_budget_usd = float(max_budget_usd)
|
| 406 |
|
| 407 |
if not np.isfinite(max_budget_usd) or max_budget_usd <= 0:
|
| 408 |
-
raise ValueError(
|
|
|
|
|
|
|
| 409 |
|
|
|
|
| 410 |
candidates = B.copy()
|
| 411 |
|
| 412 |
candidates["price"] = pd.to_numeric(
|
|
@@ -414,17 +601,13 @@ def recommend_top_3(
|
|
| 414 |
errors="coerce",
|
| 415 |
)
|
| 416 |
|
|
|
|
| 417 |
candidates = candidates[
|
| 418 |
candidates["price"].notna()
|
| 419 |
& (candidates["price"] > 0)
|
|
|
|
| 420 |
]
|
| 421 |
|
| 422 |
-
# Exact hard budget rule.
|
| 423 |
-
candidates = candidates[
|
| 424 |
-
candidates["price"] <= max_budget_usd
|
| 425 |
-
]
|
| 426 |
-
|
| 427 |
-
# Conservative investors never receive these properties.
|
| 428 |
if risk_profile == "conservative":
|
| 429 |
candidates = candidates[
|
| 430 |
(candidates["volatility_band"] != "high_volatility")
|
|
@@ -434,9 +617,29 @@ def recommend_top_3(
|
|
| 434 |
if candidates.empty:
|
| 435 |
return pd.DataFrame(
|
| 436 |
columns=RECOMMENDATION_FIELDS
|
| 437 |
-
+ [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
)
|
| 439 |
|
|
|
|
| 440 |
model_features = candidates[P].copy()
|
| 441 |
|
| 442 |
for column, value in answers.items():
|
|
@@ -447,17 +650,21 @@ def recommend_top_3(
|
|
| 447 |
prep.transform(model_features[F]),
|
| 448 |
)
|
| 449 |
|
|
|
|
| 450 |
tie_breaker = (
|
| 451 |
-
0.35 * percentile_rank(
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
+ 0.20 * percentile_rank(
|
| 454 |
candidates["price_volatility_percent"],
|
| 455 |
ascending=False,
|
| 456 |
)
|
| 457 |
+ 0.15
|
| 458 |
* (
|
| 459 |
-
1
|
| 460 |
-
- candidates["price"] / max_budget_usd
|
| 461 |
).clip(0, 1)
|
| 462 |
)
|
| 463 |
|
|
@@ -466,7 +673,7 @@ def recommend_top_3(
|
|
| 466 |
+ 0.15 * tie_breaker
|
| 467 |
)
|
| 468 |
|
| 469 |
-
|
| 470 |
[
|
| 471 |
"final_score",
|
| 472 |
"predicted_match_fit",
|
|
@@ -482,21 +689,33 @@ def recommend_top_3(
|
|
| 482 |
kind="mergesort",
|
| 483 |
)
|
| 484 |
|
| 485 |
-
|
|
|
|
|
|
|
| 486 |
|
| 487 |
-
assert (
|
| 488 |
-
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
return result[
|
| 492 |
RECOMMENDATION_FIELDS
|
| 493 |
+ [
|
| 494 |
"predicted_match_fit",
|
| 495 |
"final_score",
|
|
|
|
| 496 |
]
|
| 497 |
]
|
| 498 |
|
| 499 |
-
|
| 500 |
# ============================================================
|
| 501 |
# PART 5A - REGRESSION TEST
|
| 502 |
# ============================================================
|
|
|
|
| 144 |
print("Loaded Dataset B from Hugging Face")
|
| 145 |
print("Property inventory rows:", len(B))
|
| 146 |
|
| 147 |
+
# ============================================================
|
| 148 |
+
# LOAD SAVED DCN-v2 PROPERTY EMBEDDINGS FROM THE SPACE
|
| 149 |
+
# ============================================================
|
| 150 |
+
|
| 151 |
+
EMBEDDINGS_PATH = Path(
|
| 152 |
+
"rematch_dataset_b_dcn_v2_embeddings.parquet"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
if not EMBEDDINGS_PATH.exists():
|
| 156 |
+
raise FileNotFoundError(
|
| 157 |
+
"Missing rematch_dataset_b_dcn_v2_embeddings.parquet. "
|
| 158 |
+
"Upload it to the root of this Hugging Face Space."
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
EMBEDDINGS = pd.read_parquet(EMBEDDINGS_PATH)
|
| 162 |
+
|
| 163 |
+
EMBEDDING_COLS = sorted(
|
| 164 |
+
[
|
| 165 |
+
column
|
| 166 |
+
for column in EMBEDDINGS.columns
|
| 167 |
+
if column.startswith("dcn_v2_property_embedding_")
|
| 168 |
+
],
|
| 169 |
+
key=lambda column: int(column.rsplit("_", 1)[1]),
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
assert len(EMBEDDING_COLS) == 60, (
|
| 173 |
+
f"Expected 60 DCN-v2 embedding columns, found {len(EMBEDDING_COLS)}."
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
assert "id" in EMBEDDINGS.columns, (
|
| 177 |
+
"The embedding Parquet file must contain an id column."
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# IDs are the bridge between the vector index and Dataset B.
|
| 181 |
+
B["id"] = B["id"].astype(str)
|
| 182 |
+
EMBEDDINGS["id"] = EMBEDDINGS["id"].astype(str)
|
| 183 |
+
|
| 184 |
+
EMBEDDINGS = EMBEDDINGS[
|
| 185 |
+
["id"] + EMBEDDING_COLS
|
| 186 |
+
].drop_duplicates("id")
|
| 187 |
+
|
| 188 |
+
missing_embedding_ids = set(B["id"]) - set(EMBEDDINGS["id"])
|
| 189 |
+
|
| 190 |
+
assert not missing_embedding_ids, (
|
| 191 |
+
"Some Dataset B properties have no saved embedding. "
|
| 192 |
+
f"Missing count: {len(missing_embedding_ids)}"
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
print("Loaded DCN-v2 property embedding index from Parquet")
|
| 196 |
+
print("Embedding rows:", len(EMBEDDINGS))
|
| 197 |
+
print("Embedding dimensions:", len(EMBEDDING_COLS))
|
| 198 |
|
| 199 |
# ============================================================
|
| 200 |
# LOAD DATASET A FOR REPRESENTATIVE INVESTOR DESCRIPTIONS
|
|
|
|
| 428 |
return ranks if ascending else 1 - ranks
|
| 429 |
|
| 430 |
|
| 431 |
+
def cosine_similarity(vector_a, vector_b):
|
| 432 |
+
"""Cosine similarity for two already numeric property vectors."""
|
| 433 |
+
denominator = np.linalg.norm(vector_a) * np.linalg.norm(vector_b)
|
| 434 |
+
|
| 435 |
+
if denominator == 0:
|
| 436 |
+
return 0.0
|
| 437 |
+
|
| 438 |
+
return float(np.dot(vector_a, vector_b) / denominator)
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
def select_diverse_top_3(ranked_candidates, diversity_weight=0.015):
|
| 442 |
+
"""
|
| 443 |
+
Select three strong recommendations while using the saved DCN-v2
|
| 444 |
+
property embeddings to avoid returning three almost identical assets.
|
| 445 |
+
|
| 446 |
+
DCN-v2 fit score remains the main ranking signal.
|
| 447 |
+
The embedding similarity penalty is intentionally small.
|
| 448 |
+
"""
|
| 449 |
+
pool = ranked_candidates.head(100).copy()
|
| 450 |
+
|
| 451 |
+
selected_rows = []
|
| 452 |
+
selected_vectors = []
|
| 453 |
+
|
| 454 |
+
for _, row in pool.iterrows():
|
| 455 |
+
candidate_vector = row[EMBEDDING_COLS].to_numpy(
|
| 456 |
+
dtype=np.float32
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
if not selected_vectors:
|
| 460 |
+
max_similarity = 0.0
|
| 461 |
+
else:
|
| 462 |
+
max_similarity = max(
|
| 463 |
+
cosine_similarity(candidate_vector, selected_vector)
|
| 464 |
+
for selected_vector in selected_vectors
|
| 465 |
+
)
|
| 466 |
+
|
| 467 |
+
selection_score = (
|
| 468 |
+
float(row["final_score"])
|
| 469 |
+
- diversity_weight * max_similarity
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
selected_rows.append(
|
| 473 |
+
(
|
| 474 |
+
selection_score,
|
| 475 |
+
row,
|
| 476 |
+
candidate_vector,
|
| 477 |
+
)
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
# At each round, take the best currently available candidate.
|
| 481 |
+
best = max(
|
| 482 |
+
selected_rows,
|
| 483 |
+
key=lambda item: item[0],
|
| 484 |
+
)
|
| 485 |
+
|
| 486 |
+
# Only select when this row is the best remaining option.
|
| 487 |
+
if best[1]["id"] == row["id"]:
|
| 488 |
+
selected_vectors.append(candidate_vector)
|
| 489 |
+
|
| 490 |
+
if len(selected_vectors) == 3:
|
| 491 |
+
break
|
| 492 |
+
|
| 493 |
+
# Keep only rows not already selected.
|
| 494 |
+
selected_ids = {
|
| 495 |
+
str(vector_row[1]["id"])
|
| 496 |
+
for vector_row in selected_rows
|
| 497 |
+
if any(
|
| 498 |
+
np.array_equal(
|
| 499 |
+
vector_row[2],
|
| 500 |
+
selected_vector,
|
| 501 |
+
)
|
| 502 |
+
for selected_vector in selected_vectors
|
| 503 |
+
)
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
pool = pool[
|
| 507 |
+
~pool["id"].astype(str).isin(selected_ids)
|
| 508 |
+
]
|
| 509 |
+
|
| 510 |
+
# A clearer greedy implementation, preserving original DCN score.
|
| 511 |
+
chosen = []
|
| 512 |
+
chosen_vectors = []
|
| 513 |
+
remaining = ranked_candidates.head(100).copy()
|
| 514 |
+
|
| 515 |
+
while len(chosen) < 3 and not remaining.empty:
|
| 516 |
+
selection_scores = []
|
| 517 |
+
|
| 518 |
+
for index, row in remaining.iterrows():
|
| 519 |
+
candidate_vector = row[EMBEDDING_COLS].to_numpy(
|
| 520 |
+
dtype=np.float32
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
if chosen_vectors:
|
| 524 |
+
max_similarity = max(
|
| 525 |
+
cosine_similarity(
|
| 526 |
+
candidate_vector,
|
| 527 |
+
selected_vector,
|
| 528 |
+
)
|
| 529 |
+
for selected_vector in chosen_vectors
|
| 530 |
+
)
|
| 531 |
+
else:
|
| 532 |
+
max_similarity = 0.0
|
| 533 |
+
|
| 534 |
+
selection_score = (
|
| 535 |
+
float(row["final_score"])
|
| 536 |
+
- diversity_weight * max_similarity
|
| 537 |
+
)
|
| 538 |
+
|
| 539 |
+
selection_scores.append(
|
| 540 |
+
(index, selection_score, max_similarity)
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
best_index, best_selection_score, best_similarity = max(
|
| 544 |
+
selection_scores,
|
| 545 |
+
key=lambda item: item[1],
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
selected_row = remaining.loc[best_index].copy()
|
| 549 |
+
selected_row["embedding_similarity_penalty"] = best_similarity
|
| 550 |
+
selected_row["selection_score"] = best_selection_score
|
| 551 |
+
|
| 552 |
+
chosen.append(selected_row)
|
| 553 |
+
chosen_vectors.append(
|
| 554 |
+
selected_row[EMBEDDING_COLS].to_numpy(
|
| 555 |
+
dtype=np.float32
|
| 556 |
+
)
|
| 557 |
+
)
|
| 558 |
+
|
| 559 |
+
remaining = remaining.drop(index=best_index)
|
| 560 |
+
|
| 561 |
+
return pd.DataFrame(chosen)
|
| 562 |
+
|
| 563 |
+
|
| 564 |
def recommend_top_3(
|
| 565 |
budget_level,
|
| 566 |
max_budget_usd,
|
|
|
|
| 589 |
max_budget_usd = float(max_budget_usd)
|
| 590 |
|
| 591 |
if not np.isfinite(max_budget_usd) or max_budget_usd <= 0:
|
| 592 |
+
raise ValueError(
|
| 593 |
+
"max_budget_usd must be greater than zero."
|
| 594 |
+
)
|
| 595 |
|
| 596 |
+
# Dataset B remains the source of truth for every property.
|
| 597 |
candidates = B.copy()
|
| 598 |
|
| 599 |
candidates["price"] = pd.to_numeric(
|
|
|
|
| 601 |
errors="coerce",
|
| 602 |
)
|
| 603 |
|
| 604 |
+
# Strict eligibility rules run before any recommendation scoring.
|
| 605 |
candidates = candidates[
|
| 606 |
candidates["price"].notna()
|
| 607 |
& (candidates["price"] > 0)
|
| 608 |
+
& (candidates["price"] <= max_budget_usd)
|
| 609 |
]
|
| 610 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
if risk_profile == "conservative":
|
| 612 |
candidates = candidates[
|
| 613 |
(candidates["volatility_band"] != "high_volatility")
|
|
|
|
| 617 |
if candidates.empty:
|
| 618 |
return pd.DataFrame(
|
| 619 |
columns=RECOMMENDATION_FIELDS
|
| 620 |
+
+ [
|
| 621 |
+
"predicted_match_fit",
|
| 622 |
+
"final_score",
|
| 623 |
+
"selection_score",
|
| 624 |
+
]
|
| 625 |
+
)
|
| 626 |
+
|
| 627 |
+
# Join each live Dataset B property to its saved DCN-v2 embedding.
|
| 628 |
+
# Only the embedding vectors come from the Parquet file.
|
| 629 |
+
candidates = candidates.merge(
|
| 630 |
+
EMBEDDINGS,
|
| 631 |
+
on="id",
|
| 632 |
+
how="inner",
|
| 633 |
+
validate="one_to_one",
|
| 634 |
+
)
|
| 635 |
+
|
| 636 |
+
if candidates.empty:
|
| 637 |
+
raise RuntimeError(
|
| 638 |
+
"No eligible Dataset B properties could be matched "
|
| 639 |
+
"to the saved embedding index."
|
| 640 |
)
|
| 641 |
|
| 642 |
+
# DCN-v2 computes personalized match quality from the five answers.
|
| 643 |
model_features = candidates[P].copy()
|
| 644 |
|
| 645 |
for column, value in answers.items():
|
|
|
|
| 650 |
prep.transform(model_features[F]),
|
| 651 |
)
|
| 652 |
|
| 653 |
+
# Deterministic property-quality tie-breaker.
|
| 654 |
tie_breaker = (
|
| 655 |
+
0.35 * percentile_rank(
|
| 656 |
+
candidates["gross_rental_yield_percent"]
|
| 657 |
+
)
|
| 658 |
+
+ 0.30 * percentile_rank(
|
| 659 |
+
candidates["value_forecast_12months"]
|
| 660 |
+
)
|
| 661 |
+ 0.20 * percentile_rank(
|
| 662 |
candidates["price_volatility_percent"],
|
| 663 |
ascending=False,
|
| 664 |
)
|
| 665 |
+ 0.15
|
| 666 |
* (
|
| 667 |
+
1 - candidates["price"] / max_budget_usd
|
|
|
|
| 668 |
).clip(0, 1)
|
| 669 |
)
|
| 670 |
|
|
|
|
| 673 |
+ 0.15 * tie_breaker
|
| 674 |
)
|
| 675 |
|
| 676 |
+
ranked_candidates = candidates.sort_values(
|
| 677 |
[
|
| 678 |
"final_score",
|
| 679 |
"predicted_match_fit",
|
|
|
|
| 689 |
kind="mergesort",
|
| 690 |
)
|
| 691 |
|
| 692 |
+
# The saved Parquet vectors now actively influence the final three:
|
| 693 |
+
# selected results remain high-fit, but are less repetitive.
|
| 694 |
+
result = select_diverse_top_3(ranked_candidates)
|
| 695 |
|
| 696 |
+
assert len(result) <= 3
|
| 697 |
+
assert (result["price"] <= max_budget_usd).all(), (
|
| 698 |
+
"Exact budget guard failed"
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
+
if risk_profile == "conservative":
|
| 702 |
+
assert not (
|
| 703 |
+
result["volatility_band"] == "high_volatility"
|
| 704 |
+
).any(), "Conservative volatility guard failed"
|
| 705 |
+
|
| 706 |
+
assert not (
|
| 707 |
+
result["forecast_band"] == "negative_forecast"
|
| 708 |
+
).any(), "Conservative forecast guard failed"
|
| 709 |
|
| 710 |
return result[
|
| 711 |
RECOMMENDATION_FIELDS
|
| 712 |
+ [
|
| 713 |
"predicted_match_fit",
|
| 714 |
"final_score",
|
| 715 |
+
"selection_score",
|
| 716 |
]
|
| 717 |
]
|
| 718 |
|
|
|
|
| 719 |
# ============================================================
|
| 720 |
# PART 5A - REGRESSION TEST
|
| 721 |
# ============================================================
|