Spaces:
Sleeping
Sleeping
File size: 4,274 Bytes
e66f533 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
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
) |