525GradioApp / processors /ngram_processor.py
Ryan
update
e66f533
raw
history blame
4.12 kB
import gradio as gr
import logging
# Set up logging
logger = logging.getLogger('gradio_app.processors.ngram')
def process_ngram_analysis(analysis_results, prompt, analyses):
"""
Process N-gram 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
ngram_results = analyses["ngram_analysis"]
models = ngram_results.get("models", [])
ngram_size = ngram_results.get("ngram_size", 2)
size_name = "Unigrams" if ngram_size == 1 else f"{ngram_size}-grams"
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"### {size_name} Analysis: Comparing responses from {models[0]} and {models[1]}"
# Extract and format information for display
model1_name = models[0]
model2_name = models[1]
# Format important n-grams for each model
important_ngrams = ngram_results.get("important_ngrams", {})
model1_title_visible = False
model1_title_value = ""
model1_words_visible = False
model1_words_value = ""
if model1_name in important_ngrams:
model1_title_visible = True
model1_title_value = f"#### Top {size_name} Used by {model1_name}"
ngram_list = [f"**{item['ngram']}** ({item['count']})" for item in important_ngrams[model1_name][:10]]
model1_words_visible = True
model1_words_value = ", ".join(ngram_list)
model2_title_visible = False
model2_title_value = ""
model2_words_visible = False
model2_words_value = ""
if model2_name in important_ngrams:
model2_title_visible = True
model2_title_value = f"#### Top {size_name} Used by {model2_name}"
ngram_list = [f"**{item['ngram']}** ({item['count']})" for item in important_ngrams[model2_name][:10]]
model2_words_visible = True
model2_words_value = ", ".join(ngram_list)
similarity_title_visible = False
similarity_metrics_visible = False
similarity_metrics_value = ""
# Format similarity metrics if available
if "comparisons" in ngram_results:
comparison_key = f"{model1_name} vs {model2_name}"
if comparison_key in ngram_results["comparisons"]:
metrics = ngram_results["comparisons"][comparison_key]
common_count = metrics.get("common_ngram_count", 0)
similarity_title_visible = True
similarity_metrics_visible = True
similarity_metrics_value = f"""
- **Common {size_name}**: {common_count} {size_name.lower()} 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 N-gram analysis
)