Spaces:
Sleeping
Sleeping
File size: 4,722 Bytes
e66f533 da60688 e66f533 a109ed5 11b02dc a109ed5 e66f533 11b02dc e66f533 11b02dc e66f533 11b02dc a109ed5 e66f533 a109ed5 11b02dc e66f533 da60688 e66f533 11b02dc 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 |
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
) |