LinkedinMonitor / app.py
GuglielmoTor's picture
Update app.py
998bc4b verified
raw
history blame
45.2 kB
import gradio as gr
import pandas as pd
import os
import logging
import matplotlib
matplotlib.use('Agg') # Set backend for Matplotlib to avoid GUI conflicts with Gradio
import matplotlib.pyplot as plt
import time # For profiling if needed
# --- Module Imports ---
from gradio_utils import get_url_user_token
# Functions from newly created/refactored modules
from config import (
LINKEDIN_CLIENT_ID_ENV_VAR, BUBBLE_APP_NAME_ENV_VAR,
BUBBLE_API_KEY_PRIVATE_ENV_VAR, BUBBLE_API_ENDPOINT_ENV_VAR)
from state_manager import process_and_store_bubble_token
from sync_logic import sync_all_linkedin_data_orchestrator
from ui_generators import (
display_main_dashboard,
run_mentions_tab_display,
run_follower_stats_tab_display,
build_analytics_tab_plot_area,
BOMB_ICON, EXPLORE_ICON, FORMULA_ICON, ACTIVE_ICON
)
from analytics_data_processing import prepare_filtered_analytics_data
from analytics_plot_generator import (
generate_posts_activity_plot,
generate_mentions_activity_plot, generate_mention_sentiment_plot,
generate_followers_count_over_time_plot,
generate_followers_growth_rate_plot,
generate_followers_by_demographics_plot,
generate_engagement_rate_over_time_plot,
generate_reach_over_time_plot,
generate_impressions_over_time_plot,
create_placeholder_plot,
generate_likes_over_time_plot,
generate_clicks_over_time_plot,
generate_shares_over_time_plot,
generate_comments_over_time_plot,
generate_comments_sentiment_breakdown_plot,
generate_post_frequency_plot,
generate_content_format_breakdown_plot,
generate_content_topic_breakdown_plot
)
from formulas import PLOT_FORMULAS
# --- NEW CHATBOT MODULE IMPORTS ---
from chatbot_prompts import get_initial_insight_and_suggestions
from chatbot_handler import generate_llm_response
# --- END NEW CHATBOT MODULE IMPORTS ---
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')
# Mapping from plot_configs IDs to PLOT_FORMULAS keys
PLOT_ID_TO_FORMULA_KEY_MAP = {
"posts_activity": "posts_activity",
"mentions_activity": "mentions_activity",
"mention_sentiment": "mention_sentiment",
"followers_count": "followers_count_over_time",
"followers_growth_rate": "followers_growth_rate",
"followers_by_location": "followers_by_demographics",
"followers_by_role": "followers_by_demographics",
"followers_by_industry": "followers_by_demographics",
"followers_by_seniority": "followers_by_demographics",
"engagement_rate": "engagement_rate_over_time",
"reach_over_time": "reach_over_time",
"impressions_over_time": "impressions_over_time",
"likes_over_time": "likes_over_time",
"clicks_over_time": "clicks_over_time",
"shares_over_time": "shares_over_time",
"comments_over_time": "comments_over_time",
"comments_sentiment": "comments_sentiment_breakdown",
"post_frequency_cs": "post_frequency",
"content_format_breakdown_cs": "content_format_breakdown",
"content_topic_breakdown_cs": "content_topic_breakdown",
"mention_analysis_volume": "mentions_activity",
"mention_analysis_sentiment": "mention_sentiment"
}
# --- Analytics Tab: Plot Figure Generation Function ---
def update_analytics_plots_figures(token_state_value, date_filter_option, custom_start_date, custom_end_date):
logging.info(f"Updating analytics plot figures. Filter: {date_filter_option}, Custom Start: {custom_start_date}, Custom End: {custom_end_date}")
num_expected_plots = 19
if not token_state_value or not token_state_value.get("token"):
message = "❌ Accesso negato. Nessun token. Impossibile generare le analisi."
logging.warning(message)
placeholder_figs = [create_placeholder_plot(title="Accesso Negato", message="Nessun token.") for _ in range(num_expected_plots)]
return [message] + placeholder_figs
try:
(filtered_merged_posts_df,
filtered_mentions_df,
date_filtered_follower_stats_df,
raw_follower_stats_df,
start_dt_for_msg, end_dt_for_msg) = \
prepare_filtered_analytics_data(
token_state_value, date_filter_option, custom_start_date, custom_end_date
)
except Exception as e:
error_msg = f"❌ Errore durante la preparazione dei dati per le analisi: {e}"
logging.error(error_msg, exc_info=True)
placeholder_figs = [create_placeholder_plot(title="Errore Preparazione Dati", message=str(e)) for _ in range(num_expected_plots)]
return [error_msg] + placeholder_figs
date_column_posts = token_state_value.get("config_date_col_posts", "published_at")
date_column_mentions = token_state_value.get("config_date_col_mentions", "date")
media_type_col_name = token_state_value.get("config_media_type_col", "media_type")
eb_labels_col_name = token_state_value.get("config_eb_labels_col", "li_eb_label")
plot_figs = []
try:
# Define plot functions and their arguments
# Order matters and must match plot_configs
plot_definitions = [
{"func": generate_followers_count_over_time_plot, "args": [date_filtered_follower_stats_df, 'follower_gains_monthly'], "is_demographic": False},
{"func": generate_followers_growth_rate_plot, "args": [date_filtered_follower_stats_df, 'follower_gains_monthly'], "is_demographic": False},
{"func": generate_followers_by_demographics_plot, "args": [raw_follower_stats_df, 'follower_geo', "Follower per Località"], "type_value_key": "follower_geo", "is_demographic": True},
{"func": generate_followers_by_demographics_plot, "args": [raw_follower_stats_df, 'follower_function', "Follower per Ruolo"], "type_value_key": "follower_function", "is_demographic": True},
{"func": generate_followers_by_demographics_plot, "args": [raw_follower_stats_df, 'follower_industry', "Follower per Settore"], "type_value_key": "follower_industry", "is_demographic": True},
{"func": generate_followers_by_demographics_plot, "args": [raw_follower_stats_df, 'follower_seniority', "Follower per Anzianità"], "type_value_key": "follower_seniority", "is_demographic": True},
{"func": generate_engagement_rate_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_reach_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_impressions_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_likes_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_clicks_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_shares_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_comments_over_time_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_comments_sentiment_breakdown_plot, "args": [filtered_merged_posts_df, 'comment_sentiment'], "is_demographic": False},
{"func": generate_post_frequency_plot, "args": [filtered_merged_posts_df, date_column_posts], "is_demographic": False},
{"func": generate_content_format_breakdown_plot, "args": [filtered_merged_posts_df, media_type_col_name], "is_demographic": False},
{"func": generate_content_topic_breakdown_plot, "args": [filtered_merged_posts_df, eb_labels_col_name], "is_demographic": False},
{"func": generate_mentions_activity_plot, "args": [filtered_mentions_df, date_column_mentions], "is_demographic": False},
{"func": generate_mention_sentiment_plot, "args": [filtered_mentions_df], "is_demographic": False}
]
for i, plot_def in enumerate(plot_definitions):
plot_fn = plot_def["func"]
args = plot_def["args"]
plot_title_for_error = args[2] if plot_def["is_demographic"] else plot_fn.__name__
try:
# Specific check for demographic plots if raw_follower_stats_df is empty or missing key columns
if plot_def["is_demographic"]:
df_arg = args[0] # raw_follower_stats_df
type_val_col = plot_def["type_value_key"]
if df_arg is None or df_arg.empty:
logging.warning(f"raw_follower_stats_df is empty. Cannot generate demographic plot: {plot_title_for_error}")
raise ValueError(f"Dati demografici mancanti (raw_follower_stats_df vuoto).")
if type_val_col not in df_arg.columns:
logging.warning(f"Colonna '{type_val_col}' mancante in raw_follower_stats_df per il grafico '{plot_title_for_error}'. Colonne disponibili: {df_arg.columns.tolist()}")
raise KeyError(f"Colonna dati '{type_val_col}' non trovata.")
fig = plot_fn(*args)
plot_figs.append(fig)
except (KeyError, ValueError) as plot_e: # Catch KeyError for missing columns, ValueError for other data issues
logging.error(f"Errore generazione grafico '{plot_title_for_error}' (slot {i}): {plot_e}", exc_info=False) # Set exc_info to False for cleaner logs for known data issues
plot_figs.append(create_placeholder_plot(title=f"Errore Dati: {plot_title_for_error}", message=f"Impossibile generare: {str(plot_e)}"))
except Exception as plot_e: # Catch other unexpected errors
logging.error(f"Errore imprevisto generazione grafico '{plot_title_for_error}' (slot {i}): {plot_e}", exc_info=True)
plot_figs.append(create_placeholder_plot(title=f"Errore Grafico: {plot_title_for_error}", message=f"Dettaglio: {str(plot_e)[:100]}"))
message = f"📊 Analisi aggiornate per il periodo: {date_filter_option}"
if date_filter_option == "Intervallo Personalizzato":
s_display = start_dt_for_msg.strftime('%Y-%m-%d') if start_dt_for_msg else "Qualsiasi"
e_display = end_dt_for_msg.strftime('%Y-%m-%d') if end_dt_for_msg else "Qualsiasi"
message += f" (Da: {s_display} A: {e_display})"
final_plot_figs = []
for i, p_fig in enumerate(plot_figs):
if p_fig is not None and not isinstance(p_fig, str):
final_plot_figs.append(p_fig)
else:
logging.warning(f"Plot generation failed or unexpected type for slot {i}, using placeholder. Figure: {p_fig}")
final_plot_figs.append(create_placeholder_plot(title="Errore Grafico", message="Impossibile generare questa figura."))
while len(final_plot_figs) < num_expected_plots:
logging.warning(f"Adding missing plot placeholder. Expected {num_expected_plots}, got {len(final_plot_figs)}.")
final_plot_figs.append(create_placeholder_plot(title="Grafico Mancante", message="Figura non generata."))
return [message] + final_plot_figs[:num_expected_plots]
except Exception as e:
error_msg = f"❌ Errore durante la generazione delle figure dei grafici analitici: {e}"
logging.error(error_msg, exc_info=True)
placeholder_figs = [create_placeholder_plot(title="Errore Generazione Grafici", message=str(e)) for _ in range(num_expected_plots)]
return [error_msg] + placeholder_figs
# --- Gradio UI Blocks ---
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
title="LinkedIn Organization Dashboard") as app:
token_state = gr.State(value={
"token": None, "client_id": None, "org_urn": None,
"bubble_posts_df": pd.DataFrame(), "bubble_post_stats_df": pd.DataFrame(),
"bubble_mentions_df": pd.DataFrame(), "bubble_follower_stats_df": pd.DataFrame(),
"fetch_count_for_api": 0, "url_user_token_temp_storage": None,
"config_date_col_posts": "published_at", "config_date_col_mentions": "date",
"config_date_col_followers": "date", "config_media_type_col": "media_type",
"config_eb_labels_col": "li_eb_label"
})
chat_histories_st = gr.State({})
current_chat_plot_id_st = gr.State(None)
gr.Markdown("# 🚀 LinkedIn Organization Dashboard")
url_user_token_display = gr.Textbox(label="User Token (Nascosto)", interactive=False, visible=False)
status_box = gr.Textbox(label="Stato Generale Token LinkedIn", interactive=False, value="Inizializzazione...")
org_urn_display = gr.Textbox(label="URN Organizzazione (Nascosto)", interactive=False, visible=False)
app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn_display], api_name="get_url_params", show_progress=False)
def initial_load_sequence(url_token, org_urn_val, current_state):
status_msg, new_state, btn_update = process_and_store_bubble_token(url_token, org_urn_val, current_state)
dashboard_content = display_main_dashboard(new_state)
return status_msg, new_state, btn_update, dashboard_content
with gr.Tabs() as tabs:
with gr.TabItem("1️⃣ Dashboard & Sync", id="tab_dashboard_sync"):
gr.Markdown("Il sistema controlla i dati esistenti da Bubble. 'Sincronizza' si attiva se sono necessari nuovi dati.")
sync_data_btn = gr.Button("🔄 Sincronizza Dati LinkedIn", variant="primary", visible=False, interactive=False)
sync_status_html_output = gr.HTML("<p style='text-align:center;'>Stato sincronizzazione...</p>")
dashboard_display_html = gr.HTML("<p style='text-align:center;'>Caricamento dashboard...</p>")
org_urn_display.change(
fn=initial_load_sequence,
inputs=[url_user_token_display, org_urn_display, token_state],
outputs=[status_box, token_state, sync_data_btn, dashboard_display_html],
show_progress="full"
)
with gr.TabItem("2️⃣ Analisi", id="tab_analytics"):
gr.Markdown("## 📈 Analisi Performance LinkedIn")
gr.Markdown("Seleziona un intervallo di date. Clicca i pulsanti (💣 Insights, ƒ Formula, 🧭 Esplora) su un grafico per azioni.")
analytics_status_md = gr.Markdown("Stato analisi...")
with gr.Row():
date_filter_selector = gr.Radio(
["Sempre", "Ultimi 7 Giorni", "Ultimi 30 Giorni", "Intervallo Personalizzato"],
label="Seleziona Intervallo Date", value="Sempre", scale=3
)
with gr.Column(scale=2):
custom_start_date_picker = gr.DateTime(label="Data Inizio", visible=False, include_time=False, type="datetime")
custom_end_date_picker = gr.DateTime(label="Data Fine", visible=False, include_time=False, type="datetime")
apply_filter_btn = gr.Button("🔍 Applica Filtro & Aggiorna Analisi", variant="primary")
def toggle_custom_date_pickers(selection):
is_custom = selection == "Intervallo Personalizzato"
return gr.update(visible=is_custom), gr.update(visible=is_custom)
date_filter_selector.change(
fn=toggle_custom_date_pickers,
inputs=[date_filter_selector],
outputs=[custom_start_date_picker, custom_end_date_picker]
)
plot_configs = [
{"label": "Numero di Follower nel Tempo", "id": "followers_count", "section": "Dinamiche dei Follower"},
{"label": "Tasso di Crescita Follower", "id": "followers_growth_rate", "section": "Dinamiche dei Follower"},
{"label": "Follower per Località", "id": "followers_by_location", "section": "Demografia Follower"},
{"label": "Follower per Ruolo (Funzione)", "id": "followers_by_role", "section": "Demografia Follower"},
{"label": "Follower per Settore", "id": "followers_by_industry", "section": "Demografia Follower"},
{"label": "Follower per Anzianità", "id": "followers_by_seniority", "section": "Demografia Follower"},
{"label": "Tasso di Engagement nel Tempo", "id": "engagement_rate", "section": "Approfondimenti Performance Post"},
{"label": "Copertura nel Tempo", "id": "reach_over_time", "section": "Approfondimenti Performance Post"},
{"label": "Visualizzazioni nel Tempo", "id": "impressions_over_time", "section": "Approfondimenti Performance Post"},
{"label": "Reazioni (Like) nel Tempo", "id": "likes_over_time", "section": "Approfondimenti Performance Post"},
{"label": "Click nel Tempo", "id": "clicks_over_time", "section": "Engagement Dettagliato Post nel Tempo"},
{"label": "Condivisioni nel Tempo", "id": "shares_over_time", "section": "Engagement Dettagliato Post nel Tempo"},
{"label": "Commenti nel Tempo", "id": "comments_over_time", "section": "Engagement Dettagliato Post nel Tempo"},
{"label": "Ripartizione Commenti per Sentiment", "id": "comments_sentiment", "section": "Engagement Dettagliato Post nel Tempo"},
{"label": "Frequenza Post", "id": "post_frequency_cs", "section": "Analisi Strategia Contenuti"},
{"label": "Ripartizione Contenuti per Formato", "id": "content_format_breakdown_cs", "section": "Analisi Strategia Contenuti"},
{"label": "Ripartizione Contenuti per Argomenti", "id": "content_topic_breakdown_cs", "section": "Analisi Strategia Contenuti"},
{"label": "Volume Menzioni nel Tempo (Dettaglio)", "id": "mention_analysis_volume", "section": "Analisi Menzioni (Dettaglio)"},
{"label": "Ripartizione Menzioni per Sentiment (Dettaglio)", "id": "mention_analysis_sentiment", "section": "Analisi Menzioni (Dettaglio)"}
]
assert len(plot_configs) == 19, "Mancata corrispondenza in plot_configs e grafici attesi."
active_panel_action_state = gr.State(None)
explored_plot_id_state = gr.State(None)
plot_ui_objects = {}
with gr.Row(equal_height=False):
with gr.Column(scale=8) as plots_area_col:
plot_ui_objects = build_analytics_tab_plot_area(plot_configs)
with gr.Column(scale=4, visible=False) as global_actions_column_ui:
gr.Markdown("### 💡 Azioni Contestuali Grafico")
insights_chatbot_ui = gr.Chatbot(
label="Chat Insights", type="messages", height=450,
bubble_full_width=False, visible=False, show_label=False,
placeholder="L'analisi AI del grafico apparirà qui. Fai domande di approfondimento!"
)
insights_chat_input_ui = gr.Textbox(
label="La tua domanda:", placeholder="Chiedi all'AI riguardo a questo grafico...",
lines=2, visible=False, show_label=False
)
with gr.Row(visible=False) as insights_suggestions_row_ui:
insights_suggestion_1_btn = gr.Button(value="Suggerimento 1", size="sm", min_width=50)
insights_suggestion_2_btn = gr.Button(value="Suggerimento 2", size="sm", min_width=50)
insights_suggestion_3_btn = gr.Button(value="Suggerimento 3", size="sm", min_width=50)
formula_display_markdown_ui = gr.Markdown(
"I dettagli sulla formula/metodologia appariranno qui.", visible=False
)
async def handle_panel_action(
plot_id_clicked: str,
action_type: str,
current_active_action_from_state: dict,
current_chat_histories: dict,
current_chat_plot_id: str
):
logging.info(f"Azione '{action_type}' per grafico: {plot_id_clicked}. Attualmente attivo: {current_active_action_from_state}")
clicked_plot_config = next((p for p in plot_configs if p["id"] == plot_id_clicked), None)
if not clicked_plot_config:
logging.error(f"Configurazione non trovata per plot_id {plot_id_clicked}")
num_button_updates = 2 * len(plot_configs)
error_updates = [gr.update(visible=False)] * 10
error_updates.extend([current_active_action_from_state, current_chat_plot_id, current_chat_histories])
error_updates.extend([gr.update()] * num_button_updates)
return error_updates
clicked_plot_label = clicked_plot_config["label"]
hypothetical_new_active_state = {"plot_id": plot_id_clicked, "type": action_type}
is_toggling_off = current_active_action_from_state == hypothetical_new_active_state
new_active_action_state_to_set = None
action_col_visible_update = gr.update(visible=True)
insights_chatbot_visible_update = gr.update(visible=False)
insights_chat_input_visible_update = gr.update(visible=False)
insights_suggestions_row_visible_update = gr.update(visible=False)
formula_display_visible_update = gr.update(visible=False)
chatbot_content_update = gr.update()
suggestion_1_update = gr.update()
suggestion_2_update = gr.update()
suggestion_3_update = gr.update()
new_current_chat_plot_id = current_chat_plot_id
updated_chat_histories = current_chat_histories
formula_content_update = gr.update()
if is_toggling_off:
new_active_action_state_to_set = None
action_col_visible_update = gr.update(visible=False)
new_current_chat_plot_id = None
logging.info(f"Chiusura pannello {action_type} per {plot_id_clicked}")
else:
new_active_action_state_to_set = hypothetical_new_active_state
if action_type == "insights":
insights_chatbot_visible_update = gr.update(visible=True)
insights_chat_input_visible_update = gr.update(visible=True)
insights_suggestions_row_visible_update = gr.update(visible=True)
new_current_chat_plot_id = plot_id_clicked
chat_history_for_this_plot = current_chat_histories.get(plot_id_clicked, [])
if not chat_history_for_this_plot:
initial_insight_msg, suggestions = get_initial_insight_and_suggestions(plot_id_clicked, clicked_plot_label)
chat_history_for_this_plot = [initial_insight_msg]
updated_chat_histories = current_chat_histories.copy()
updated_chat_histories[plot_id_clicked] = chat_history_for_this_plot
else:
_, suggestions = get_initial_insight_and_suggestions(plot_id_clicked, clicked_plot_label)
chatbot_content_update = gr.update(value=chat_history_for_this_plot)
suggestion_1_update = gr.update(value=suggestions[0])
suggestion_2_update = gr.update(value=suggestions[1])
suggestion_3_update = gr.update(value=suggestions[2])
logging.info(f"Apertura pannello CHAT per {plot_id_clicked} ('{clicked_plot_label}')")
elif action_type == "formula":
formula_display_visible_update = gr.update(visible=True)
formula_key = PLOT_ID_TO_FORMULA_KEY_MAP.get(plot_id_clicked)
formula_text = f"**Formula/Metodologia per: {clicked_plot_label}**\n\nID Grafico: `{plot_id_clicked}`.\n\n"
if formula_key and formula_key in PLOT_FORMULAS:
formula_data = PLOT_FORMULAS[formula_key]
formula_text += f"### {formula_data['title']}\n\n"
formula_text += f"**Descrizione:**\n{formula_data['description']}\n\n"
formula_text += "**Come viene calcolato:**\n"
for step in formula_data['calculation_steps']:
formula_text += f"- {step}\n"
else:
formula_text += "(Nessuna informazione dettagliata sulla formula trovata per questo ID grafico in `formulas.py`)"
formula_content_update = gr.update(value=formula_text)
new_current_chat_plot_id = None
logging.info(f"Apertura pannello FORMULA per {plot_id_clicked} (mappato a {formula_key})")
all_button_icon_updates = []
for cfg_item in plot_configs:
p_id_iter = cfg_item["id"]
if new_active_action_state_to_set == {"plot_id": p_id_iter, "type": "insights"}:
all_button_icon_updates.append(gr.update(value=ACTIVE_ICON))
else:
all_button_icon_updates.append(gr.update(value=BOMB_ICON))
if new_active_action_state_to_set == {"plot_id": p_id_iter, "type": "formula"}:
all_button_icon_updates.append(gr.update(value=ACTIVE_ICON))
else:
all_button_icon_updates.append(gr.update(value=FORMULA_ICON))
final_updates = [
action_col_visible_update,
insights_chatbot_visible_update, chatbot_content_update,
insights_chat_input_visible_update,
insights_suggestions_row_visible_update, suggestion_1_update, suggestion_2_update, suggestion_3_update,
formula_display_visible_update, formula_content_update,
new_active_action_state_to_set,
new_current_chat_plot_id,
updated_chat_histories
] + all_button_icon_updates
return final_updates
async def handle_chat_message_submission(
user_message: str,
current_plot_id: str,
chat_histories: dict,
):
if not current_plot_id or not user_message.strip():
history_for_plot = chat_histories.get(current_plot_id, [])
yield history_for_plot, "", chat_histories
return
plot_config = next((p for p in plot_configs if p["id"] == current_plot_id), None)
plot_label = plot_config["label"] if plot_config else "Grafico Selezionato"
history_for_plot = chat_histories.get(current_plot_id, []).copy()
history_for_plot.append({"role": "user", "content": user_message})
yield history_for_plot, "", chat_histories
bot_response_text = await generate_llm_response(user_message, current_plot_id, plot_label, history_for_plot)
history_for_plot.append({"role": "assistant", "content": bot_response_text})
updated_chat_histories = chat_histories.copy()
updated_chat_histories[current_plot_id] = history_for_plot
yield history_for_plot, "", updated_chat_histories
async def handle_suggested_question_click(
suggestion_text: str,
current_plot_id: str,
chat_histories: dict,
):
if not current_plot_id or not suggestion_text.strip():
history_for_plot = chat_histories.get(current_plot_id, [])
yield history_for_plot, "", chat_histories
return
plot_config = next((p for p in plot_configs if p["id"] == current_plot_id), None)
plot_label = plot_config["label"] if plot_config else "Grafico Selezionato"
history_for_plot = chat_histories.get(current_plot_id, []).copy()
history_for_plot.append({"role": "user", "content": suggestion_text})
yield history_for_plot, "", chat_histories
bot_response_text = await generate_llm_response(suggestion_text, current_plot_id, plot_label, history_for_plot)
history_for_plot.append({"role": "assistant", "content": bot_response_text})
updated_chat_histories = chat_histories.copy()
updated_chat_histories[current_plot_id] = history_for_plot
yield history_for_plot, "", updated_chat_histories
def handle_explore_click(plot_id_clicked, current_explored_plot_id_from_state):
logging.info(f"Click su Esplora per: {plot_id_clicked}. Attualmente esplorato da stato: {current_explored_plot_id_from_state}")
if not plot_ui_objects:
logging.error("plot_ui_objects non popolato durante handle_explore_click.")
updates_for_missing_ui = [current_explored_plot_id_from_state]
for _ in plot_configs:
updates_for_missing_ui.extend([gr.update(), gr.update()])
return updates_for_missing_ui
new_explored_id_to_set = None
is_toggling_off = (plot_id_clicked == current_explored_plot_id_from_state)
if is_toggling_off:
new_explored_id_to_set = None
logging.info(f"Interruzione esplorazione grafico: {plot_id_clicked}")
else:
new_explored_id_to_set = plot_id_clicked
logging.info(f"Esplorazione grafico: {plot_id_clicked}")
panel_and_button_updates = []
for cfg in plot_configs:
p_id = cfg["id"]
if p_id in plot_ui_objects:
panel_visible = not new_explored_id_to_set or (p_id == new_explored_id_to_set)
panel_and_button_updates.append(gr.update(visible=panel_visible))
if p_id == new_explored_id_to_set:
panel_and_button_updates.append(gr.update(value=ACTIVE_ICON))
else:
panel_and_button_updates.append(gr.update(value=EXPLORE_ICON))
else:
panel_and_button_updates.extend([gr.update(), gr.update()])
final_updates = [new_explored_id_to_set] + panel_and_button_updates
return final_updates
action_panel_outputs_list = [
global_actions_column_ui,
insights_chatbot_ui, insights_chatbot_ui,
insights_chat_input_ui,
insights_suggestions_row_ui, insights_suggestion_1_btn, insights_suggestion_2_btn, insights_suggestion_3_btn,
formula_display_markdown_ui, formula_display_markdown_ui,
active_panel_action_state,
current_chat_plot_id_st,
chat_histories_st
]
for cfg_item_action in plot_configs:
pid_action = cfg_item_action["id"]
if pid_action in plot_ui_objects:
action_panel_outputs_list.append(plot_ui_objects[pid_action]["bomb_button"])
action_panel_outputs_list.append(plot_ui_objects[pid_action]["formula_button"])
else:
action_panel_outputs_list.extend([None, None])
explore_buttons_outputs_list = [explored_plot_id_state]
for cfg_item_explore in plot_configs:
pid_explore = cfg_item_explore["id"]
if pid_explore in plot_ui_objects:
explore_buttons_outputs_list.append(plot_ui_objects[pid_explore]["panel_component"])
explore_buttons_outputs_list.append(plot_ui_objects[pid_explore]["explore_button"])
else:
explore_buttons_outputs_list.extend([None, None])
action_click_inputs = [
active_panel_action_state,
chat_histories_st,
current_chat_plot_id_st
]
explore_click_inputs = [explored_plot_id_state]
# --- Define async wrapper functions for click handlers ---
async def insights_click_wrapper(current_active_val, current_chats_val, current_chat_pid, p_id):
return await handle_panel_action(p_id, "insights", current_active_val, current_chats_val, current_chat_pid)
async def formula_click_wrapper(current_active_val, current_chats_val, current_chat_pid, p_id):
return await handle_panel_action(p_id, "formula", current_active_val, current_chats_val, current_chat_pid)
# --- End async wrapper functions ---
for config_item in plot_configs:
plot_id = config_item["id"]
# plot_label = config_item["label"] # Not needed here anymore
if plot_id in plot_ui_objects:
ui_obj = plot_ui_objects[plot_id]
# Use a standard lambda to call the async wrapper, capturing p_id
ui_obj["bomb_button"].click(
fn=lambda cav, ccv, ccpid, p=plot_id: insights_click_wrapper(cav, ccv, ccpid, p),
inputs=action_click_inputs,
outputs=action_panel_outputs_list,
api_name=f"action_insights_{plot_id}"
)
ui_obj["formula_button"].click(
fn=lambda cav, ccv, ccpid, p=plot_id: formula_click_wrapper(cav, ccv, ccpid, p),
inputs=action_click_inputs,
outputs=action_panel_outputs_list,
api_name=f"action_formula_{plot_id}"
)
ui_obj["explore_button"].click(
fn=lambda current_explored_val, p_id=plot_id: handle_explore_click(p_id, current_explored_val),
inputs=explore_click_inputs,
outputs=explore_buttons_outputs_list,
api_name=f"action_explore_{plot_id}"
)
else:
logging.warning(f"Oggetto UI per plot_id '{plot_id}' non trovato durante il tentativo di associare i gestori di click.")
chat_submission_outputs = [insights_chatbot_ui, insights_chat_input_ui, chat_histories_st]
insights_chat_input_ui.submit(
fn=handle_chat_message_submission,
inputs=[insights_chat_input_ui, current_chat_plot_id_st, chat_histories_st],
outputs=chat_submission_outputs,
api_name="submit_chat_message"
)
insights_suggestion_1_btn.click(
fn=handle_suggested_question_click,
inputs=[insights_suggestion_1_btn, current_chat_plot_id_st, chat_histories_st],
outputs=chat_submission_outputs,
api_name="click_suggestion_1"
)
insights_suggestion_2_btn.click(
fn=handle_suggested_question_click,
inputs=[insights_suggestion_2_btn, current_chat_plot_id_st, chat_histories_st],
outputs=chat_submission_outputs,
api_name="click_suggestion_2"
)
insights_suggestion_3_btn.click(
fn=handle_suggested_question_click,
inputs=[insights_suggestion_3_btn, current_chat_plot_id_st, chat_histories_st],
outputs=chat_submission_outputs,
api_name="click_suggestion_3"
)
def refresh_all_analytics_ui_elements(current_token_state, date_filter_val, custom_start_val, custom_end_val, current_chat_histories):
logging.info("Aggiornamento di tutti gli elementi UI delle analisi e reset delle azioni/chat.")
plot_generation_results = update_analytics_plots_figures(
current_token_state, date_filter_val, custom_start_val, custom_end_val
)
status_message_update = plot_generation_results[0]
generated_plot_figures = plot_generation_results[1:]
all_updates = [status_message_update]
for i in range(len(plot_configs)):
if i < len(generated_plot_figures):
all_updates.append(generated_plot_figures[i])
else:
all_updates.append(create_placeholder_plot("Errore Figura", f"Figura mancante per grafico {plot_configs[i]['id']}"))
all_updates.extend([
gr.update(visible=False),
gr.update(value=[], visible=False),
gr.update(value="", visible=False),
gr.update(visible=False),
gr.update(value="Suggerimento 1", visible=True),
gr.update(value="Suggerimento 2", visible=True),
gr.update(value="Suggerimento 3", visible=True),
gr.update(value="I dettagli sulla formula/metodologia appariranno qui.", visible=False),
None,
None,
current_chat_histories,
])
for cfg in plot_configs:
pid = cfg["id"]
if pid in plot_ui_objects:
all_updates.append(gr.update(value=BOMB_ICON))
all_updates.append(gr.update(value=FORMULA_ICON))
all_updates.append(gr.update(value=EXPLORE_ICON))
all_updates.append(gr.update(visible=True))
else:
all_updates.extend([None, None, None, None])
all_updates.append(None)
logging.info(f"Preparati {len(all_updates)} aggiornamenti per il refresh completo delle analisi.")
return all_updates
apply_filter_and_sync_outputs_list = [analytics_status_md]
for config_item_filter_sync in plot_configs:
pid_filter_sync = config_item_filter_sync["id"]
if pid_filter_sync in plot_ui_objects and "plot_component" in plot_ui_objects[pid_filter_sync]:
apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync]["plot_component"])
else:
apply_filter_and_sync_outputs_list.append(None)
apply_filter_and_sync_outputs_list.extend([
global_actions_column_ui,
insights_chatbot_ui,
insights_chat_input_ui,
insights_suggestions_row_ui,
insights_suggestion_1_btn,
insights_suggestion_2_btn,
insights_suggestion_3_btn,
formula_display_markdown_ui,
active_panel_action_state,
current_chat_plot_id_st,
chat_histories_st
])
for cfg_filter_sync_btns in plot_configs:
pid_filter_sync_btns = cfg_filter_sync_btns["id"]
if pid_filter_sync_btns in plot_ui_objects:
apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["bomb_button"])
apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["formula_button"])
apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["explore_button"])
apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["panel_component"])
else:
apply_filter_and_sync_outputs_list.extend([None, None, None, None])
apply_filter_and_sync_outputs_list.append(explored_plot_id_state)
logging.info(f"Output totali definiti per apply_filter/sync: {len(apply_filter_and_sync_outputs_list)}")
apply_filter_btn.click(
fn=refresh_all_analytics_ui_elements,
inputs=[token_state, date_filter_selector, custom_start_date_picker, custom_end_date_picker, chat_histories_st],
outputs=apply_filter_and_sync_outputs_list,
show_progress="full"
)
with gr.TabItem("3️⃣ Menzioni", id="tab_mentions"):
refresh_mentions_display_btn = gr.Button("🔄 Aggiorna Visualizzazione Menzioni", variant="secondary")
mentions_html = gr.HTML("Dati menzioni...")
mentions_sentiment_dist_plot = gr.Plot(label="Distribuzione Sentiment Menzioni")
refresh_mentions_display_btn.click(
fn=run_mentions_tab_display, inputs=[token_state],
outputs=[mentions_html, mentions_sentiment_dist_plot],
show_progress="full"
)
with gr.TabItem("4️⃣ Statistiche Follower", id="tab_follower_stats"):
refresh_follower_stats_btn = gr.Button("🔄 Aggiorna Visualizzazione Statistiche Follower", variant="secondary")
follower_stats_html = gr.HTML("Statistiche follower...")
with gr.Row():
fs_plot_monthly_gains = gr.Plot(label="Guadagni Mensili Follower")
with gr.Row():
fs_plot_seniority = gr.Plot(label="Follower per Anzianità (Top 10 Organici)")
fs_plot_industry = gr.Plot(label="Follower per Settore (Top 10 Organici)")
refresh_follower_stats_btn.click(
fn=run_follower_stats_tab_display, inputs=[token_state],
outputs=[follower_stats_html, fs_plot_monthly_gains, fs_plot_seniority, fs_plot_industry],
show_progress="full"
)
sync_event_part1 = sync_data_btn.click(
fn=sync_all_linkedin_data_orchestrator,
inputs=[token_state], outputs=[sync_status_html_output, token_state], show_progress="full"
)
sync_event_part2 = sync_event_part1.then(
fn=process_and_store_bubble_token,
inputs=[url_user_token_display, org_urn_display, token_state],
outputs=[status_box, token_state, sync_data_btn], show_progress=False
)
sync_event_part3 = sync_event_part2.then(
fn=display_main_dashboard,
inputs=[token_state], outputs=[dashboard_display_html], show_progress=False
)
sync_event_final = sync_event_part3.then(
fn=refresh_all_analytics_ui_elements,
inputs=[token_state, date_filter_selector, custom_start_date_picker, custom_end_date_picker, chat_histories_st],
outputs=apply_filter_and_sync_outputs_list,
show_progress="full"
)
if __name__ == "__main__":
if not os.environ.get(LINKEDIN_CLIENT_ID_ENV_VAR):
logging.warning(f"ATTENZIONE: Variabile d'ambiente '{LINKEDIN_CLIENT_ID_ENV_VAR}' non impostata.")
if not os.environ.get(BUBBLE_APP_NAME_ENV_VAR) or \
not os.environ.get(BUBBLE_API_KEY_PRIVATE_ENV_VAR) or \
not os.environ.get(BUBBLE_API_ENDPOINT_ENV_VAR):
logging.warning("ATTENZIONE: Variabili d'ambiente Bubble non completamente impostate.")
try:
logging.info(f"Versione Matplotlib: {matplotlib.__version__}, Backend: {matplotlib.get_backend()}")
except ImportError:
logging.warning("Matplotlib non trovato direttamente, ma potrebbe essere usato dai generatori di grafici.")
app.launch(server_name="0.0.0.0", server_port=7860, debug=True)