Spaces:
Running
Running
#!/usr/bin/env python | |
""" | |
AI Creative Studio - Hugging Face Spaces Entry Point | |
""" | |
import os | |
import sys | |
import logging | |
from pathlib import Path | |
# Configure logging | |
logging.basicConfig( | |
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
) | |
logger = logging.getLogger("huggingface-spaces") | |
# Get the absolute path of the current script | |
current_dir = Path(__file__).parent.resolve() | |
# Add the app directory to the path directly, don't try to import as a package | |
sys.path.append(str(current_dir)) | |
logger.info(f"Added {current_dir} to sys.path") | |
# Set environment variables for Spaces | |
os.environ["HTTPX_VERIFY"] = "0" | |
os.environ["UI_PORT"] = "7860" # Standard Spaces port | |
os.environ["HF_SPACES"] = "1" # Flag to indicate we're running in Spaces | |
# Configure persistent data directory for Spaces | |
# Use /home/user/app/data for persistent storage in Hugging Face Spaces | |
persistent_data_dir = "/home/user/app/data" | |
os.environ["DATA_DIR"] = persistent_data_dir # This is critical for gallery to work | |
os.environ["IMAGE_OUTPUT_DIR"] = f"{persistent_data_dir}/images" | |
os.environ["MODEL_OUTPUT_DIR"] = f"{persistent_data_dir}/models" | |
logger.info(f"Set persistent data directory to {persistent_data_dir}") | |
# Create directories if they don't exist | |
for subdir in ["", "images", "models", "downloads"]: | |
dir_path = f"{persistent_data_dir}/{subdir}" if subdir else persistent_data_dir | |
os.makedirs(dir_path, exist_ok=True) | |
logger.info(f"Ensured directory exists: {dir_path}") | |
# Set model environment variables explicitly for Hugging Face Spaces | |
# These will override any variables loaded from .env.spaces | |
os.environ["MODEL_ID"] = ( | |
"TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF" # TinyLlama chat model | |
) | |
os.environ["MODEL_FILENAME"] = ( | |
"tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf" # Correct model filename | |
) | |
os.environ["USE_LOCAL_MODEL"] = "true" | |
os.environ["MODEL_TYPE"] = "gguf" # Use ctransformers | |
os.environ["MODEL_QUANTIZED"] = "false" # We're using a pre-quantized GGUF model | |
os.environ["MODEL_ARCHITECTURE"] = "causal" # Llama is a causal language model | |
os.environ["MODEL_CPU_THREADS"] = "2" # Control CPU usage in Spaces | |
# Import UI module directly | |
try: | |
# Import main function directly from the UI module using direct path import | |
sys.path.append(str(current_dir / "app" / "ui")) | |
from app.ui.app import main as ui_main | |
logger.info("Successfully imported UI module") | |
except ImportError as e: | |
# Try alternative import approach if first one fails | |
try: | |
logger.warning( | |
f"First import attempt failed: {str(e)}, trying alternative approach" | |
) | |
sys.path.append(str(current_dir / "app")) | |
from ui.app import main as ui_main | |
logger.info("Successfully imported UI module using alternative approach") | |
except ImportError as e2: | |
logger.error(f"Failed to import UI module: {str(e2)}") | |
raise | |
if __name__ == "__main__": | |
logger.info("Starting AI Creative Studio on Hugging Face Spaces") | |
ui_main() | |