dragonllm-finance-models / update_hf_space.py
jeanbaptdzd's picture
feat: Clean deployment to HuggingFace Space with model config test endpoint
8c0b652
#!/usr/bin/env python3
"""
Update HuggingFace Space using the API.
This script will trigger a rebuild of the Space with the latest code.
"""
import os
import requests
import json
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def update_huggingface_space():
"""Update the HuggingFace Space to trigger a rebuild."""
# HuggingFace Space details
space_id = "jeanbaptdzd/linguacustodia-financial-api"
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
print("❌ HF_TOKEN not found in environment variables")
print("Please set HF_TOKEN in your .env file")
return False
print(f"πŸš€ Updating HuggingFace Space: {space_id}")
# HuggingFace API endpoint for restarting a Space
url = f"https://huggingface.co/api/spaces/{space_id}/restart"
headers = {
"Authorization": f"Bearer {hf_token}",
"Content-Type": "application/json"
}
try:
print("πŸ“‘ Sending restart request to HuggingFace API...")
response = requests.post(url, headers=headers, timeout=30)
if response.status_code == 200:
print("βœ… Space restart triggered successfully!")
print(f"🌐 Space URL: https://huggingface.co/spaces/{space_id}")
print("⏳ The Space will rebuild with the latest code from the repository")
print("πŸ“ You can monitor the build progress in the HuggingFace UI")
return True
else:
print(f"❌ Failed to restart Space: {response.status_code}")
print(f"Response: {response.text}")
return False
except Exception as e:
print(f"❌ Error updating Space: {e}")
return False
def test_space_endpoint():
"""Test the Space endpoint to verify it's working."""
space_id = "jeanbaptdzd/linguacustodia-financial-api"
test_url = f"https://huggingface.co/spaces/{space_id}/test/model-configs"
print(f"πŸ§ͺ Testing Space endpoint: {test_url}")
try:
response = requests.get(test_url, timeout=30)
if response.status_code == 200:
print("βœ… Space is responding!")
data = response.json()
print("\nπŸ“Š Model Configuration Test Results:")
print("=" * 50)
for model_name, result in data.get("test_results", {}).items():
context_length = result.get("context_length")
if context_length:
print(f"βœ… {model_name}: {context_length:,} tokens")
else:
print(f"❌ {model_name}: Unknown context length")
print("\n🎯 Expected vs Actual:")
print("=" * 50)
expected = data.get("expected_contexts", {})
for model_name, expected_length in expected.items():
actual = data.get("test_results", {}).get(model_name, {}).get("context_length")
if actual:
if actual == expected_length:
print(f"βœ… {model_name}: {actual:,} tokens (CORRECT)")
else:
print(f"❌ {model_name}: {actual:,} tokens (EXPECTED {expected_length:,})")
else:
print(f"⚠️ {model_name}: Unknown (EXPECTED {expected_length:,})")
return True
else:
print(f"❌ Space not responding: {response.status_code}")
return False
except Exception as e:
print(f"❌ Error testing Space: {e}")
return False
def main():
"""Main function."""
print("πŸš€ HuggingFace Space Updater")
print("=" * 40)
# Update the Space
if update_huggingface_space():
print("\n⏳ Waiting for Space to rebuild...")
print("πŸ’‘ This may take a few minutes. You can check the progress at:")
print(" https://huggingface.co/spaces/jeanbaptdzd/linguacustodia-financial-api")
# Wait a bit and then test
import time
print("\n⏳ Waiting 30 seconds before testing...")
time.sleep(30)
# Test the endpoint
print("\nπŸ§ͺ Testing the updated Space...")
test_space_endpoint()
else:
print("❌ Failed to update Space")
if __name__ == "__main__":
main()