Spaces:
Sleeping
Sleeping
fix: make web3 a soft/optional import in blockchain_store
Browse filesweb3 is not installed on HF Spaces (no Ethereum needed there).
BlockchainLogger already falls back to local JSON logging when not
connected — now it also loads cleanly when web3 is not importable.
2 unmodified lines345678910116 unmodified lines1271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601612 unmodified linesimport timeimport osfrom typing import Dict, Any, Optional, List, Unionfrom web3 import Web3, HTTPProviderfrom web3.exceptions import TransactionNotFoundimport json# Smart contract ABI (Application Binary Interface)116 unmodified linesself.contract = Noneif self.enable_blockchain:try:print(f"Connecting to Ethereum node at {self.blockchain_endpoint}...")self.web3 = Web3(HTTPProvider(self.blockchain_endpoint))self.connected = self.web3.is_connected()if self.connected:print("Connected to Ethereum node successfully")# Auto-detect wallet address if not specifiedif not self.wallet_address and self.web3.eth.accounts:self.wallet_address = self.web3.eth.accounts[0]print(f"Using wallet address: {self.wallet_address}")# If we have a contract address, initialize the contractif self.contract_address:self.contract = self.web3.eth.contract(address=self.web3.to_checksum_address(self.contract_address),abi=self.abi)print(f"Contract initialized at address: {self.contract_address}")else:print("Warning: No contract address provided. Smart contract verification will be unavailable.")else:print(f"Failed to connect to Ethereum node at {self.blockchain_endpoint}")except Exception as e:print(f"Warning: Failed to connect to Ethereum node: {str(e)}")else:# Silent mode when disabledpass# Always provide fallback in case blockchain is not availableself.local_log_file = "blockchain_log.json"2 unmodified lines34567891011121314151617181920116 unmodified lines1371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741752 unmodified linesimport timeimport osfrom typing import Dict, Any, Optional, List, Union# web3 is only required when ENABLE_BLOCKCHAIN=1. On HF Spaces it is not# installed, so we degrade gracefully to local JSON logging.try:from web3 import Web3, HTTPProviderfrom web3.exceptions import TransactionNotFound_WEB3_AVAILABLE = Trueexcept ImportError:Web3 = None # type: ignoreHTTPProvider = None # type: ignoreTransactionNotFound = Exception # type: ignore_WEB3_AVAILABLE = Falseimport json# Smart contract ABI (Application Binary Interface)116 unmodified linesself.contract = Noneif self.enable_blockchain:if not _WEB3_AVAILABLE:print("Warning: web3 not installed — blockchain logging disabled. Install web3 to enable.")else:try:print(f"Connecting to Ethereum node at {self.blockchain_endpoint}...")self.web3 = Web3(HTTPProvider(self.blockchain_endpoint))self.connected = self.web3.is_connected()if self.connected:print("Connected to Ethereum node successfully")# Auto-detect wallet address if not specifiedif not self.wallet_address and self.web3.eth.accounts:self.wallet_address = self.web3.eth.accounts[0]print(f"Using wallet address: {self.wallet_address}")# If we have a contract address, initialize the contractif self.contract_address:self.contract = self.web3.eth.contract(address=self.web3.to_checksum_address(self.contract_address),abi=self.abi)print(f"Contract initialized at address: {self.contract_address}")else:print("Warning: No contract address provided. Smart contract verification will be unavailable.")else:print(f"Failed to connect to Ethereum node at {self.blockchain_endpoint}")except Exception as e:print(f"Warning: Failed to connect to Ethereum node: {str(e)}")else:# Silent mode when disabledpass# Always provide fallback in case blockchain is not availableself.local_log_file = "blockchain_log.json"
src/verification/blockchain_store.py+41-27