in src/embeddings_streamlit.py [0:0]
def main():
st.title("Hugging Face Embeddings Visualization")
# --- Model Selection ---
model_name = st.text_input(
"Enter Hugging Face Model Name",
value="sentence-transformers/all-MiniLM-L6-v2"
)
# Load the model
model = load_model(model_name)
# --- Text Input ---
user_input = st.text_area(
"Enter one or more sentences (each on a new line):",
"Hello world!\nStreamlit is awesome.\nHugging Face rocks!"
)
texts = [preprocess(line.strip()) for line in user_input.split('\n') if line.strip()]
# --- Reduction Options ---
method = st.radio("Dimensionality Reduction Method", ("PCA", "t-SNE"))
dims = st.radio("Number of Dimensions to Display", (2, 3))
# --- Button to Compute ---
if st.button("Compute & Visualize Embeddings"):
if len(texts) == 0:
st.warning("Please enter some text.")
else:
# Get embeddings
embeddings = get_embeddings(texts, model)
# Reduce to 2D or 3D
reduced_embeddings = reduce_dimensions(embeddings, method=method, n_components=dims)
if dims == 2:
# 2D Plot
fig = px.scatter(
x=reduced_embeddings[:, 0],
y=reduced_embeddings[:, 1],
text=texts,
title=f"{method} Embeddings (2D)"
)
fig.update_traces(textposition='top center')
st.plotly_chart(fig, use_container_width=True)
else:
# 3D Plot
fig = px.scatter_3d(
x=reduced_embeddings[:, 0],
y=reduced_embeddings[:, 1],
z=reduced_embeddings[:, 2],
text=texts,
title=f"{method} Embeddings (3D)"
)
st.plotly_chart(fig, use_container_width=True)