Tonyivan commited on
Commit
3e23390
·
verified ·
1 Parent(s): ebb0019

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
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: Quantize embeddings to chosen precision (e.g., uint8)
 
 
 
 
 
95
  if precision == 'uint8':
96
- quantized_embeddings = np.array(embeddings, dtype='float32').astype('uint8')
97
  elif precision == 'uint16':
98
- quantized_embeddings = np.array(embeddings, dtype='float32').astype('uint16')
99
  else:
100
  raise ValueError("Unsupported precision. Use 'uint8' or 'uint16'.")
101
 
102
- return quantized_embeddings
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