Upload backend/hue_portal/hue-portal-backendDocker/set_embedding_model_1024.py with huggingface_hub
Browse files
backend/hue_portal/hue-portal-backendDocker/set_embedding_model_1024.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Script to set embedding model to 1024 dim (multilingual-e5-large) on Hugging Face Spaces.
|
| 4 |
+
This fixes the dimension mismatch: query=768, stored=1024.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
from huggingface_hub import HfApi
|
| 9 |
+
from huggingface_hub.utils import HfFolder
|
| 10 |
+
|
| 11 |
+
def get_hf_token():
|
| 12 |
+
"""Get Hugging Face token from cache."""
|
| 13 |
+
token = HfFolder.get_token()
|
| 14 |
+
if not token:
|
| 15 |
+
print("❌ No Hugging Face token found!")
|
| 16 |
+
print("💡 Run: huggingface-cli login")
|
| 17 |
+
return None
|
| 18 |
+
return token
|
| 19 |
+
|
| 20 |
+
def set_secret(api, repo_id, key, value):
|
| 21 |
+
"""Set a secret on Hugging Face Space."""
|
| 22 |
+
try:
|
| 23 |
+
# Delete existing secret first
|
| 24 |
+
try:
|
| 25 |
+
api.delete_space_secret(repo_id=repo_id, key=key, token=api.token)
|
| 26 |
+
print(f"🗑️ Deleted existing secret: {key}")
|
| 27 |
+
except Exception:
|
| 28 |
+
pass # Secret doesn't exist, continue
|
| 29 |
+
|
| 30 |
+
# Add new secret
|
| 31 |
+
api.add_space_secret(repo_id=repo_id, key=key, value=value, token=api.token)
|
| 32 |
+
print(f"✅ Set secret: {key}={value}")
|
| 33 |
+
return True
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"❌ Error setting secret {key}: {e}")
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
def main():
|
| 39 |
+
repo_id = "davidtran999/hue-portal-backend"
|
| 40 |
+
|
| 41 |
+
print("🔧 Setting embedding model to 1024 dim (multilingual-e5-large)")
|
| 42 |
+
print(f"📦 Repository: {repo_id}\n")
|
| 43 |
+
|
| 44 |
+
# Get token
|
| 45 |
+
token = get_hf_token()
|
| 46 |
+
if not token:
|
| 47 |
+
return
|
| 48 |
+
|
| 49 |
+
api = HfApi(token=token)
|
| 50 |
+
|
| 51 |
+
# Set embedding model to multilingual-e5-large (1024 dim)
|
| 52 |
+
success = set_secret(api, repo_id, "EMBEDDING_MODEL", "multilingual-e5-large")
|
| 53 |
+
|
| 54 |
+
if success:
|
| 55 |
+
print("\n✅ Successfully set embedding model to multilingual-e5-large (1024 dim)")
|
| 56 |
+
print("\n📊 This will fix dimension mismatch:")
|
| 57 |
+
print(" - Before: query=768, stored=1024 ❌")
|
| 58 |
+
print(" - After: query=1024, stored=1024 ✅")
|
| 59 |
+
print("\n🔄 Rebuild your HF Space to apply changes!")
|
| 60 |
+
print(" → Vector search will work again after rebuild")
|
| 61 |
+
else:
|
| 62 |
+
print("\n❌ Failed to set embedding model")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|