Spaces:
Sleeping
Sleeping
Download app/embedding.py from jonathanjordan21/Test-ticketing: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/spaces/jonathanjordan21/Test-ticketing/resolve/main/app/embedding.py
- Command line
-
hf download hf://spaces/jonathanjordan21/Test-ticketing/app/embedding.py
-
curl -L -o embedding.py https://huggingface.co/spaces/jonathanjordan21/Test-ticketing/resolve/main/app/embedding.py
2.48 kB
| import os | |
| import joblib | |
| import numpy as np | |
| from app.models import models | |
| PCA_PATH = os.getenv( | |
| "PCA_PATH", | |
| "artifacts/embedding_pca.joblib" | |
| ) | |
| def generate_embeddings(request): | |
| model = models.embedding() | |
| vectors = model.encode( | |
| request.input, | |
| convert_to_numpy=True, | |
| normalize_embeddings=False, | |
| show_progress_bar=False | |
| ).astype(np.float32) | |
| # PHASE 1: Embedding generation | |
| original_dimensions = vectors.shape[1] | |
| # PHASE 2: Dimensionality reduction | |
| if request.dimensions: | |
| if request.dimensions > original_dimensions: | |
| raise ValueError("Requested dimensions exceed model output") | |
| if request.dimensions < original_dimensions: | |
| if not os.path.exists(PCA_PATH): | |
| raise ValueError( | |
| "PCA artifact not configured. " | |
| "Train and deploy PCA before reducing dimensions." | |
| ) | |
| pca = joblib.load(PCA_PATH) | |
| if pca.n_features_in_ != original_dimensions: | |
| raise ValueError("PCA input dimensions mismatch") | |
| if pca.n_components_ != request.dimensions: | |
| raise ValueError("PCA output dimensions mismatch") | |
| vectors = pca.transform(vectors) | |
| # Optional normalization after projection | |
| if request.normalize: | |
| norms = np.linalg.norm( | |
| vectors, | |
| axis=1, | |
| keepdims=True | |
| ) | |
| vectors = vectors / np.maximum(norms, 1e-12) | |
| # PHASE 3: Precision quantization | |
| scales = None | |
| if request.precision == "float32": | |
| output = vectors.astype(np.float32).tolist() | |
| elif request.precision == "float16": | |
| output = vectors.astype(np.float16).tolist() | |
| elif request.precision == "int8": | |
| # Symmetric per-vector quantization | |
| max_values = np.max( | |
| np.abs(vectors), | |
| axis=1, | |
| keepdims=True | |
| ) | |
| scale_array = np.maximum( | |
| max_values / 127.0, | |
| 1e-12 | |
| ) | |
| quantized = np.clip( | |
| np.round(vectors / scale_array), | |
| -127, | |
| 127 | |
| ).astype(np.int8) | |
| output = quantized.tolist() | |
| scales = scale_array.flatten().tolist() | |
| return { | |
| "dimensions": vectors.shape[1], | |
| "precision": request.precision, | |
| "embeddings": output, | |
| "scales": scales, | |
| "original_dimensions": original_dimensions | |
| } | |