Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -731,11 +731,458 @@ async def api_ia_configurer(request: dict):
|
|
| 731 |
}
|
| 732 |
except Exception as e:
|
| 733 |
return {"success": False, "error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
# =============================================================================
|
| 736 |
# INTÉGRATION AVEC LE SYSTÈME EXISTANT
|
| 737 |
# =============================================================================
|
| 738 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
# Ajoutez cette ligne APRÈS la création de cortex_optimise (vers la ligne ~400)
|
| 740 |
integration_apis = IntegrationAPIs()
|
| 741 |
|
|
|
|
| 731 |
}
|
| 732 |
except Exception as e:
|
| 733 |
return {"success": False, "error": str(e)}
|
| 734 |
+
# =============================================================================
|
| 735 |
+
# SYSTÈME AVANCÉ DE RECOMMANDATIONS PERSONNALISÉES
|
| 736 |
+
# =============================================================================
|
| 737 |
+
|
| 738 |
+
class SystemeRecommandations:
|
| 739 |
+
"""Système intelligent de recommandations basé sur le comportement utilisateur"""
|
| 740 |
+
|
| 741 |
+
def __init__(self):
|
| 742 |
+
self.historique_interactions = deque(maxlen=1000)
|
| 743 |
+
self.profil_utilisateur = {
|
| 744 |
+
"themes_frequents": defaultdict(int),
|
| 745 |
+
"horaires_activite": defaultdict(int),
|
| 746 |
+
"types_requetes": defaultdict(int),
|
| 747 |
+
"fonctionnalites_preferees": defaultdict(int),
|
| 748 |
+
"niveau_technique": "intermediaire" # debutant, intermediaire, expert
|
| 749 |
+
}
|
| 750 |
+
|
| 751 |
+
self.recommandations_actives = []
|
| 752 |
+
self.derniere_analyse = time.time()
|
| 753 |
+
self.score_personnalisation = 0.0
|
| 754 |
+
|
| 755 |
+
logger.info("🎯 Système de recommandations personnalisées initialisé")
|
| 756 |
+
|
| 757 |
+
def enregistrer_interaction(self, interaction: Dict[str, Any]):
|
| 758 |
+
"""Enregistre une interaction utilisateur pour l'analyse"""
|
| 759 |
+
self.historique_interactions.append(interaction)
|
| 760 |
+
|
| 761 |
+
# Mettre à jour le profil en temps réel
|
| 762 |
+
self._mettre_a_jour_profil(interaction)
|
| 763 |
+
|
| 764 |
+
# Générer de nouvelles recommandations si nécessaire
|
| 765 |
+
if time.time() - self.derniere_analyse > 300: # Toutes les 5 minutes
|
| 766 |
+
self.analyser_comportement()
|
| 767 |
+
self.derniere_analyse = time.time()
|
| 768 |
+
|
| 769 |
+
def analyser_comportement(self) -> Dict[str, Any]:
|
| 770 |
+
"""Analyse approfondie du comportement utilisateur"""
|
| 771 |
+
logger.info("🔍 Analyse du comportement utilisateur...")
|
| 772 |
+
|
| 773 |
+
if not self.historique_interactions:
|
| 774 |
+
return {"statut": "donnees_insuffisantes"}
|
| 775 |
+
|
| 776 |
+
# Analyse des thèmes fréquents
|
| 777 |
+
themes = self._analyser_themes_frequents()
|
| 778 |
+
|
| 779 |
+
# Analyse des patterns temporels
|
| 780 |
+
patterns_temps = self._analyser_patterns_temporels()
|
| 781 |
+
|
| 782 |
+
# Analyse du niveau technique
|
| 783 |
+
niveau_tech = self._evaluer_niveau_technique()
|
| 784 |
+
|
| 785 |
+
# Détection des besoins implicites
|
| 786 |
+
besoins = self._detecter_besoins_implicites()
|
| 787 |
+
|
| 788 |
+
# Générer les recommandations
|
| 789 |
+
self._generer_recommandations(themes, patterns_temps, niveau_tech, besoins)
|
| 790 |
+
|
| 791 |
+
return {
|
| 792 |
+
"themes_principaux": dict(sorted(themes.items(), key=lambda x: x[1], reverse=True)[:5]),
|
| 793 |
+
"patterns_temporels": patterns_temps,
|
| 794 |
+
"niveau_technique": niveau_tech,
|
| 795 |
+
"besoins_detectes": besoins,
|
| 796 |
+
"score_personnalisation": self.score_personnalisation,
|
| 797 |
+
"recommandations_actives": len(self.recommandations_actives)
|
| 798 |
+
}
|
| 799 |
+
|
| 800 |
+
def _analyser_themes_frequents(self) -> Dict[str, int]:
|
| 801 |
+
"""Analyse les thèmes les plus fréquents dans les interactions"""
|
| 802 |
+
themes = defaultdict(int)
|
| 803 |
+
|
| 804 |
+
for interaction in self.historique_interactions:
|
| 805 |
+
message = interaction.get("message", "").lower()
|
| 806 |
+
|
| 807 |
+
# Détection de thèmes
|
| 808 |
+
if any(mot in message for mot in ['cortex', 'mémoire', 'optimisation', 'performance']):
|
| 809 |
+
themes["optimisation_technique"] += 1
|
| 810 |
+
if any(mot in message for mot in ['bonjour', 'salut', 'hello', 'merci']):
|
| 811 |
+
themes["communication"] += 1
|
| 812 |
+
if any(mot in message for mot in ['état', 'statut', 'santé', 'santé']):
|
| 813 |
+
themes["monitoring_systeme"] += 1
|
| 814 |
+
if any(mot in message for mot in ['aide', 'comment', 'quoi', 'pourquoi']):
|
| 815 |
+
themes["recherche_aide"] += 1
|
| 816 |
+
if any(mot in message for mot in ['ia', 'intelligence', 'apprentissage']):
|
| 817 |
+
themes["ia_avancee"] += 1
|
| 818 |
+
if any(mot in message for mot in ['projet', 'developpement', 'code', 'programmation']):
|
| 819 |
+
themes["developpement"] += 1
|
| 820 |
+
|
| 821 |
+
# Mettre à jour le profil
|
| 822 |
+
for theme, count in themes.items():
|
| 823 |
+
self.profil_utilisateur["themes_frequents"][theme] += count
|
| 824 |
+
|
| 825 |
+
return themes
|
| 826 |
+
|
| 827 |
+
def _analyser_patterns_temporels(self) -> Dict[str, Any]:
|
| 828 |
+
"""Analyse les patterns d'utilisation temporels"""
|
| 829 |
+
heures_activite = defaultdict(int)
|
| 830 |
+
jours_activite = defaultdict(int)
|
| 831 |
+
|
| 832 |
+
for interaction in self.historique_interactions:
|
| 833 |
+
timestamp = interaction.get("timestamp", time.time())
|
| 834 |
+
heure = datetime.fromtimestamp(timestamp).hour
|
| 835 |
+
jour = datetime.fromtimestamp(timestamp).strftime("%A")
|
| 836 |
+
|
| 837 |
+
heures_activite[heure] += 1
|
| 838 |
+
jours_activite[jour] += 1
|
| 839 |
+
|
| 840 |
+
# Déterminer les pics d'activité
|
| 841 |
+
heure_pic = max(heures_activite.items(), key=lambda x: x[1])[0] if heures_activite else 12
|
| 842 |
+
jour_pic = max(jours_activite.items(), key=lambda x: x[1])[0] if jours_activite else "Lundi"
|
| 843 |
+
|
| 844 |
+
return {
|
| 845 |
+
"heures_actives": dict(heures_activite),
|
| 846 |
+
"jours_actifs": dict(jours_activite),
|
| 847 |
+
"pic_activite": f"{heure_pic}h",
|
| 848 |
+
"jour_favori": jour_pic
|
| 849 |
+
}
|
| 850 |
+
|
| 851 |
+
def _evaluer_niveau_technique(self) -> str:
|
| 852 |
+
"""Évalue le niveau technique de l'utilisateur"""
|
| 853 |
+
termes_techniques = 0
|
| 854 |
+
termes_simples = 0
|
| 855 |
+
|
| 856 |
+
for interaction in self.historique_interactions:
|
| 857 |
+
message = interaction.get("message", "").lower()
|
| 858 |
+
|
| 859 |
+
# Termes techniques
|
| 860 |
+
if any(mot in message for mot in ['api', 'endpoint', 'docker', 'uvicorn', 'fastapi', 'websocket']):
|
| 861 |
+
termes_techniques += 1
|
| 862 |
+
# Termes simples
|
| 863 |
+
if any(mot in message for mot in ['comment', 'quoi', 'aide', 'simple']):
|
| 864 |
+
termes_simples += 1
|
| 865 |
+
|
| 866 |
+
total_interactions = len(self.historique_interactions)
|
| 867 |
+
if total_interactions == 0:
|
| 868 |
+
return "debutant"
|
| 869 |
+
|
| 870 |
+
ratio_technique = termes_techniques / total_interactions
|
| 871 |
+
|
| 872 |
+
if ratio_technique > 0.3:
|
| 873 |
+
return "expert"
|
| 874 |
+
elif ratio_technique > 0.1:
|
| 875 |
+
return "intermediaire"
|
| 876 |
+
else:
|
| 877 |
+
return "debutant"
|
| 878 |
+
|
| 879 |
+
def _detecter_besoins_implicites(self) -> List[str]:
|
| 880 |
+
"""Détecte les besoins non exprimés directement"""
|
| 881 |
+
besoins = []
|
| 882 |
+
|
| 883 |
+
# Analyser les patterns pour détecter les besoins
|
| 884 |
+
themes = self.profil_utilisateur["themes_frequents"]
|
| 885 |
+
|
| 886 |
+
if themes.get("optimisation_technique", 0) > 5:
|
| 887 |
+
besoins.append("optimisation_avancee")
|
| 888 |
+
if themes.get("monitoring_systeme", 0) > 3:
|
| 889 |
+
besoins.append("dashboard_ameliore")
|
| 890 |
+
if themes.get("recherche_aide", 0) > 5:
|
| 891 |
+
besoins.append("assistant_intelligent")
|
| 892 |
+
if themes.get("ia_avancee", 0) > 2:
|
| 893 |
+
besoins.append("integrations_ia")
|
| 894 |
+
if themes.get("developpement", 0) > 3:
|
| 895 |
+
besoins.append("outils_developement")
|
| 896 |
+
|
| 897 |
+
return besoins
|
| 898 |
+
|
| 899 |
+
def _generer_recommandations(self, themes: Dict, patterns: Dict, niveau: str, besoins: List[str]):
|
| 900 |
+
"""Génère des recommandations personnalisées"""
|
| 901 |
+
self.recommandations_actives = []
|
| 902 |
+
|
| 903 |
+
# Recommandations basées sur les thèmes
|
| 904 |
+
for theme, count in themes.items():
|
| 905 |
+
if count > 3: # Seuil d'activation
|
| 906 |
+
if theme == "optimisation_technique":
|
| 907 |
+
self.recommandations_actives.append({
|
| 908 |
+
"type": "fonctionnalite",
|
| 909 |
+
"titre": "🔧 Mode Optimisation Avancée",
|
| 910 |
+
"description": "Activez le mode haute performance pour l'optimisation mémoire",
|
| 911 |
+
"priorite": "haute",
|
| 912 |
+
"action": "/api/optimisation/activer",
|
| 913 |
+
"score_confiance": min(1.0, count / 10)
|
| 914 |
+
})
|
| 915 |
+
elif theme == "monitoring_systeme":
|
| 916 |
+
self.recommandations_actives.append({
|
| 917 |
+
"type": "interface",
|
| 918 |
+
"titre": "📊 Dashboard Détaillé",
|
| 919 |
+
"description": "Accédez à des métriques système avancées",
|
| 920 |
+
"priorite": "moyenne",
|
| 921 |
+
"action": "/dashboard/avance",
|
| 922 |
+
"score_confiance": min(1.0, count / 8)
|
| 923 |
+
})
|
| 924 |
+
|
| 925 |
+
# Recommandations basées sur le niveau technique
|
| 926 |
+
if niveau == "debutant":
|
| 927 |
+
self.recommandations_actives.append({
|
| 928 |
+
"type": "guide",
|
| 929 |
+
"titre": "🎓 Guide de Démarrage",
|
| 930 |
+
"description": "Découvrez les fonctionnalités principales de Barouia-Cortex",
|
| 931 |
+
"priorite": "haute",
|
| 932 |
+
"action": "/guide/demarrage",
|
| 933 |
+
"score_confiance": 0.9
|
| 934 |
+
})
|
| 935 |
+
elif niveau == "expert":
|
| 936 |
+
self.recommandations_actives.append({
|
| 937 |
+
"type": "fonctionnalite",
|
| 938 |
+
"titre": "⚙️ API Avancée",
|
| 939 |
+
"description": "Accédez aux endpoints experts et à la documentation technique",
|
| 940 |
+
"priorite": "moyenne",
|
| 941 |
+
"action": "/api/docs",
|
| 942 |
+
"score_confiance": 0.8
|
| 943 |
+
})
|
| 944 |
+
|
| 945 |
+
# Recommandations basées sur les besoins détectés
|
| 946 |
+
for besoin in besoins:
|
| 947 |
+
if besoin == "integrations_ia":
|
| 948 |
+
self.recommandations_actives.append({
|
| 949 |
+
"type": "integration",
|
| 950 |
+
"titre": "🤖 Intégrations IA",
|
| 951 |
+
"description": "Connectez Barouia-Cortex à GPT-4, Claude et autres modèles",
|
| 952 |
+
"priorite": "haute",
|
| 953 |
+
"action": "/api/ia/configurer",
|
| 954 |
+
"score_confiance": 0.85
|
| 955 |
+
})
|
| 956 |
+
elif besoin == "assistant_intelligent":
|
| 957 |
+
self.recommandations_actives.append({
|
| 958 |
+
"type": "fonctionnalite",
|
| 959 |
+
"titre": "💡 Assistant Contextuel",
|
| 960 |
+
"description": "Activez l'assistant qui comprend le contexte de vos projets",
|
| 961 |
+
"priorite": "moyenne",
|
| 962 |
+
"action": "/api/assistant/activer",
|
| 963 |
+
"score_confiance": 0.75
|
| 964 |
+
})
|
| 965 |
+
|
| 966 |
+
# Calcul du score de personnalisation
|
| 967 |
+
self.score_personnalisation = min(1.0, len(self.historique_interactions) / 50)
|
| 968 |
+
|
| 969 |
+
# Trier par priorité et score de confiance
|
| 970 |
+
self.recommandations_actives.sort(
|
| 971 |
+
key=lambda x: (1 if x["priorite"] == "haute" else 0.5, x["score_confiance"]),
|
| 972 |
+
reverse=True
|
| 973 |
+
)
|
| 974 |
+
|
| 975 |
+
def suggerer_fonctionnalites(self, limite: int = 5) -> List[Dict[str, Any]]:
|
| 976 |
+
"""Suggère des fonctionnalités personnalisées"""
|
| 977 |
+
if not self.recommandations_actives:
|
| 978 |
+
self.analyser_comportement()
|
| 979 |
+
|
| 980 |
+
# Retourner les recommandations les plus pertinentes
|
| 981 |
+
return self.recommandations_actives[:limite]
|
| 982 |
+
|
| 983 |
+
def suggerer_themes(self, limite: int = 3) -> List[str]:
|
| 984 |
+
"""Suggère des thèmes de discussion basés sur les intérêts"""
|
| 985 |
+
themes_tries = sorted(
|
| 986 |
+
self.profil_utilisateur["themes_frequents"].items(),
|
| 987 |
+
key=lambda x: x[1],
|
| 988 |
+
reverse=True
|
| 989 |
+
)
|
| 990 |
+
|
| 991 |
+
themes_suggerees = []
|
| 992 |
+
correspondance_themes = {
|
| 993 |
+
"optimisation_technique": "Optimisation du système et performances",
|
| 994 |
+
"communication": "Communication et interaction naturelle",
|
| 995 |
+
"monitoring_systeme": "Surveillance et métriques avancées",
|
| 996 |
+
"ia_avancee": "Intelligence artificielle et apprentissage",
|
| 997 |
+
"developpement": "Développement et programmation"
|
| 998 |
+
}
|
| 999 |
+
|
| 1000 |
+
for theme, count in themes_tries[:limite]:
|
| 1001 |
+
if count > 2: # Seuil minimum
|
| 1002 |
+
themes_suggerees.append(correspondance_themes.get(theme, theme))
|
| 1003 |
+
|
| 1004 |
+
return themes_suggerees
|
| 1005 |
+
|
| 1006 |
+
def get_statistiques(self) -> Dict[str, Any]:
|
| 1007 |
+
"""Retourne les statistiques du système de recommandations"""
|
| 1008 |
+
return {
|
| 1009 |
+
"total_interactions": len(self.historique_interactions),
|
| 1010 |
+
"score_personnalisation": self.score_personnalisation,
|
| 1011 |
+
"recommandations_actives": len(self.recommandations_actives),
|
| 1012 |
+
"profil_utilisateur": {
|
| 1013 |
+
"niveau_technique": self.profil_utilisateur["niveau_technique"],
|
| 1014 |
+
"themes_principaux": dict(
|
| 1015 |
+
sorted(
|
| 1016 |
+
self.profil_utilisateur["themes_frequents"].items(),
|
| 1017 |
+
key=lambda x: x[1],
|
| 1018 |
+
reverse=True
|
| 1019 |
+
)[:5]
|
| 1020 |
+
)
|
| 1021 |
+
},
|
| 1022 |
+
"derniere_analyse": self.derniere_analyse
|
| 1023 |
+
}
|
| 1024 |
|
| 1025 |
# =============================================================================
|
| 1026 |
# INTÉGRATION AVEC LE SYSTÈME EXISTANT
|
| 1027 |
# =============================================================================
|
| 1028 |
|
| 1029 |
+
# Ajoutez cette ligne APRÈS la création des autres systèmes
|
| 1030 |
+
systeme_recommandations = SystemeRecommandations()
|
| 1031 |
+
|
| 1032 |
+
# Modifiez la fonction de traitement des messages pour enregistrer les interactions
|
| 1033 |
+
async def traiter_requete_avec_recommandations(self, requete: str) -> Dict[str, Any]:
|
| 1034 |
+
"""Version améliorée qui enregistre les interactions pour les recommandations"""
|
| 1035 |
+
resultat = await self.traiter_requete_optimisee(requete)
|
| 1036 |
+
|
| 1037 |
+
# Enregistrer l'interaction pour les recommandations
|
| 1038 |
+
interaction = {
|
| 1039 |
+
"timestamp": time.time(),
|
| 1040 |
+
"message": requete,
|
| 1041 |
+
"type": "chat",
|
| 1042 |
+
"longueur": len(requete),
|
| 1043 |
+
"themes_detectes": [] # Serait rempli par analyse NLP
|
| 1044 |
+
}
|
| 1045 |
+
|
| 1046 |
+
systeme_recommandations.enregistrer_interaction(interaction)
|
| 1047 |
+
|
| 1048 |
+
return resultat
|
| 1049 |
+
|
| 1050 |
+
# Remplacez la méthode originale
|
| 1051 |
+
CortexOptimise.traiter_requete_optimisee = traiter_requete_avec_recommandations
|
| 1052 |
+
|
| 1053 |
+
# =============================================================================
|
| 1054 |
+
# ROUTES API POUR LES RECOMMANDATIONS
|
| 1055 |
+
# =============================================================================
|
| 1056 |
+
|
| 1057 |
+
@app.get("/api/recommandations/obtenir")
|
| 1058 |
+
async def obtenir_recommandations(limite: int = 5):
|
| 1059 |
+
"""Endpoint pour obtenir les recommandations personnalisées"""
|
| 1060 |
+
recommandations = systeme_recommandations.suggerer_fonctionnalites(limite)
|
| 1061 |
+
|
| 1062 |
+
return {
|
| 1063 |
+
"success": True,
|
| 1064 |
+
"recommandations": recommandations,
|
| 1065 |
+
"statistiques": systeme_recommandations.get_statistiques(),
|
| 1066 |
+
"themes_suggerees": systeme_recommandations.suggerer_themes(3)
|
| 1067 |
+
}
|
| 1068 |
+
|
| 1069 |
+
@app.post("/api/recommandations/analyser")
|
| 1070 |
+
async def analyser_comportement():
|
| 1071 |
+
"""Déclenche une analyse manuelle du comportement"""
|
| 1072 |
+
analyse = systeme_recommandations.analyser_comportement()
|
| 1073 |
+
|
| 1074 |
+
return {
|
| 1075 |
+
"success": True,
|
| 1076 |
+
"analyse": analyse,
|
| 1077 |
+
"message": "Analyse du comportement terminée"
|
| 1078 |
+
}
|
| 1079 |
+
|
| 1080 |
+
@app.get("/api/recommandations/statistiques")
|
| 1081 |
+
async def get_recommandations_statistiques():
|
| 1082 |
+
"""Statistiques détaillées du système de recommandations"""
|
| 1083 |
+
return {
|
| 1084 |
+
"success": True,
|
| 1085 |
+
"statistiques": systeme_recommandations.get_statistiques()
|
| 1086 |
+
}
|
| 1087 |
+
|
| 1088 |
+
@app.post("/api/recommandations/feedback")
|
| 1089 |
+
async def donner_feedback_recommandation(request: dict):
|
| 1090 |
+
"""Donne un feedback sur une recommandation (utile pour l'apprentissage)"""
|
| 1091 |
+
recommandation_id = request.get("recommandation_id")
|
| 1092 |
+
note = request.get("note") # 1-5
|
| 1093 |
+
utile = request.get("utile", False)
|
| 1094 |
+
|
| 1095 |
+
# Ici on pourrait enregistrer le feedback pour améliorer le système
|
| 1096 |
+
logger.info(f"📝 Feedback reçu: {recommandation_id} - Note: {note} - Utile: {utile}")
|
| 1097 |
+
|
| 1098 |
+
return {
|
| 1099 |
+
"success": True,
|
| 1100 |
+
"message": "Feedback enregistré pour amélioration du système"
|
| 1101 |
+
}
|
| 1102 |
+
|
| 1103 |
+
# =============================================================================
|
| 1104 |
+
# INTÉGRATION DANS L'INTERFACE
|
| 1105 |
+
# =============================================================================
|
| 1106 |
+
|
| 1107 |
+
# Ajoutez cette route pour une interface dédiée aux recommandations
|
| 1108 |
+
@app.get("/recommandations", response_class=HTMLResponse)
|
| 1109 |
+
async def interface_recommandations():
|
| 1110 |
+
"""Interface dédiée aux recommandations personnalisées"""
|
| 1111 |
+
return """
|
| 1112 |
+
<!DOCTYPE html>
|
| 1113 |
+
<html>
|
| 1114 |
+
<head>
|
| 1115 |
+
<title>Recommandations Personnalisées - Barouia-Cortex</title>
|
| 1116 |
+
<style>
|
| 1117 |
+
body {
|
| 1118 |
+
font-family: Arial, sans-serif;
|
| 1119 |
+
margin: 40px;
|
| 1120 |
+
background: #0f0f23;
|
| 1121 |
+
color: white;
|
| 1122 |
+
}
|
| 1123 |
+
.container { max-width: 800px; margin: 0 auto; }
|
| 1124 |
+
.header { text-align: center; margin-bottom: 2rem; }
|
| 1125 |
+
.recommandation {
|
| 1126 |
+
background: rgba(255,255,255,0.1);
|
| 1127 |
+
padding: 1rem;
|
| 1128 |
+
margin: 1rem 0;
|
| 1129 |
+
border-radius: 10px;
|
| 1130 |
+
border-left: 4px solid #667eea;
|
| 1131 |
+
}
|
| 1132 |
+
.priorite-haute { border-left-color: #ff6b6b; }
|
| 1133 |
+
.priorite-moyenne { border-left-color: #ffd93d; }
|
| 1134 |
+
</style>
|
| 1135 |
+
</head>
|
| 1136 |
+
<body>
|
| 1137 |
+
<div class="container">
|
| 1138 |
+
<div class="header">
|
| 1139 |
+
<h1>🎯 Recommandations Personnalisées</h1>
|
| 1140 |
+
<p>Découvrez les fonctionnalités adaptées à votre usage</p>
|
| 1141 |
+
</div>
|
| 1142 |
+
<div id="recommandations-container">
|
| 1143 |
+
<p>Chargement des recommandations...</p>
|
| 1144 |
+
</div>
|
| 1145 |
+
</div>
|
| 1146 |
+
|
| 1147 |
+
<script>
|
| 1148 |
+
async function chargerRecommandations() {
|
| 1149 |
+
try {
|
| 1150 |
+
const response = await fetch('/api/recommandations/obtenir?limite=10');
|
| 1151 |
+
const data = await response.json();
|
| 1152 |
+
|
| 1153 |
+
const container = document.getElementById('recommandations-container');
|
| 1154 |
+
container.innerHTML = '';
|
| 1155 |
+
|
| 1156 |
+
if (data.recommandations.length === 0) {
|
| 1157 |
+
container.innerHTML = '<p>Aucune recommandation pour le moment. Continuez à utiliser le système !</p>';
|
| 1158 |
+
return;
|
| 1159 |
+
}
|
| 1160 |
+
|
| 1161 |
+
data.recommandations.forEach(rec => {
|
| 1162 |
+
const div = document.createElement('div');
|
| 1163 |
+
div.className = `recommandation priorite-${rec.priorite}`;
|
| 1164 |
+
div.innerHTML = `
|
| 1165 |
+
<h3>${rec.titre}</h3>
|
| 1166 |
+
<p>${rec.description}</p>
|
| 1167 |
+
<small>Confiance: ${(rec.score_confiance * 100).toFixed(0)}%</small>
|
| 1168 |
+
`;
|
| 1169 |
+
container.appendChild(div);
|
| 1170 |
+
});
|
| 1171 |
+
|
| 1172 |
+
} catch (error) {
|
| 1173 |
+
console.error('Erreur:', error);
|
| 1174 |
+
}
|
| 1175 |
+
}
|
| 1176 |
+
|
| 1177 |
+
document.addEventListener('DOMContentLoaded', chargerRecommandations);
|
| 1178 |
+
</script>
|
| 1179 |
+
</body>
|
| 1180 |
+
</html>
|
| 1181 |
+
"""
|
| 1182 |
+
# =============================================================================
|
| 1183 |
+
# INTÉGRATION AVEC LE SYSTÈME EXISTANT
|
| 1184 |
+
# =============================================================================
|
| 1185 |
+
|
| 1186 |
# Ajoutez cette ligne APRÈS la création de cortex_optimise (vers la ligne ~400)
|
| 1187 |
integration_apis = IntegrationAPIs()
|
| 1188 |
|