File size: 6,724 Bytes
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
4552666
63bcd5a
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import logging
from typing import List, Dict, Any, Set

from src.recommendation_engine.context_builder import build_project_context
from src.recommendation_engine.prompt_builder import build_feature_prompt
from src.recommendation_engine.llm_client import generate_text
from src.recommendation_engine.validator import validate_generated_list
from src.recommendation_engine.novelty_checker import is_feature_novel

from src.similarity_model import compare_two_ideas

from src.recommendation_engine.config import (
    DEFAULT_FEATURE_COUNT,
    GENERATION_BATCH_SIZE
)

logger = logging.getLogger(__name__)

MAX_RETRIES = 5

SIMILARITY_THRESHOLD_LOCAL = 0.82

def normalize(text: str) -> str:
    return " ".join(str(text).strip().lower().split())

GENERIC_PATTERNS = [
    "dashboard",
    "login",
    "signup",
    "authentication",
    "analytics module",
    "ai module",
    "admin panel",
    "settings page",
    "reports system",
    "user management"
]

def is_generic_feature(text: str) -> bool:

    low = normalize(text)

    if len(low.split()) < 2:
        return True

    for bad in GENERIC_PATTERNS:
        if bad in low:
            return True

    return False

def clean_features(features: List[str]) -> List[str]:

    final = []

    for f in features:

        clean = str(f).strip()

        if not clean:
            continue

        words = clean.split()

        
        if len(words) < 3 or len(words) > 10:
            continue

        if is_generic_feature(clean):
            continue

        final.append(clean)

    return final

def is_duplicate_local(feature: str, existing: List[str]) -> bool:

    for old in existing:

        score = compare_two_ideas(feature, old)

        if score >= SIMILARITY_THRESHOLD_LOCAL:
            logger.info(f"[LOCAL DUPLICATE] {feature} ~ {old} ({score:.2f})")
            return True

    return False

def fallback_features(title: str) -> List[str]:

    title = (title or "").lower()

    
    
    
    if any(k in title for k in ["health", "hospital", "medical", "clinic"]):

        return [
            "Real-time patient monitoring",
            "Emergency alert notification system",
            "AI-assisted diagnosis support",
            "Medical data visualization dashboard",
            "Predictive patient risk analysis"
        ]

    
    
    
    if any(k in title for k in ["education", "learning", "student", "school"]):

        return [
            "Adaptive learning recommendation engine",
            "Student performance prediction system",
            "Automated assignment evaluation",
            "Gamified engagement tracking",
            "Personalized study path generation"
        ]

    
    
    
    if any(k in title for k in ["security", "cyber", "threat"]):

        return [
            "Real-time threat detection engine",
            "Behavior anomaly monitoring",
            "Automated attack alert system",
            "Security event visualization",
            "Risk prediction analytics"
        ]

    
    
    
    return [
        "Real-time intelligent monitoring",
        "Predictive analytics engine",
        "Smart recommendation system",
        "Automated decision support",
        "Dynamic performance optimization"
    ]

def generate_features(
    title: str,
    description: str,
    abstract: str = "",
    features: List[str] = None,
    previous_generated_features: List[str] = None,
    top_k: int = DEFAULT_FEATURE_COUNT
) -> Dict[str, Any]:

    features = features or []
    previous_generated_features = previous_generated_features or []

    top_k = max(1, min(top_k, 20))

    logger.info(f"Starting feature generation | title={title}")

    
    
    
    context = build_project_context(
        title=title,
        description=description,
        abstract=abstract,
        features=features
    )

    final_features: List[str] = []
    final_norm_set: Set[str] = set()

    existing_features = context.get("features", [])

    existing_norm = set(
        normalize(f)
        for f in existing_features
    )

    previous_norm = set(
        normalize(f)
        for f in previous_generated_features
    )

    attempts = 0

    
    
    
    while len(final_features) < top_k and attempts < MAX_RETRIES:

        attempts += 1

        logger.info(f"Generation attempt #{attempts}")

        generation_count = max(
            top_k * 4,
            GENERATION_BATCH_SIZE
        )

        
        
        
        prompt = build_feature_prompt(
            context=context,
            count=generation_count,
            previous_features=previous_generated_features
        )

        
        
        
        raw_text = generate_text(
            prompt,
            task="feature"
        )

        if not raw_text:
            logger.warning("Empty feature response")
            continue

        
        
        
        generated = validate_generated_list(
            text=raw_text,
            top_k=generation_count
        )

        generated = clean_features(generated)

        logger.info(f"Generated {len(generated)} candidate features")

        
        
        
        for feat in generated:

            norm = normalize(feat)

            if not norm:
                continue

            
            if (
                norm in final_norm_set
                or norm in existing_norm
                or norm in previous_norm
            ):
                continue

            
            if not is_feature_novel(feat, existing_features):
                continue

            
            if is_duplicate_local(feat, final_features):
                continue

            final_features.append(feat)
            final_norm_set.add(norm)

            logger.info(f"[NEW FEATURE] {feat}")

            if len(final_features) >= top_k:
                break

    
    
    
    if len(final_features) < top_k:

        logger.warning("Using fallback features")

        fallback = fallback_features(title)

        for feat in fallback:

            norm = normalize(feat)

            if (
                norm not in final_norm_set
                and norm not in existing_norm
            ):

                final_features.append(feat)
                final_norm_set.add(norm)

            if len(final_features) >= top_k:
                break

    logger.info(f"Final generated features: {final_features}")

    return {
        "project_title": context.get("project_title", title),
        "current_features": existing_features,
        "recommended_features": final_features,
        "originality_score": context.get("originality_score", 1.0),
        "similar_projects": context.get("similar_titles", [])
    }