File size: 2,764 Bytes
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41bd215
 
63bcd5a
 
 
 
 
 
 
 
8d49ff0
eb029d7
8d49ff0
63bcd5a
 
d4e242f
63bcd5a
d4e242f
8d49ff0
 
63bcd5a
 
4ffaaf7
 
41bd215
63bcd5a
 
41bd215
63bcd5a
 
 
 
41bd215
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pandas as pd
from fastapi import HTTPException

from src.similarity_model import find_similar_projects
from src.similarity_model import extract_features


def analyze_project(
    title: str,
    description: str,
    abstract: str = "",
    features=None,
    top_k: int = 5
):

    if features is None:
        features = []

    full_text = f"{title}. {abstract}. {description}"

    auto_features = extract_features(full_text)

    merged = []
    seen = set()

    for item in features + auto_features:
        val = str(item).strip().lower()

        if val and val not in seen:
            seen.add(val)
            merged.append(val)

    results = find_similar_projects(
        title=title,
        description=f"{abstract} {description}",
        features=merged,
        top_k=top_k
    )

    if not isinstance(results, pd.DataFrame) or len(results) == 0:
        return {
            "message": "No similar projects found",
            "extracted_features": merged,
            "overall_originality_score": 100.0
        }

    # -----------------------------------
    # رجع Top K كله
    # -----------------------------------
    top_projects = []

    for _, row in results.iterrows():
        orig_score = round(float(row.get("originality_score", 0)), 2)
        sim_percent = round(float(row.get("hybrid_score", 0)) * 100, 2)

        top_projects.append({
            "project_title": row.get("project_title", ""),
            "project_features": row.get("candidate_features", []),
            "matched_features": row.get("matched_features", []),
            "unique_features": row.get("unique_candidate_features", []),
            "similarity_score": sim_percent,
            "final_originality_score": orig_score
        })

    # Overall = worst-case originality (against the most similar project)
    overall_originality_score = top_projects[0]["final_originality_score"]

    return {
        "extracted_features": merged,
        "overall_originality_score": overall_originality_score,
        "top_similar_projects": top_projects
    }



def chat_with_llm(user_id: str, message: str):
    try:
        from src.recommendation_engine.chatbot_engine import chatbot
        from src.recommendation_engine.llm_client import LLMProviderError
    except Exception as exc:
        raise HTTPException(
            status_code=503,
            detail=f"LLM service could not start: {exc}"
        )

    try:
        response = chatbot(
            user_id=user_id,
            user_input=message
        )
    except LLMProviderError as exc:
        raise HTTPException(
            status_code=exc.status_code,
            detail=exc.message
        )

    return {
        "user_id": user_id,
        "response": response
    }