Spaces:
Sleeping
Sleeping
import gradio as gr | |
import logging | |
# Set up logging | |
logger = logging.getLogger('gradio_app.processors.bow') | |
def process_bow_analysis(analysis_results, prompt, analyses): | |
""" | |
Process Bag of Words 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 | |
""" | |
visualization_area_visible = True | |
bow_results = analyses["bag_of_words"] | |
models = bow_results.get("models", []) | |
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"### Comparing responses from {models[0]} and {models[1]}" | |
# Extract and format information for display | |
model1_name = models[0] | |
model2_name = models[1] | |
# Format important words for each model | |
important_words = bow_results.get("important_words", {}) | |
model1_title_visible = False | |
model1_title_value = "" | |
model1_words_visible = False | |
model1_words_value = "" | |
if model1_name in important_words: | |
model1_title_visible = True | |
model1_title_value = f"#### Top Words Used by {model1_name}" | |
word_list = [f"**{item['word']}** ({item['count']})" for item in important_words[model1_name][:10]] | |
model1_words_visible = True | |
model1_words_value = ", ".join(word_list) | |
model2_title_visible = False | |
model2_title_value = "" | |
model2_words_visible = False | |
model2_words_value = "" | |
if model2_name in important_words: | |
model2_title_visible = True | |
model2_title_value = f"#### Top Words Used by {model2_name}" | |
word_list = [f"**{item['word']}** ({item['count']})" for item in important_words[model2_name][:10]] | |
model2_words_visible = True | |
model2_words_value = ", ".join(word_list) | |
similarity_title_visible = False | |
similarity_metrics_visible = False | |
similarity_metrics_value = "" | |
# Format similarity metrics | |
comparisons = bow_results.get("comparisons", {}) | |
comparison_key = f"{model1_name} vs {model2_name}" | |
if comparison_key in comparisons: | |
metrics = comparisons[comparison_key] | |
cosine = metrics.get("cosine_similarity", 0) | |
jaccard = metrics.get("jaccard_similarity", 0) | |
semantic = metrics.get("semantic_similarity", 0) | |
common_words = metrics.get("common_word_count", 0) | |
similarity_title_visible = True | |
similarity_metrics_visible = True | |
similarity_metrics_value = f""" | |
- **Cosine Similarity**: {cosine:.2f} (higher means more similar word frequency patterns) | |
- **Jaccard Similarity**: {jaccard:.2f} (higher means more word overlap) | |
- **Semantic Similarity**: {semantic:.2f} (higher means more similar meaning) | |
- **Common Words**: {common_words} words appear in both responses | |
""" | |
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 BoW analysis | |
) |