#!/usr/bin/env python3 """Simple startup script for debugging Flow2API on HuggingFace Spaces""" import os import sys import time import asyncio from pathlib import Path def debug_environment(): """Print environment debug information""" print("=" * 60) print("🔍 FLOW2API DEBUG STARTUP") print("=" * 60) print(f"Python version: {sys.version}") print(f"Current working directory: {os.getcwd()}") print(f"User ID: {os.getuid()}") print(f"Group ID: {os.getgid()}") print(f"Environment variables:") for key, value in sorted(os.environ.items()): if key.startswith('FLOW2API') or key in ['HOST', 'PORT', 'HOME', 'PATH']: print(f" {key}: {value}") print("=" * 60) def check_file_permissions(): """Check critical file permissions""" files_to_check = [ '/app', '/app/logs.txt', '/app/data', '/app/config' ] print("📁 FILE PERMISSIONS CHECK:") for file_path in files_to_check: path = Path(file_path) if path.exists(): stat_info = path.stat() print(f" ✅ {file_path}: exists, mode={oct(stat_info.st_mode)}") if path.is_file(): try: # Test write permission with open(path, 'a') as f: f.write("") print(f" ✅ {file_path}: writable") except Exception as e: print(f" ❌ {file_path}: NOT writable - {e}") else: print(f" ❌ {file_path}: does not exist") print("=" * 60) async def test_basic_import(): """Test if basic modules can be imported""" print("📦 IMPORT TEST:") try: import fastapi print(f" ✅ FastAPI: {fastapi.__version__}") except Exception as e: print(f" ❌ FastAPI import failed: {e}") try: import uvicorn print(f" ✅ Uvicorn: available") except Exception as e: print(f" ❌ Uvicorn import failed: {e}") try: import aiosqlite print(f" ✅ aiosqlite: available") except Exception as e: print(f" ❌ aiosqlite import failed: {e}") print("=" * 60) async def test_database_connection(): """Test database creation and connection""" print("💾 DATABASE TEST:") try: import aiosqlite # Test database directory creation data_dir = Path("/app/data") data_dir.mkdir(exist_ok=True) print(f" ✅ Data directory: {data_dir}") # Test database connection db_path = "/app/data/test.db" async with aiosqlite.connect(db_path) as db: await db.execute("SELECT 1") print(f" ✅ Database connection: successful") # Clean up test file os.remove(db_path) print(f" ✅ Test database: cleaned up") except Exception as e: print(f" ❌ Database test failed: {e}") import traceback traceback.print_exc() print("=" * 60) def start_simple_server(): """Start a simple test server""" print("🌐 SIMPLE SERVER TEST:") try: from fastapi import FastAPI import uvicorn app = FastAPI() @app.get("/") async def root(): return {"message": "Flow2API is running", "status": "ok"} @app.get("/health") async def health(): return {"status": "healthy"} print(" ✅ Simple FastAPI app created") print(" 🚀 Starting simple server on port 7860...") # Start server uvicorn.run( app, host="0.0.0.0", port=7860, log_level="info" ) except Exception as e: print(f" ❌ Simple server failed: {e}") import traceback traceback.print_exc() async def main(): """Main debugging function""" debug_environment() check_file_permissions() await test_basic_import() await test_database_connection() print("🎯 STARTING FLOW2API:") try: # Import and start the main application from src.main import app print(" ✅ Main app imported successfully") # Start with uvicorn directly import uvicorn print(" 🚀 Starting main application...") uvicorn.run( app, host="0.0.0.0", port=7860, log_level="info", access_log=True ) except Exception as e: print(f" ❌ Main app startup failed: {e}") import traceback traceback.print_exc() print("🔄 FALLING BACK TO SIMPLE SERVER...") start_simple_server() if __name__ == "__main__": print("Starting Flow2API with debug mode...") asyncio.run(main())