Spaces:
Runtime error
Runtime error
Update: services.py
Browse files- api/services.py +100 -100
api/services.py
CHANGED
|
@@ -1,100 +1,100 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
from fastapi import HTTPException
|
| 3 |
-
|
| 4 |
-
from src.similarity_model import find_similar_projects
|
| 5 |
-
from src.similarity_model import extract_features
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def analyze_project(
|
| 9 |
-
title: str,
|
| 10 |
-
description: str,
|
| 11 |
-
abstract: str = "",
|
| 12 |
-
features=None,
|
| 13 |
-
top_k: int = 5
|
| 14 |
-
):
|
| 15 |
-
|
| 16 |
-
if features is None:
|
| 17 |
-
features = []
|
| 18 |
-
|
| 19 |
-
full_text = f"{title}. {abstract}. {description}"
|
| 20 |
-
|
| 21 |
-
auto_features = extract_features(full_text)
|
| 22 |
-
|
| 23 |
-
merged = []
|
| 24 |
-
seen = set()
|
| 25 |
-
|
| 26 |
-
for item in features + auto_features:
|
| 27 |
-
val = str(item).strip().lower()
|
| 28 |
-
|
| 29 |
-
if val and val not in seen:
|
| 30 |
-
seen.add(val)
|
| 31 |
-
merged.append(val)
|
| 32 |
-
|
| 33 |
-
results = find_similar_projects(
|
| 34 |
-
title=title,
|
| 35 |
-
description=f"{abstract} {description}",
|
| 36 |
-
features=merged,
|
| 37 |
-
top_k=top_k
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
if not isinstance(results, pd.DataFrame) or len(results) == 0:
|
| 41 |
-
return {
|
| 42 |
-
"message": "No similar projects found",
|
| 43 |
-
"extracted_features": merged,
|
| 44 |
-
"overall_originality_score": 100.0
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
# -----------------------------------
|
| 48 |
-
# رجع Top K كله
|
| 49 |
-
# -----------------------------------
|
| 50 |
-
top_projects = []
|
| 51 |
-
|
| 52 |
-
for _, row in results.iterrows():
|
| 53 |
-
orig_score = round(float(row.get("originality_score", 0)), 2)
|
| 54 |
-
sim_percent = round(float(row.get("hybrid_score", 0)) * 100, 2)
|
| 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 |
-
})
|
| 64 |
-
|
| 65 |
-
# Overall =
|
| 66 |
-
overall_originality_score =
|
| 67 |
-
|
| 68 |
-
return {
|
| 69 |
-
"extracted_features": merged,
|
| 70 |
-
"overall_originality_score": overall_originality_score,
|
| 71 |
-
"top_similar_projects": top_projects
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def chat_with_llm(user_id: str, message: str):
|
| 77 |
-
try:
|
| 78 |
-
from src.recommendation_engine.chatbot_engine import chatbot
|
| 79 |
-
from src.recommendation_engine.llm_client import LLMProviderError
|
| 80 |
-
except Exception as exc:
|
| 81 |
-
raise HTTPException(
|
| 82 |
-
status_code=503,
|
| 83 |
-
detail=f"LLM service could not start: {exc}"
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
try:
|
| 87 |
-
response = chatbot(
|
| 88 |
-
user_id=user_id,
|
| 89 |
-
user_input=message
|
| 90 |
-
)
|
| 91 |
-
except LLMProviderError as exc:
|
| 92 |
-
raise HTTPException(
|
| 93 |
-
status_code=exc.status_code,
|
| 94 |
-
detail=exc.message
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
return {
|
| 98 |
-
"user_id": user_id,
|
| 99 |
-
"response": response
|
| 100 |
-
}
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from fastapi import HTTPException
|
| 3 |
+
|
| 4 |
+
from src.similarity_model import find_similar_projects
|
| 5 |
+
from src.similarity_model import extract_features
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def analyze_project(
|
| 9 |
+
title: str,
|
| 10 |
+
description: str,
|
| 11 |
+
abstract: str = "",
|
| 12 |
+
features=None,
|
| 13 |
+
top_k: int = 5
|
| 14 |
+
):
|
| 15 |
+
|
| 16 |
+
if features is None:
|
| 17 |
+
features = []
|
| 18 |
+
|
| 19 |
+
full_text = f"{title}. {abstract}. {description}"
|
| 20 |
+
|
| 21 |
+
auto_features = extract_features(full_text)
|
| 22 |
+
|
| 23 |
+
merged = []
|
| 24 |
+
seen = set()
|
| 25 |
+
|
| 26 |
+
for item in features + auto_features:
|
| 27 |
+
val = str(item).strip().lower()
|
| 28 |
+
|
| 29 |
+
if val and val not in seen:
|
| 30 |
+
seen.add(val)
|
| 31 |
+
merged.append(val)
|
| 32 |
+
|
| 33 |
+
results = find_similar_projects(
|
| 34 |
+
title=title,
|
| 35 |
+
description=f"{abstract} {description}",
|
| 36 |
+
features=merged,
|
| 37 |
+
top_k=top_k
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if not isinstance(results, pd.DataFrame) or len(results) == 0:
|
| 41 |
+
return {
|
| 42 |
+
"message": "No similar projects found",
|
| 43 |
+
"extracted_features": merged,
|
| 44 |
+
"overall_originality_score": 100.0
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# -----------------------------------
|
| 48 |
+
# رجع Top K كله
|
| 49 |
+
# -----------------------------------
|
| 50 |
+
top_projects = []
|
| 51 |
+
|
| 52 |
+
for _, row in results.iterrows():
|
| 53 |
+
orig_score = round(float(row.get("originality_score", 0)), 2)
|
| 54 |
+
sim_percent = round(float(row.get("hybrid_score", 0)) * 100, 2)
|
| 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 |
+
})
|
| 64 |
+
|
| 65 |
+
# Overall = largest (maximum) originality score among the top matches
|
| 66 |
+
overall_originality_score = max(p["final_originality_score"] for p in top_projects)
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"extracted_features": merged,
|
| 70 |
+
"overall_originality_score": overall_originality_score,
|
| 71 |
+
"top_similar_projects": top_projects
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def chat_with_llm(user_id: str, message: str):
|
| 77 |
+
try:
|
| 78 |
+
from src.recommendation_engine.chatbot_engine import chatbot
|
| 79 |
+
from src.recommendation_engine.llm_client import LLMProviderError
|
| 80 |
+
except Exception as exc:
|
| 81 |
+
raise HTTPException(
|
| 82 |
+
status_code=503,
|
| 83 |
+
detail=f"LLM service could not start: {exc}"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
response = chatbot(
|
| 88 |
+
user_id=user_id,
|
| 89 |
+
user_input=message
|
| 90 |
+
)
|
| 91 |
+
except LLMProviderError as exc:
|
| 92 |
+
raise HTTPException(
|
| 93 |
+
status_code=exc.status_code,
|
| 94 |
+
detail=exc.message
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
return {
|
| 98 |
+
"user_id": user_id,
|
| 99 |
+
"response": response
|
| 100 |
+
}
|