| |
| """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: |
| |
| 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 |
|
|
| |
| data_dir = Path("/app/data") |
| data_dir.mkdir(exist_ok=True) |
| print(f" โ
Data directory: {data_dir}") |
|
|
| |
| db_path = "/app/data/test.db" |
| async with aiosqlite.connect(db_path) as db: |
| await db.execute("SELECT 1") |
| print(f" โ
Database connection: successful") |
|
|
| |
| 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...") |
|
|
| |
| 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: |
| |
| from src.main import app |
| print(" โ
Main app imported successfully") |
|
|
| |
| 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()) |