Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Huggingface Spaces entry point for MoneyPrinterTurbo | |
""" | |
import os | |
import sys | |
import subprocess | |
import time | |
# Add the root directory to Python path | |
root_dir = os.path.dirname(os.path.abspath(__file__)) | |
sys.path.insert(0, root_dir) | |
def setup_environment(): | |
"""Setup environment for Huggingface Spaces""" | |
print("π Creating storage directories...") | |
# Create necessary directories | |
os.makedirs(os.path.join(root_dir, "storage", "tasks"), exist_ok=True) | |
os.makedirs(os.path.join(root_dir, "storage", "cache_videos"), exist_ok=True) | |
os.makedirs(os.path.join(root_dir, "storage", "temp"), exist_ok=True) | |
# Set environment variables for Huggingface Spaces | |
os.environ["STREAMLIT_SERVER_PORT"] = "7860" | |
os.environ["STREAMLIT_SERVER_ADDRESS"] = "0.0.0.0" | |
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false" | |
os.environ["STREAMLIT_SERVER_ENABLE_CORS"] = "true" | |
print("π§ Setting up configuration...") | |
# Load API keys from environment variables if available | |
setup_api_keys_from_env() | |
def setup_api_keys_from_env(): | |
"""Setup API keys from environment variables""" | |
try: | |
from app.config import config | |
keys_loaded = [] | |
# MoneyPrinterTurbo API Key for external access | |
if os.getenv("MONEYPRINTER_API_KEY"): | |
config.app["api_key"] = os.getenv("MONEYPRINTER_API_KEY") | |
config.app["api_enabled"] = True | |
keys_loaded.append("MoneyPrinter API") | |
# LLM Provider API Keys | |
if os.getenv("OPENAI_API_KEY"): | |
config.app["openai_api_key"] = os.getenv("OPENAI_API_KEY") | |
keys_loaded.append("OpenAI") | |
if os.getenv("DEEPSEEK_API_KEY"): | |
config.app["deepseek_api_key"] = os.getenv("DEEPSEEK_API_KEY") | |
keys_loaded.append("DeepSeek") | |
if os.getenv("MOONSHOT_API_KEY"): | |
config.app["moonshot_api_key"] = os.getenv("MOONSHOT_API_KEY") | |
keys_loaded.append("Moonshot") | |
# Video Source API Keys | |
if os.getenv("PEXELS_API_KEY"): | |
config.app["pexels_api_keys"] = [os.getenv("PEXELS_API_KEY")] | |
keys_loaded.append("Pexels") | |
if os.getenv("PIXABAY_API_KEY"): | |
config.app["pixabay_api_keys"] = [os.getenv("PIXABAY_API_KEY")] | |
keys_loaded.append("Pixabay") | |
# Azure Speech API | |
if os.getenv("AZURE_SPEECH_KEY"): | |
config.azure["speech_key"] = os.getenv("AZURE_SPEECH_KEY") | |
keys_loaded.append("Azure Speech") | |
if os.getenv("AZURE_SPEECH_REGION"): | |
config.azure["speech_region"] = os.getenv("AZURE_SPEECH_REGION") | |
# Set default LLM provider based on available keys | |
if os.getenv("DEEPSEEK_API_KEY"): | |
config.app["llm_provider"] = "deepseek" | |
print("π€ Default LLM provider: DeepSeek") | |
elif os.getenv("MOONSHOT_API_KEY"): | |
config.app["llm_provider"] = "moonshot" | |
print("π€ Default LLM provider: Moonshot") | |
elif os.getenv("OPENAI_API_KEY"): | |
config.app["llm_provider"] = "openai" | |
print("π€ Default LLM provider: OpenAI") | |
if keys_loaded: | |
print(f"β API keys configured: {len(keys_loaded)} service(s)") | |
else: | |
print("π No API keys found in environment variables") | |
print("π‘ You can configure them in the WebUI after startup") | |
config.save_config() | |
except Exception as e: | |
print(f"β οΈ Warning: Could not load API keys from environment: {e}") | |
def check_system_dependencies(): | |
"""Check if required system dependencies are available""" | |
print("π Checking system dependencies...") | |
# Check for ffmpeg | |
try: | |
subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True) | |
print("β FFmpeg is available") | |
except (subprocess.CalledProcessError, FileNotFoundError): | |
print("β οΈ FFmpeg not found - video processing may fail") | |
# Check Python version | |
if sys.version_info >= (3, 8): | |
print(f"β Python {sys.version_info.major}.{sys.version_info.minor} is compatible") | |
else: | |
print(f"β οΈ Python {sys.version_info.major}.{sys.version_info.minor} may have compatibility issues") | |
def start_streamlit(): | |
"""Start Streamlit app with proper error handling""" | |
print("π Starting Streamlit application...") | |
streamlit_cmd = [ | |
sys.executable, "-m", "streamlit", "run", | |
os.path.join(root_dir, "webui", "Main.py"), | |
"--server.port=7860", | |
"--server.address=0.0.0.0", | |
"--browser.gatherUsageStats=false", | |
"--server.enableCORS=true", | |
"--theme.base=light", | |
"--server.enableWebsocketCompression=false" | |
] | |
try: | |
# Use exec to replace the current process | |
os.execvp(sys.executable, streamlit_cmd) | |
except Exception as e: | |
print(f"β Failed to start Streamlit: {e}") | |
# Fallback to subprocess | |
subprocess.run(streamlit_cmd) | |
if __name__ == "__main__": | |
print("=" * 50) | |
print("π MoneyPrinterTurbo Starting on Huggingface Spaces") | |
print("=" * 50) | |
# Check for environment variables without exposing which ones | |
env_check_keys = ["OPENAI_API_KEY", "DEEPSEEK_API_KEY", "MOONSHOT_API_KEY", | |
"PEXELS_API_KEY", "PIXABAY_API_KEY"] | |
env_keys_count = sum(1 for key in env_check_keys if os.getenv(key)) | |
if env_keys_count > 0: | |
print(f"π Found {env_keys_count} API key(s) in environment") | |
else: | |
print("π No API keys found in environment variables") | |
print("π Required: LLM Provider API Key + Video Source API Key") | |
# Setup environment | |
setup_environment() | |
# Check system dependencies | |
check_system_dependencies() | |
# Small delay to ensure everything is set up | |
time.sleep(2) | |
# Start the application | |
start_streamlit() |