#!/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()