File size: 3,031 Bytes
1d21f23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5ad63f
 
 
 
 
 
1d21f23
 
 
 
2547144
1d21f23
6a2811f
 
 
 
 
 
 
 
 
 
 
 
 
 
40a0639
 
94a2d06
 
34d21a4
94a2d06
62f25ac
b26638e
94a2d06
 
 
 
 
40a0639
e5ad63f
1d21f23
e5ad63f
 
1d21f23
2547144
1d21f23
 
e5ad63f
 
2547144
 
 
e5ad63f
 
2547144
e5ad63f
 
 
 
1d21f23
 
 
2547144
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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()