File size: 3,428 Bytes
0914e96
 
b901ed7
0914e96
b901ed7
 
 
0914e96
b901ed7
 
 
 
 
0914e96
 
b901ed7
0914e96
b901ed7
 
 
 
0914e96
 
b901ed7
0914e96
b901ed7
 
 
 
 
 
0914e96
 
 
b901ed7
0914e96
b901ed7
0914e96
b901ed7
 
 
0914e96
b901ed7
0914e96
b901ed7
 
0914e96
b901ed7
 
 
 
 
0914e96
 
 
 
b901ed7
0914e96
 
 
b901ed7
0914e96
 
b901ed7
0914e96
 
 
b901ed7
0914e96
 
 
b901ed7
0914e96
b901ed7
0914e96
 
 
b901ed7
0914e96
 
b901ed7
0914e96
b901ed7
 
 
 
 
 
 
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
import joblib
import pandas as pd
import numpy as np
from typing import List, Dict
import traceback

print(">>> Loading ai-service/core/predictor.py (Version: FINAL, SAFE LOAD)")

# --- SAFE MODEL LOADING ---
# Hum 'Exception' use kar rahe hain taaki version mismatch ya missing file 
# se server crash na ho.
influencer_pipeline = None
performance_pipeline = None

try:
    print("--- Loading Influencer Matcher Model... ---")
    influencer_pipeline = joblib.load('models/influencer_matcher_v1.joblib')
    print("✅ Influencer Matcher loaded successfully.")
except Exception as e:
    print(f"⚠️ Warning: Influencer Matcher model failed to load: {e}")
    # Continue running, don't crash

try:
    print("--- Loading Performance Predictor Model... ---")
    performance_pipeline = joblib.load('models/performance_predictor_v1.joblib')
    print("✅ Performance Predictor loaded successfully.")
except Exception as e:
    print(f"⚠️ Warning: Performance Predictor model failed to load: {e}")
    # Continue running

# --- LOGIC ---

def rank_influencers_by_match(influencers: List[Dict], campaign_details: Dict, top_n: int = 5) -> List[Dict]:
    """
    Influencers ko rank karta hai. Agar model nahi hai toh basic fallback use karega.
    """
    # 1. Agar model load nahi hua ya data khali hai, toh empty list bhejo (Crash mat karo)
    if not influencers or influencer_pipeline is None:
        print("ℹ️ Matcher skipped: No model or no influencers provided.")
        return influencers[:top_n] # Fallback: Return original list as is

    try:
        print(f"--- Predictor: Ranking {len(influencers)} influencers...")
        
        # DataFrame Prepare
        influencer_df = pd.DataFrame(influencers)
        
        # Safe Feature Extraction
        features = influencer_df[['category', 'bio']].copy() if 'bio' in influencer_df else pd.DataFrame()
        if features.empty: return influencers[:top_n]

        features['niche'] = campaign_details.get('category', 'General')
        features['country'] = campaign_details.get('location', 'USA')
        features['followers'] = campaign_details.get('followers', 10000)
        features['engagement_rate'] = campaign_details.get('engagement_rate', 0.03)
        
        # Prediction
        match_scores = influencer_pipeline.predict(features)
        influencer_df['match_score'] = match_scores
        
        # Sorting
        top_influencers_df = influencer_df.sort_values(by='match_score', ascending=False).head(top_n)

        # Cleanup Result
        result_cols = ['id', 'name', 'handle', 'followers', 'category', 'bio']
        final_cols = [col for col in result_cols if col in top_influencers_df.columns]
        
        results = top_influencers_df[final_cols].to_dict(orient='records')
        return results

    except Exception as e:
        print(f"❌ Ranking Error: {e}")
        traceback.print_exc()
        return influencers[:top_n] # Error aane par original list wapas kardo

def predict_performance(data: dict) -> int:
    """
    Campaign performance predict karta hai.
    """
    if performance_pipeline is None:
        return 50000 # Default/Safe Value

    try:
        df = pd.DataFrame(data, index=[0])
        prediction = performance_pipeline.predict(df)
        return int(prediction[0])
    except Exception as e:
        print(f"❌ Performance Prediction Error: {e}")
        return 50000 # Default/Safe Value