Spaces:
Sleeping
Sleeping
feat: implement hybrid similarity ranking model and API service integration for project comparison
Browse files- api/services.py +2 -160
- src/similarity_model/hybrid_ranker.py +4 -1
api/services.py
CHANGED
|
@@ -1,162 +1,3 @@
|
|
| 1 |
-
# # api/services.py
|
| 2 |
-
|
| 3 |
-
# import pandas as pd
|
| 4 |
-
|
| 5 |
-
# from src.similarity_engine import find_similar_projects
|
| 6 |
-
# from src.preprocessing import extract_features
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# # =====================================================
|
| 10 |
-
# # Main Analyze Service
|
| 11 |
-
# # =====================================================
|
| 12 |
-
# def analyze_project(
|
| 13 |
-
# title: str,
|
| 14 |
-
# description: str,
|
| 15 |
-
# abstract: str = "",
|
| 16 |
-
# features=None,
|
| 17 |
-
# top_k: int = 5
|
| 18 |
-
# ):
|
| 19 |
-
# """
|
| 20 |
-
# Full project analysis service
|
| 21 |
-
# """
|
| 22 |
-
|
| 23 |
-
# if features is None:
|
| 24 |
-
# features = []
|
| 25 |
-
|
| 26 |
-
# # ---------------------------------------------
|
| 27 |
-
# # Build text for auto feature extraction
|
| 28 |
-
# # ---------------------------------------------
|
| 29 |
-
# full_text = f"{title}. {abstract}. {description}"
|
| 30 |
-
|
| 31 |
-
# auto_features = extract_features(full_text)
|
| 32 |
-
|
| 33 |
-
# # merge manual + auto
|
| 34 |
-
# merged = []
|
| 35 |
-
# seen = set()
|
| 36 |
-
|
| 37 |
-
# for item in features + auto_features:
|
| 38 |
-
# val = str(item).strip().lower()
|
| 39 |
-
|
| 40 |
-
# if val and val not in seen:
|
| 41 |
-
# seen.add(val)
|
| 42 |
-
# merged.append(val)
|
| 43 |
-
|
| 44 |
-
# # ---------------------------------------------
|
| 45 |
-
# # Run similarity engine
|
| 46 |
-
# # ---------------------------------------------
|
| 47 |
-
# results = find_similar_projects(
|
| 48 |
-
# title=title,
|
| 49 |
-
# description=f"{abstract} {description}",
|
| 50 |
-
# features=merged,
|
| 51 |
-
# top_k=top_k
|
| 52 |
-
# )
|
| 53 |
-
|
| 54 |
-
# # ---------------------------------------------
|
| 55 |
-
# # Convert dataframe to json records
|
| 56 |
-
# # ---------------------------------------------
|
| 57 |
-
# if isinstance(results, pd.DataFrame):
|
| 58 |
-
# rows = results.to_dict(orient="records")
|
| 59 |
-
# else:
|
| 60 |
-
# rows = []
|
| 61 |
-
|
| 62 |
-
# return {
|
| 63 |
-
# "extracted_features": merged,
|
| 64 |
-
# "results": rows
|
| 65 |
-
# }
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# api/services.py
|
| 69 |
-
|
| 70 |
-
# import pandas as pd
|
| 71 |
-
|
| 72 |
-
# from src.similarity_model import find_similar_projects
|
| 73 |
-
# from src.similarity_model import extract_features
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# # =====================================================
|
| 77 |
-
# # Main Analyze Service
|
| 78 |
-
# # =====================================================
|
| 79 |
-
# def analyze_project(
|
| 80 |
-
# title: str,
|
| 81 |
-
# description: str,
|
| 82 |
-
# abstract: str = "",
|
| 83 |
-
# features=None,
|
| 84 |
-
# top_k: int = 5
|
| 85 |
-
# ):
|
| 86 |
-
# """
|
| 87 |
-
# Final clean API response
|
| 88 |
-
# Returns only user-needed metrics
|
| 89 |
-
# """
|
| 90 |
-
|
| 91 |
-
# if features is None:
|
| 92 |
-
# features = []
|
| 93 |
-
|
| 94 |
-
# # -------------------------------------------------
|
| 95 |
-
# # Build full text for automatic feature extraction
|
| 96 |
-
# # -------------------------------------------------
|
| 97 |
-
# full_text = f"{title}. {abstract}. {description}"
|
| 98 |
-
|
| 99 |
-
# auto_features = extract_features(full_text)
|
| 100 |
-
|
| 101 |
-
# # -------------------------------------------------
|
| 102 |
-
# # Merge manual + extracted features
|
| 103 |
-
# # -------------------------------------------------
|
| 104 |
-
# merged = []
|
| 105 |
-
# seen = set()
|
| 106 |
-
|
| 107 |
-
# for item in features + auto_features:
|
| 108 |
-
# val = str(item).strip().lower()
|
| 109 |
-
|
| 110 |
-
# if val and val not in seen:
|
| 111 |
-
# seen.add(val)
|
| 112 |
-
# merged.append(val)
|
| 113 |
-
|
| 114 |
-
# # -------------------------------------------------
|
| 115 |
-
# # Run similarity model
|
| 116 |
-
# # -------------------------------------------------
|
| 117 |
-
# results = find_similar_projects(
|
| 118 |
-
# title=title,
|
| 119 |
-
# description=f"{abstract} {description}",
|
| 120 |
-
# features=merged,
|
| 121 |
-
# top_k=top_k
|
| 122 |
-
# )
|
| 123 |
-
|
| 124 |
-
# # -------------------------------------------------
|
| 125 |
-
# # No results found
|
| 126 |
-
# # -------------------------------------------------
|
| 127 |
-
# if not isinstance(results, pd.DataFrame) or len(results) == 0:
|
| 128 |
-
# return {
|
| 129 |
-
# "message": "No similar projects found",
|
| 130 |
-
# "extracted_features": merged
|
| 131 |
-
# }
|
| 132 |
-
|
| 133 |
-
# # -------------------------------------------------
|
| 134 |
-
# # Take top matched project only
|
| 135 |
-
# # -------------------------------------------------
|
| 136 |
-
# top = results.iloc[0]
|
| 137 |
-
|
| 138 |
-
# return {
|
| 139 |
-
# "extracted_features": merged,
|
| 140 |
-
|
| 141 |
-
# "matched_features": top.get(
|
| 142 |
-
# "matched_features", []
|
| 143 |
-
# ),
|
| 144 |
-
|
| 145 |
-
# "unique_features": top.get(
|
| 146 |
-
# "unique_query_features", []
|
| 147 |
-
# ),
|
| 148 |
-
|
| 149 |
-
# "hybrid_similarity": round(
|
| 150 |
-
# float(top.get("hybrid_score", 0)),
|
| 151 |
-
# 4
|
| 152 |
-
# ),
|
| 153 |
-
|
| 154 |
-
# "final_originality_score": round(
|
| 155 |
-
# float(top.get("originality_score", 0)),
|
| 156 |
-
# 4
|
| 157 |
-
# )
|
| 158 |
-
# }
|
| 159 |
-
|
| 160 |
import pandas as pd
|
| 161 |
from fastapi import HTTPException
|
| 162 |
|
|
@@ -214,8 +55,9 @@ def analyze_project(
|
|
| 214 |
|
| 215 |
top_projects.append({
|
| 216 |
"project_title": row.get("project_title", ""),
|
|
|
|
| 217 |
"matched_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|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
from fastapi import HTTPException
|
| 3 |
|
|
|
|
| 55 |
|
| 56 |
top_projects.append({
|
| 57 |
"project_title": row.get("project_title", ""),
|
| 58 |
+
"project_features": row.get("candidate_features", []),
|
| 59 |
"matched_features": row.get("matched_features", []),
|
| 60 |
+
"unique_features": row.get("unique_candidate_features", []),
|
| 61 |
"similarity_score": sim_percent,
|
| 62 |
"final_originality_score": orig_score
|
| 63 |
})
|
src/similarity_model/hybrid_ranker.py
CHANGED
|
@@ -287,7 +287,10 @@ def compare_single_candidate(
|
|
| 287 |
feature_result["unique_a"],
|
| 288 |
|
| 289 |
"unique_candidate_features":
|
| 290 |
-
feature_result["unique_b"]
|
|
|
|
|
|
|
|
|
|
| 291 |
}
|
| 292 |
|
| 293 |
|
|
|
|
| 287 |
feature_result["unique_a"],
|
| 288 |
|
| 289 |
"unique_candidate_features":
|
| 290 |
+
feature_result["unique_b"],
|
| 291 |
+
|
| 292 |
+
"candidate_features":
|
| 293 |
+
candidate_features
|
| 294 |
}
|
| 295 |
|
| 296 |
|