Add Plotly Express
Browse files
app.py
CHANGED
@@ -21,11 +21,23 @@ import gradio as gr
|
|
21 |
import numpy as np
|
22 |
import numpy.typing as npt
|
23 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
|
26 |
# https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-newman-py
|
27 |
def aggregate(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
|
28 |
-
seed: int = 0, tolerance: float = 10e-6, limit: int = 20) -> npt.
|
29 |
assert wins.shape == ties.shape, 'wins and ties shapes are different'
|
30 |
|
31 |
rng = np.random.default_rng(seed)
|
@@ -71,7 +83,7 @@ def aggregate(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
|
|
71 |
return pi
|
72 |
|
73 |
|
74 |
-
def handler(file: typing.IO[bytes], seed: int) -> pd.DataFrame:
|
75 |
if file is None:
|
76 |
raise gr.Error('File must be uploaded')
|
77 |
|
@@ -114,7 +126,12 @@ def handler(file: typing.IO[bytes], seed: int) -> pd.DataFrame:
|
|
114 |
df_result.sort_values(by=['rank', 'score'], ascending=[True, False], inplace=True)
|
115 |
df_result.reset_index(inplace=True)
|
116 |
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
|
120 |
def main() -> None:
|
@@ -131,10 +148,15 @@ def main() -> None:
|
|
131 |
precision=0
|
132 |
)
|
133 |
],
|
134 |
-
outputs=
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
138 |
title='Pair2Rank: Turn Your Side-by-Side Comparisons into Ranking!',
|
139 |
description='''
|
140 |
This easy-to-use tool transforms pairwise comparisons (aka side-by-side) to a meaningful ranking of items.
|
|
|
21 |
import numpy as np
|
22 |
import numpy.typing as npt
|
23 |
import pandas as pd
|
24 |
+
import plotly.express as px
|
25 |
+
from plotly.graph_objects import Figure
|
26 |
+
|
27 |
+
|
28 |
+
def visualize(df_pairwise: pd.DataFrame) -> Figure:
|
29 |
+
fig = px.imshow(df_pairwise, color_continuous_scale='RdBu', text_auto='.2f')
|
30 |
+
|
31 |
+
fig.update_layout(xaxis_title='Loser', yaxis_title='Winner', xaxis_side='top')
|
32 |
+
|
33 |
+
fig.update_traces(hovertemplate='Winner: %{y}<br>Loser: %{x}<br>Fraction of Wins: %{z}<extra></extra>')
|
34 |
+
|
35 |
+
return fig
|
36 |
|
37 |
|
38 |
# https://gist.github.com/dustalov/41678b70c40ba5a55430fa5e77b121d9#file-newman-py
|
39 |
def aggregate(wins: npt.NDArray[np.int64], ties: npt.NDArray[np.int64],
|
40 |
+
seed: int = 0, tolerance: float = 10e-6, limit: int = 20) -> npt.NDArray[np.float64]:
|
41 |
assert wins.shape == ties.shape, 'wins and ties shapes are different'
|
42 |
|
43 |
rng = np.random.default_rng(seed)
|
|
|
83 |
return pi
|
84 |
|
85 |
|
86 |
+
def handler(file: typing.IO[bytes], seed: int) -> typing.Tuple[pd.DataFrame, Figure]:
|
87 |
if file is None:
|
88 |
raise gr.Error('File must be uploaded')
|
89 |
|
|
|
126 |
df_result.sort_values(by=['rank', 'score'], ascending=[True, False], inplace=True)
|
127 |
df_result.reset_index(inplace=True)
|
128 |
|
129 |
+
df_pairwise = pd.DataFrame(data=scores[:, np.newaxis] / (scores + scores[:, np.newaxis]),
|
130 |
+
index=index, columns=index)
|
131 |
+
|
132 |
+
fig = visualize(df_pairwise)
|
133 |
+
|
134 |
+
return df_result, fig
|
135 |
|
136 |
|
137 |
def main() -> None:
|
|
|
148 |
precision=0
|
149 |
)
|
150 |
],
|
151 |
+
outputs=[
|
152 |
+
gr.Dataframe(
|
153 |
+
headers=['item', 'score', 'rank'],
|
154 |
+
label='Ranking'
|
155 |
+
),
|
156 |
+
gr.Plot(
|
157 |
+
label='Chances of Winning the Comparison'
|
158 |
+
)
|
159 |
+
],
|
160 |
title='Pair2Rank: Turn Your Side-by-Side Comparisons into Ranking!',
|
161 |
description='''
|
162 |
This easy-to-use tool transforms pairwise comparisons (aka side-by-side) to a meaningful ranking of items.
|