Spaces:
Runtime error
Runtime error
| #!/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() | |