Spaces:
Running on Zero
Running on Zero
Download embedding_utils.py from ExperienceNow/indobert-embedding-explorer: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/spaces/ExperienceNow/indobert-embedding-explorer/resolve/main/embedding_utils.py
- Command line
-
hf download hf://spaces/ExperienceNow/indobert-embedding-explorer/embedding_utils.py
-
curl -L -o embedding_utils.py https://huggingface.co/spaces/ExperienceNow/indobert-embedding-explorer/resolve/main/embedding_utils.py
2.41 kB
| """Small, dependency-free helpers for the IndoBERT embedding demo.""" | |
| from __future__ import annotations | |
| from math import sqrt | |
| import numpy as np | |
| def parse_input_lines(raw_text: str, max_samples: int = 6, max_chars: int = 280) -> list[str]: | |
| """Parse newline-separated samples and enforce the demo's input limits.""" | |
| texts = [line.strip() for line in raw_text.splitlines() if line.strip()] | |
| if not texts: | |
| raise ValueError("Masukkan setidaknya satu kalimat Bahasa Indonesia.") | |
| if len(texts) > max_samples: | |
| raise ValueError(f"Maksimal {max_samples} kalimat dalam satu kali analisis.") | |
| if any(len(text) > max_chars for text in texts): | |
| raise ValueError(f"Setiap kalimat dibatasi hingga {max_chars} karakter.") | |
| return texts | |
| def cosine_similarity_matrix(vectors: list[list[float]]) -> list[list[float]]: | |
| """Return a symmetric cosine-similarity matrix; zero vectors score 0.""" | |
| if not vectors: | |
| return [] | |
| dimensions = len(vectors[0]) | |
| if any(len(vector) != dimensions for vector in vectors): | |
| raise ValueError("Semua embedding harus memiliki dimensi yang sama.") | |
| norms = [sqrt(sum(value * value for value in vector)) for vector in vectors] | |
| matrix: list[list[float]] = [] | |
| for i, left in enumerate(vectors): | |
| row = [] | |
| for j, right in enumerate(vectors): | |
| denominator = norms[i] * norms[j] | |
| similarity = sum(a * b for a, b in zip(left, right)) / denominator if denominator else 0.0 | |
| row.append(round(similarity, 6)) | |
| matrix.append(row) | |
| return matrix | |
| def principal_component_projection(vectors: list[list[float]]) -> list[list[float]]: | |
| """Project rows to two centered PCA coordinates for a small-sample chart.""" | |
| if not vectors: | |
| return [] | |
| values = np.asarray(vectors, dtype=np.float64) | |
| if values.ndim != 2: | |
| raise ValueError("Embedding harus berupa matriks dua dimensi.") | |
| centered = values - values.mean(axis=0, keepdims=True) | |
| if values.shape[0] == 1: | |
| return [[0.0, 0.0]] | |
| left, singular_values, _ = np.linalg.svd(centered, full_matrices=False) | |
| component_count = min(2, len(singular_values)) | |
| coordinates = left[:, :component_count] * singular_values[:component_count] | |
| if component_count == 1: | |
| coordinates = np.column_stack([coordinates, np.zeros(values.shape[0])]) | |
| return coordinates.tolist() | |