import gradio as gr import logging # Set up logging logger = logging.getLogger('gradio_app.processors.topic') def process_topic_modeling(analysis_results, prompt, analyses): """ Process Topic Modeling analysis and return UI updates Args: analysis_results (dict): Complete analysis results prompt (str): The prompt being analyzed analyses (dict): Analysis data for the prompt Returns: tuple: UI component updates """ topic_results = analyses["topic_modeling"] # Check for errors in topic modeling if "error" in topic_results: return ( analysis_results, False, # Don't show raw JSON False, # Don't show visualization area gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), True, # Show status message gr.update(visible=True, value=f"❌ **Topic modeling error:** {topic_results['error']}"), gr.update(visible=False) # bias_visualizations ) visualization_area_visible = True models = topic_results.get("models", []) method = topic_results.get("method", "lda").upper() n_topics = topic_results.get("n_topics", 3) if len(models) < 2: from analysis_runner import default_no_visualization return default_no_visualization(analysis_results) prompt_title_visible = True prompt_title_value = f"## Analysis of Prompt: \"{prompt[:100]}...\"" models_compared_visible = True models_compared_value = f"### Topic Modeling Analysis ({method}, {n_topics} topics)" # Initialize component visibility and values model1_title_visible = False model1_title_value = "" model1_words_visible = False model1_words_value = "" model2_title_visible = False model2_title_value = "" model2_words_visible = False model2_words_value = "" similarity_title_visible = False similarity_metrics_visible = False similarity_metrics_value = "" # Extract and format topic information topics = topic_results.get("topics", []) if topics: # Format topic info for display topic_info = [] for topic in topics[:5]: # Show first 5 topics topic_id = topic.get("id", 0) words = topic.get("words", [])[:5] # Top 5 words per topic if words: topic_info.append(f"**Topic {topic_id + 1}**: {', '.join(words)}") if topic_info: model1_title_visible = True model1_title_value = "#### Discovered Topics" model1_words_visible = True model1_words_value = "\n".join(topic_info) # Get topic distributions for models model_topics = topic_results.get("model_topics", {}) if model_topics: model1_name = models[0] model2_name = models[1] # Format topic distribution info if model1_name in model_topics and model2_name in model_topics: model2_title_visible = True model2_title_value = "#### Topic Distribution" model2_words_visible = True # Simple distribution display dist1 = model_topics[model1_name] dist2 = model_topics[model2_name] model2_words_value = f""" **{model1_name}**: {', '.join([f"Topic {i + 1}: {v:.2f}" for i, v in enumerate(dist1[:5])])} **{model2_name}**: {', '.join([f"Topic {i + 1}: {v:.2f}" for i, v in enumerate(dist2[:5])])} """ # Add similarity metrics if available comparisons = topic_results.get("comparisons", {}) if comparisons: comparison_key = f"{model1_name} vs {model2_name}" if comparison_key in comparisons: metrics = comparisons[comparison_key] js_div = metrics.get("js_divergence", 0) # Add interpretation similarity_text = "" if js_div < 0.2: similarity_text = "very similar" elif js_div < 0.4: similarity_text = "somewhat similar" elif js_div < 0.6: similarity_text = "moderately different" else: similarity_text = "very different" similarity_title_visible = True similarity_metrics_visible = True similarity_metrics_value = f""" - **Topic Distribution Divergence**: {js_div:.4f} - The topic distributions between models are **{similarity_text}** - *Lower divergence values indicate more similar topic distributions* """ return ( analysis_results, # analysis_results_state False, # analysis_output visibility True, # visualization_area_visible gr.update(visible=True), # analysis_title gr.update(visible=prompt_title_visible, value=prompt_title_value), # prompt_title gr.update(visible=models_compared_visible, value=models_compared_value), # models_compared gr.update(visible=model1_title_visible, value=model1_title_value), # model1_title gr.update(visible=model1_words_visible, value=model1_words_value), # model1_words gr.update(visible=model2_title_visible, value=model2_title_value), # model2_title gr.update(visible=model2_words_visible, value=model2_words_value), # model2_words gr.update(visible=similarity_title_visible), # similarity_metrics_title gr.update(visible=similarity_metrics_visible, value=similarity_metrics_value), # similarity_metrics False, # status_message_visible gr.update(visible=False), # status_message gr.update(visible=False) # bias_visualizations - Not visible for Topic Modeling )