|
|
|
|
|
"""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()) |