Spaces:
Sleeping
Sleeping
Tolulope Ogunremi
commited on
Commit
·
bf1fd5b
1
Parent(s):
9075cf3
adding new frontend
Browse files
app.py
CHANGED
|
@@ -1583,6 +1583,30 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1583 |
outputs=[context_analysis]
|
| 1584 |
)
|
| 1585 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1586 |
with gr.Tab("Embedding Projector"):
|
| 1587 |
gr.Markdown("### TensorFlow Projector-Style 3D Visualization")
|
| 1588 |
gr.Markdown("Interactive exploration similar to TensorFlow's Embedding Projector")
|
|
@@ -1599,12 +1623,24 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1599 |
)
|
| 1600 |
|
| 1601 |
projection_method = gr.Radio(
|
| 1602 |
-
|
| 1603 |
-
choices=['PCA', 'UMAP'],
|
| 1604 |
value='UMAP',
|
| 1605 |
label="Projection Method"
|
| 1606 |
)
|
| 1607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1608 |
dimension = gr.Radio(
|
| 1609 |
choices=['3D', '2D'],
|
| 1610 |
value='3D',
|
|
@@ -1628,19 +1664,57 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1628 |
value='By Label',
|
| 1629 |
label="Search Mode"
|
| 1630 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1631 |
|
| 1632 |
# Label search (simple)
|
| 1633 |
with gr.Group(visible=True) as label_search_group:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1634 |
search_label_type = gr.Radio(
|
| 1635 |
-
choices=[
|
| 1636 |
-
value=
|
| 1637 |
label="Search in"
|
| 1638 |
)
|
| 1639 |
-
|
| 1640 |
-
search_term = gr.
|
| 1641 |
-
|
| 1642 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1643 |
)
|
|
|
|
| 1644 |
|
| 1645 |
# Feature search (advanced)
|
| 1646 |
with gr.Group(visible=False) as feature_search_group:
|
|
@@ -1679,21 +1753,21 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1679 |
|
| 1680 |
search_btn = gr.Button("🔍 Search")
|
| 1681 |
|
| 1682 |
-
gr.Markdown("#### Nearest Neighbors")
|
| 1683 |
|
| 1684 |
-
point_idx = gr.Number(
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
-
|
| 1688 |
-
)
|
| 1689 |
|
| 1690 |
-
n_neighbors = gr.Slider(
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
|
| 1694 |
-
)
|
| 1695 |
|
| 1696 |
-
show_nn_btn = gr.Button("Show Neighbors")
|
| 1697 |
|
| 1698 |
info_display = gr.Markdown("*Select a point or search*")
|
| 1699 |
|
|
@@ -1707,7 +1781,7 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1707 |
# comparison_plot = gr.Plot(label="Comparison", visible=False)
|
| 1708 |
|
| 1709 |
# Projector callbacks
|
| 1710 |
-
def compute_projections(model_name, method):
|
| 1711 |
if not model_name or model_name not in analyzer.projector_vizs:
|
| 1712 |
return "Model not available", None
|
| 1713 |
|
|
@@ -1715,7 +1789,7 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1715 |
|
| 1716 |
try:
|
| 1717 |
method_lower = method.lower()
|
| 1718 |
-
viz.compute_projections(method_lower)
|
| 1719 |
|
| 1720 |
# Create initial plot
|
| 1721 |
proj_key = f"{method_lower}_3d"
|
|
@@ -1882,12 +1956,21 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1882 |
return gr.update(visible=True), fig
|
| 1883 |
|
| 1884 |
# Connect callbacks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1885 |
compute_btn.click(
|
| 1886 |
fn=compute_projections,
|
| 1887 |
-
inputs=[projector_model, projection_method
|
|
|
|
| 1888 |
outputs=[compute_status, projector_plot]
|
| 1889 |
)
|
| 1890 |
-
|
|
|
|
|
|
|
| 1891 |
search_mode.change(
|
| 1892 |
fn=toggle_search_mode,
|
| 1893 |
inputs=[search_mode],
|
|
@@ -1910,21 +1993,24 @@ def create_integrated_gradio_interface(analyzer: MultiModelAnalyzer):
|
|
| 1910 |
outputs=[projector_plot, info_display]
|
| 1911 |
)
|
| 1912 |
|
| 1913 |
-
show_nn_btn.click(
|
| 1914 |
-
|
| 1915 |
-
|
| 1916 |
-
|
| 1917 |
-
|
| 1918 |
-
)
|
| 1919 |
|
| 1920 |
# comparison_btn.click(
|
| 1921 |
# fn=lambda m, c: show_comparison_view(m, c),
|
| 1922 |
# inputs=[projector_model, projector_color_by],
|
| 1923 |
# outputs=[comparison_plot, comparison_plot]
|
| 1924 |
# )
|
|
|
|
| 1925 |
return demo
|
| 1926 |
|
| 1927 |
|
|
|
|
|
|
|
| 1928 |
def create_root_interface(output_dir):
|
| 1929 |
subdirs = get_top_level_dirs(output_dir)
|
| 1930 |
|
|
|
|
| 1583 |
outputs=[context_analysis]
|
| 1584 |
)
|
| 1585 |
"""
|
| 1586 |
+
|
| 1587 |
+
# def get_choices(model_name, label_type):
|
| 1588 |
+
# viz = analyzer.projector_vizs[model_name]
|
| 1589 |
+
# df = pd.DataFrame(viz.labels)
|
| 1590 |
+
# choices = [str(x) for x in df[label_type].unique()]
|
| 1591 |
+
# print(choices)
|
| 1592 |
+
# value = choices[0] if choices else None
|
| 1593 |
+
# return choices, value
|
| 1594 |
+
|
| 1595 |
+
def get_choices(model_name, label_type):
|
| 1596 |
+
viz = analyzer.projector_vizs[model_name]
|
| 1597 |
+
df = pd.DataFrame(viz.labels)
|
| 1598 |
+
if label_type == "phone":
|
| 1599 |
+
choices = df["phone"].unique()
|
| 1600 |
+
elif label_type == "cluster":
|
| 1601 |
+
choices = df["cluster"].unique()
|
| 1602 |
+
else:
|
| 1603 |
+
choices = df["language"].unique()
|
| 1604 |
+
|
| 1605 |
+
return gr.update(
|
| 1606 |
+
choices=[str(x) for x in choices], # MUST be a Python list of strings
|
| 1607 |
+
value=str(choices[0]) # MUST be one of the choices
|
| 1608 |
+
)
|
| 1609 |
+
|
| 1610 |
with gr.Tab("Embedding Projector"):
|
| 1611 |
gr.Markdown("### TensorFlow Projector-Style 3D Visualization")
|
| 1612 |
gr.Markdown("Interactive exploration similar to TensorFlow's Embedding Projector")
|
|
|
|
| 1623 |
)
|
| 1624 |
|
| 1625 |
projection_method = gr.Radio(
|
| 1626 |
+
choices=['PCA', 't-SNE', 'UMAP'],
|
| 1627 |
+
# choices=['PCA', 'UMAP'],
|
| 1628 |
value='UMAP',
|
| 1629 |
label="Projection Method"
|
| 1630 |
)
|
| 1631 |
+
|
| 1632 |
+
tsne_perplexity = gr.Slider(5, 50, value=30, step=1, label="t-SNE Perplexity", visible=False)
|
| 1633 |
+
tsne_lr = gr.Slider(10, 1000, value=200, step=10, label="t-SNE Learning Rate", visible=False)
|
| 1634 |
+
tsne_iters = gr.Slider(250, 5000, value=1000, step=250, label="t-SNE Iterations", visible=False)
|
| 1635 |
+
|
| 1636 |
+
|
| 1637 |
+
projection_method.change(
|
| 1638 |
+
fn=toggle_tsne_params,
|
| 1639 |
+
inputs=[projection_method],
|
| 1640 |
+
outputs=[tsne_perplexity, tsne_lr, tsne_iters]
|
| 1641 |
+
)
|
| 1642 |
+
|
| 1643 |
+
|
| 1644 |
dimension = gr.Radio(
|
| 1645 |
choices=['3D', '2D'],
|
| 1646 |
value='3D',
|
|
|
|
| 1664 |
value='By Label',
|
| 1665 |
label="Search Mode"
|
| 1666 |
)
|
| 1667 |
+
|
| 1668 |
+
phones = ["æ", "ɑ", "ə", "i", "u"]
|
| 1669 |
+
clusters = [0, 1, 2, 3]
|
| 1670 |
+
languages = ["hi", "pa"]
|
| 1671 |
|
| 1672 |
# Label search (simple)
|
| 1673 |
with gr.Group(visible=True) as label_search_group:
|
| 1674 |
+
# search_label_type = gr.Radio(
|
| 1675 |
+
# choices=['phone', 'cluster', 'language'],
|
| 1676 |
+
# value='phone',
|
| 1677 |
+
# label="Search in"
|
| 1678 |
+
# )
|
| 1679 |
+
|
| 1680 |
+
# search_term = gr.Textbox(
|
| 1681 |
+
# label="Search term",
|
| 1682 |
+
# placeholder="e.g., 'æ' or '5'"
|
| 1683 |
+
# )
|
| 1684 |
+
|
| 1685 |
+
# search_term = gr.Dropdown(
|
| 1686 |
+
# choices=list(phones), # initial choices
|
| 1687 |
+
# value=phones[0], # initial value
|
| 1688 |
+
# label="Search term",
|
| 1689 |
+
# allow_custom_value=True
|
| 1690 |
+
# )
|
| 1691 |
+
|
| 1692 |
+
# # Update dropdown choices when the label type changes
|
| 1693 |
+
# # Update search_term whenever the label type changes
|
| 1694 |
+
# search_label_type.change(
|
| 1695 |
+
# fn=get_choices,
|
| 1696 |
+
# inputs=[projector_model, search_label_type],
|
| 1697 |
+
# outputs=[search_term, search_term] # first = choices, second = value
|
| 1698 |
+
# )
|
| 1699 |
+
|
| 1700 |
search_label_type = gr.Radio(
|
| 1701 |
+
choices=["phone", "cluster", "language"],
|
| 1702 |
+
value="phone",
|
| 1703 |
label="Search in"
|
| 1704 |
)
|
| 1705 |
+
|
| 1706 |
+
search_term = gr.Dropdown(
|
| 1707 |
+
choices=[str(x) for x in phones],
|
| 1708 |
+
value=str(phones[0]),
|
| 1709 |
+
label="Search term"
|
| 1710 |
+
)
|
| 1711 |
+
|
| 1712 |
+
search_label_type.change(
|
| 1713 |
+
fn=get_choices,
|
| 1714 |
+
inputs=[projector_model, search_label_type],
|
| 1715 |
+
outputs=search_term
|
| 1716 |
)
|
| 1717 |
+
|
| 1718 |
|
| 1719 |
# Feature search (advanced)
|
| 1720 |
with gr.Group(visible=False) as feature_search_group:
|
|
|
|
| 1753 |
|
| 1754 |
search_btn = gr.Button("🔍 Search")
|
| 1755 |
|
| 1756 |
+
# gr.Markdown("#### Nearest Neighbors")
|
| 1757 |
|
| 1758 |
+
# point_idx = gr.Number(
|
| 1759 |
+
# label="Point index",
|
| 1760 |
+
# value=0,
|
| 1761 |
+
# precision=0
|
| 1762 |
+
# )
|
| 1763 |
|
| 1764 |
+
# n_neighbors = gr.Slider(
|
| 1765 |
+
# 1, 50, value=10,
|
| 1766 |
+
# step=1,
|
| 1767 |
+
# label="Number of neighbors"
|
| 1768 |
+
# )
|
| 1769 |
|
| 1770 |
+
# show_nn_btn = gr.Button("Show Neighbors")
|
| 1771 |
|
| 1772 |
info_display = gr.Markdown("*Select a point or search*")
|
| 1773 |
|
|
|
|
| 1781 |
# comparison_plot = gr.Plot(label="Comparison", visible=False)
|
| 1782 |
|
| 1783 |
# Projector callbacks
|
| 1784 |
+
def compute_projections(model_name, method, tsne_perplexity, tsne_lr, tsne_iters):
|
| 1785 |
if not model_name or model_name not in analyzer.projector_vizs:
|
| 1786 |
return "Model not available", None
|
| 1787 |
|
|
|
|
| 1789 |
|
| 1790 |
try:
|
| 1791 |
method_lower = method.lower()
|
| 1792 |
+
viz.compute_projections(method_lower, tsne_perplexity, tsne_lr, tsne_iters)
|
| 1793 |
|
| 1794 |
# Create initial plot
|
| 1795 |
proj_key = f"{method_lower}_3d"
|
|
|
|
| 1956 |
return gr.update(visible=True), fig
|
| 1957 |
|
| 1958 |
# Connect callbacks
|
| 1959 |
+
# compute_btn.click(
|
| 1960 |
+
# fn=compute_projections,
|
| 1961 |
+
# inputs=[projector_model, projection_method],
|
| 1962 |
+
# outputs=[compute_status, projector_plot]
|
| 1963 |
+
# )
|
| 1964 |
+
|
| 1965 |
compute_btn.click(
|
| 1966 |
fn=compute_projections,
|
| 1967 |
+
inputs=[projector_model, projection_method,
|
| 1968 |
+
tsne_perplexity, tsne_lr, tsne_iters],
|
| 1969 |
outputs=[compute_status, projector_plot]
|
| 1970 |
)
|
| 1971 |
+
|
| 1972 |
+
|
| 1973 |
+
|
| 1974 |
search_mode.change(
|
| 1975 |
fn=toggle_search_mode,
|
| 1976 |
inputs=[search_mode],
|
|
|
|
| 1993 |
outputs=[projector_plot, info_display]
|
| 1994 |
)
|
| 1995 |
|
| 1996 |
+
# show_nn_btn.click(
|
| 1997 |
+
# fn=show_neighbors,
|
| 1998 |
+
# inputs=[projector_model, point_idx, n_neighbors,
|
| 1999 |
+
# projection_method, dimension, projector_color_by],
|
| 2000 |
+
# outputs=[projector_plot, info_display]
|
| 2001 |
+
# )
|
| 2002 |
|
| 2003 |
# comparison_btn.click(
|
| 2004 |
# fn=lambda m, c: show_comparison_view(m, c),
|
| 2005 |
# inputs=[projector_model, projector_color_by],
|
| 2006 |
# outputs=[comparison_plot, comparison_plot]
|
| 2007 |
# )
|
| 2008 |
+
|
| 2009 |
return demo
|
| 2010 |
|
| 2011 |
|
| 2012 |
+
|
| 2013 |
+
|
| 2014 |
def create_root_interface(output_dir):
|
| 2015 |
subdirs = get_top_level_dirs(output_dir)
|
| 2016 |
|