|
|
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)") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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. |
|
|
""" |
|
|
|
|
|
if not influencers or influencer_pipeline is None: |
|
|
print("ℹ️ Matcher skipped: No model or no influencers provided.") |
|
|
return influencers[:top_n] |
|
|
|
|
|
try: |
|
|
print(f"--- Predictor: Ranking {len(influencers)} influencers...") |
|
|
|
|
|
|
|
|
influencer_df = pd.DataFrame(influencers) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
match_scores = influencer_pipeline.predict(features) |
|
|
influencer_df['match_score'] = match_scores |
|
|
|
|
|
|
|
|
top_influencers_df = influencer_df.sort_values(by='match_score', ascending=False).head(top_n) |
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
def predict_performance(data: dict) -> int: |
|
|
""" |
|
|
Campaign performance predict karta hai. |
|
|
""" |
|
|
if performance_pipeline is None: |
|
|
return 50000 |
|
|
|
|
|
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 |