525GradioApp / processors /bias_processor.py
Ryan
update
11b02dc
raw
history blame
4.72 kB
import gradio as gr
import logging
import traceback
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import io
import base64
# Set up logging
logger = logging.getLogger('gradio_app.processors.bias')
def process_bias_detection(analysis_results, prompt, analyses):
"""
Process Bias Detection 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
"""
logger.info("Processing Bias Detection visualization")
models = analyses["bias_detection"].get("models", ["Model 1", "Model 2"])
logger.info(f"Bias models: {models}")
try:
# Get the bias detection results
bias_results = analyses["bias_detection"]
# Create markdown text for bias analysis results
results_markdown = f"""
## Bias Analysis Results
### Sentiment Analysis
- {models[0]}: {bias_results[models[0]]['sentiment']['bias_direction']} (strength: {bias_results[models[0]]['sentiment']['bias_strength']:.2f})
- {models[1]}: {bias_results[models[1]]['sentiment']['bias_direction']} (strength: {bias_results[models[1]]['sentiment']['bias_strength']:.2f})
- Difference: {bias_results['comparative']['sentiment']['difference']:.2f}
### Partisan Leaning
- {models[0]}: {bias_results[models[0]]['partisan']['leaning']} (score: {bias_results[models[0]]['partisan']['lean_score']:.2f})
- {models[1]}: {bias_results[models[1]]['partisan']['leaning']} (score: {bias_results[models[1]]['partisan']['lean_score']:.2f})
- Difference: {bias_results['comparative']['partisan']['difference']:.2f}
### Framing Analysis
- {models[0]} dominant frame: {bias_results[models[0]]['framing']['dominant_frame']}
- {models[1]} dominant frame: {bias_results[models[1]]['framing']['dominant_frame']}
- Different frames: {'Yes' if bias_results['comparative']['framing']['different_frames'] else 'No'}
### Liberal Terms Found
- {models[0]}: {', '.join(bias_results[models[0]]['partisan']['liberal_terms'][:10])}
- {models[1]}: {', '.join(bias_results[models[1]]['partisan']['liberal_terms'][:10])}
### Conservative Terms Found
- {models[0]}: {', '.join(bias_results[models[0]]['partisan']['conservative_terms'][:10])}
- {models[1]}: {', '.join(bias_results[models[1]]['partisan']['conservative_terms'][:10])}
### Overall Comparison
The overall bias difference is {bias_results['comparative']['overall']['difference']:.2f}, which is
{'significant' if bias_results['comparative']['overall']['significant_bias_difference'] else 'not significant'}.
"""
# Return the expected components
return (
analysis_results, # analysis_results_state
False, # analysis_output visibility
True, # visualization_area_visible
gr.update(visible=True), # analysis_title
gr.update(visible=True, value=f"## Analysis of Prompt: \"{prompt[:100]}...\""), # prompt_title
gr.update(visible=True, value=f"### Comparing responses from {models[0]} and {models[1]}"), # models_compared
gr.update(visible=True, value="#### Bias detection visualization is available below"), # model1_title
gr.update(visible=True, value="The detailed bias analysis includes sentiment analysis, partisan term detection, and framing analysis."), # model1_words
gr.update(visible=False), # model2_title
gr.update(visible=False), # model2_words
gr.update(visible=False), # similarity_metrics_title
gr.update(visible=False), # similarity_metrics
False, # status_message_visible
gr.update(visible=False), # status_message
gr.update(visible=True, value=results_markdown) # bias_visualizations - Pass markdown content
)
except Exception as e:
logger.error(f"Error generating bias visualization: {str(e)}\n{traceback.format_exc()}")
return (
analysis_results,
True, # Show raw JSON for debugging
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),
gr.update(visible=False),
True,
gr.update(visible=True, value=f"❌ **Error generating bias visualization:** {str(e)}"),
gr.update(visible=False) # bias_visualizations
)