Update app.py
Browse files
app.py
CHANGED
@@ -91,15 +91,20 @@ def optimize_embedding(texts, precision='uint8'):
|
|
91 |
# Step 1: Generate embeddings with 384 dimensions
|
92 |
embeddings = model.encode(texts)
|
93 |
|
94 |
-
# Step 2:
|
|
|
|
|
|
|
|
|
|
|
95 |
if precision == 'uint8':
|
96 |
-
|
97 |
elif precision == 'uint16':
|
98 |
-
|
99 |
else:
|
100 |
raise ValueError("Unsupported precision. Use 'uint8' or 'uint16'.")
|
101 |
|
102 |
-
return
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
import uvicorn
|
|
|
91 |
# Step 1: Generate embeddings with 384 dimensions
|
92 |
embeddings = model.encode(texts)
|
93 |
|
94 |
+
# Step 2: Normalize embeddings to [0, 1] range
|
95 |
+
embeddings_min = embeddings.min(axis=1, keepdims=True)
|
96 |
+
embeddings_max = embeddings.max(axis=1, keepdims=True)
|
97 |
+
normalized_embeddings = (embeddings - embeddings_min) / (embeddings_max - embeddings_min + 1e-8)
|
98 |
+
|
99 |
+
# Step 3: Scale normalized embeddings to fit within the range of uint8 or uint16
|
100 |
if precision == 'uint8':
|
101 |
+
scaled_embeddings = (normalized_embeddings * 255).astype('uint8')
|
102 |
elif precision == 'uint16':
|
103 |
+
scaled_embeddings = (normalized_embeddings * 65535).astype('uint16')
|
104 |
else:
|
105 |
raise ValueError("Unsupported precision. Use 'uint8' or 'uint16'.")
|
106 |
|
107 |
+
return scaled_embeddings
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
import uvicorn
|