Zero GPU spaces will cause an error if the spaces library is not imported first. (This has been made even stricter by a recent specification change.)
In addition, at least one function with the @spaces decorator must be present or an error will occur as well.
Dummy functions are fine.
import spaces
import os
import gradio as gr
from huggingface_hub import HfApi
from gradio_client.exceptions import AuthenticationError
@spaces.GPU
def dummy(): # just a dummy
pass
def load_space():
# Try to get the token from environment variable
hf_token = os.environ.get('api')
if not hf_token:
print("Warning: HUGGINGFACE_TOKEN environment variable not set.")
print("Attempting to use Hugging Face CLI login...")
try:
# Attempt to load the space
iface = gr.load(
name="My space",
hf_token=hf_token,
src="spaces"
)
print("Space loaded successfully!")
return iface
except AuthenticationError as e:
print(f"Authentication error: {e}")
print("Please make sure you're logged in or using a valid token.")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None
def main():
iface = load_space()
if iface:
iface.queue(max_size=300)
iface.launch(debug=False)
else:
print("Failed to load the space. Please check the error messages above.")
if __name__ == "__main__":
main()