Spaces:
Build error
Build error
first version
Browse files- app.py +81 -0
- causalquest_embeddings_tsne.json +0 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import json
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
def load_data():
|
| 7 |
+
file_path = "causalquest_embeddings_tsne.json"
|
| 8 |
+
with open(file_path, 'r') as f:
|
| 9 |
+
data = json.load(f)
|
| 10 |
+
|
| 11 |
+
df = pd.DataFrame({
|
| 12 |
+
'query_id': data['query_id'],
|
| 13 |
+
'query': data['query'],
|
| 14 |
+
'source': data['source'],
|
| 15 |
+
'is_causal': data['is_causal'],
|
| 16 |
+
'domain_class': data['domain_class'],
|
| 17 |
+
'action_class': data['action_class'],
|
| 18 |
+
'is_subjective': data['is_subjective'],
|
| 19 |
+
'x': data['tsne_x'],
|
| 20 |
+
'y': data['tsne_y']
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
# subsample data for faster visualization
|
| 24 |
+
df = df.sample(1000, random_state=42)
|
| 25 |
+
|
| 26 |
+
return df
|
| 27 |
+
|
| 28 |
+
def create_plot(df):
|
| 29 |
+
fig = px.scatter(df, x='x', y='y', hover_data=['query', 'source', 'is_causal', 'domain_class', 'action_class', 'is_subjective'])
|
| 30 |
+
|
| 31 |
+
# Remove the hover box
|
| 32 |
+
fig.update_traces(hoverinfo='none', hovertemplate=None)
|
| 33 |
+
|
| 34 |
+
# Function to wrap long text
|
| 35 |
+
def wrap_text(text, max_length=50):
|
| 36 |
+
if len(text) <= max_length:
|
| 37 |
+
return text
|
| 38 |
+
words = text.split()
|
| 39 |
+
lines = []
|
| 40 |
+
current_line = []
|
| 41 |
+
current_length = 0
|
| 42 |
+
for word in words:
|
| 43 |
+
if current_length + len(word) + 1 <= max_length:
|
| 44 |
+
current_line.append(word)
|
| 45 |
+
current_length += len(word) + 1
|
| 46 |
+
else:
|
| 47 |
+
lines.append(' '.join(current_line))
|
| 48 |
+
current_line = [word]
|
| 49 |
+
current_length = len(word)
|
| 50 |
+
if current_line:
|
| 51 |
+
lines.append(' '.join(current_line))
|
| 52 |
+
return '<br>'.join(lines)
|
| 53 |
+
|
| 54 |
+
# Apply text wrapping to queries
|
| 55 |
+
df['wrapped_query'] = df['query'].apply(wrap_text)
|
| 56 |
+
|
| 57 |
+
# Add custom hover text with wrapped query
|
| 58 |
+
fig.update_traces(
|
| 59 |
+
customdata=df[['wrapped_query', 'source', 'is_causal', 'domain_class', 'action_class', 'is_subjective']],
|
| 60 |
+
hovertemplate='Query: %{customdata[0]}<br>Source: %{customdata[1]}<br>Is Causal: %{customdata[2]}'
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return fig
|
| 64 |
+
|
| 65 |
+
def visualize_data(processed_file):
|
| 66 |
+
df = load_data(processed_file)
|
| 67 |
+
fig = create_plot(df)
|
| 68 |
+
return fig
|
| 69 |
+
|
| 70 |
+
# Create Gradio interface
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
+
fn=visualize_data,
|
| 73 |
+
outputs=gr.Plot(label="tsne Visualization"),
|
| 74 |
+
title="CausalQuest Visualization",
|
| 75 |
+
description="Visualize causalquest data using pre-computed embeddings and tsne coordinates",
|
| 76 |
+
allow_flagging="never",
|
| 77 |
+
fill_width=True
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Launch the app
|
| 81 |
+
iface.launch()
|
causalquest_embeddings_tsne.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|