Spaces:
Sleeping
Sleeping
Ryan
commited on
Commit
·
95d7f60
1
Parent(s):
7731b47
update
Browse files- ui/roberta_screen.py +125 -0
ui/roberta_screen.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
UI components for RoBERTa sentiment analysis screen
|
3 |
+
"""
|
4 |
+
import gradio as gr
|
5 |
+
from processors.roberta_analysis import compare_sentiment_roberta
|
6 |
+
|
7 |
+
def create_roberta_screen():
|
8 |
+
"""
|
9 |
+
Create the RoBERTa sentiment analysis screen components
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
tuple: (run_roberta_btn, roberta_output, other components...)
|
13 |
+
"""
|
14 |
+
with gr.Column() as roberta_screen:
|
15 |
+
gr.Markdown("## RoBERTa Sentiment Analysis")
|
16 |
+
gr.Markdown("""
|
17 |
+
This tab uses the RoBERTa transformer model to perform sentiment analysis on the LLM responses
|
18 |
+
and compare their emotional tones. RoBERTa was trained on a diverse dataset and can detect subtle
|
19 |
+
differences in sentiment that simpler rule-based classifiers might miss.
|
20 |
+
|
21 |
+
Click 'Run Sentiment Analysis' to analyze your dataset.
|
22 |
+
""")
|
23 |
+
|
24 |
+
with gr.Row():
|
25 |
+
with gr.Column(scale=1):
|
26 |
+
# Analysis options (expandable in future)
|
27 |
+
with gr.Accordion("Advanced Options", open=False):
|
28 |
+
sentence_level = gr.Checkbox(
|
29 |
+
value=True,
|
30 |
+
label="Perform sentence-level analysis",
|
31 |
+
info="Analyze sentiment at the sentence level for more detailed results"
|
32 |
+
)
|
33 |
+
|
34 |
+
visualization_style = gr.Radio(
|
35 |
+
choices=["Standard", "Detailed"],
|
36 |
+
value="Standard",
|
37 |
+
label="Visualization Style",
|
38 |
+
info="Standard is more concise, Detailed shows more information"
|
39 |
+
)
|
40 |
+
|
41 |
+
with gr.Column(scale=1):
|
42 |
+
# Run button in its own column for better visibility
|
43 |
+
run_roberta_btn = gr.Button("Run Sentiment Analysis", variant="primary", size="large")
|
44 |
+
|
45 |
+
# Hidden output to store raw analysis results
|
46 |
+
roberta_output = gr.JSON(label="Sentiment Analysis Results", visible=False)
|
47 |
+
|
48 |
+
# Pre-create visualization components (all initially hidden)
|
49 |
+
visualization_container = gr.Markdown(visible=False)
|
50 |
+
status_message = gr.Markdown(visible=False)
|
51 |
+
|
52 |
+
return run_roberta_btn, roberta_output, sentence_level, visualization_style, visualization_container, status_message
|
53 |
+
|
54 |
+
def process_roberta_request(dataset, sentence_level=True, visualization_style="Standard"):
|
55 |
+
"""
|
56 |
+
Process the RoBERTa sentiment analysis request
|
57 |
+
|
58 |
+
Args:
|
59 |
+
dataset (dict): The input dataset
|
60 |
+
sentence_level (bool): Whether to perform sentence-level analysis
|
61 |
+
visualization_style (str): Visualization style preference
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
dict: Analysis results
|
65 |
+
"""
|
66 |
+
if not dataset or "entries" not in dataset or not dataset["entries"]:
|
67 |
+
return {"error": "No dataset loaded. Please create or load a dataset first."}
|
68 |
+
|
69 |
+
# Initialize the results structure
|
70 |
+
results = {"analyses": {}}
|
71 |
+
|
72 |
+
# Get the prompt text from the first entry
|
73 |
+
prompt_text = dataset["entries"][0].get("prompt", "")
|
74 |
+
if not prompt_text:
|
75 |
+
return {"error": "No prompt found in dataset"}
|
76 |
+
|
77 |
+
# Initialize the analysis container for this prompt
|
78 |
+
results["analyses"][prompt_text] = {}
|
79 |
+
|
80 |
+
# Get model names and responses
|
81 |
+
model1_name = dataset["entries"][0].get("model", "Model 1")
|
82 |
+
model2_name = dataset["entries"][1].get("model", "Model 2")
|
83 |
+
|
84 |
+
model1_response = dataset["entries"][0].get("response", "")
|
85 |
+
model2_response = dataset["entries"][1].get("response", "")
|
86 |
+
|
87 |
+
try:
|
88 |
+
# Perform RoBERTa sentiment analysis
|
89 |
+
print("Starting RoBERTa sentiment analysis...")
|
90 |
+
|
91 |
+
sentiment_results = compare_sentiment_roberta(
|
92 |
+
texts=[model1_response, model2_response],
|
93 |
+
model_names=[model1_name, model2_name]
|
94 |
+
)
|
95 |
+
|
96 |
+
# Store the results
|
97 |
+
results["analyses"][prompt_text]["roberta_sentiment"] = sentiment_results
|
98 |
+
|
99 |
+
# Add metadata about the analysis
|
100 |
+
results["analyses"][prompt_text]["roberta_sentiment"]["metadata"] = {
|
101 |
+
"sentence_level": sentence_level,
|
102 |
+
"visualization_style": visualization_style
|
103 |
+
}
|
104 |
+
|
105 |
+
return results
|
106 |
+
|
107 |
+
except Exception as e:
|
108 |
+
import traceback
|
109 |
+
error_msg = f"Error in RoBERTa sentiment analysis: {str(e)}\n{traceback.format_exc()}"
|
110 |
+
print(error_msg)
|
111 |
+
|
112 |
+
# Return error information
|
113 |
+
return {
|
114 |
+
"error": str(e),
|
115 |
+
"traceback": traceback.format_exc(),
|
116 |
+
"analyses": {
|
117 |
+
prompt_text: {
|
118 |
+
"roberta_sentiment": {
|
119 |
+
"error": str(e),
|
120 |
+
"models": [model1_name, model2_name],
|
121 |
+
"message": "RoBERTa sentiment analysis failed. Try again or use a different analysis method."
|
122 |
+
}
|
123 |
+
}
|
124 |
+
}
|
125 |
+
}
|