from fastapi import FastAPI, UploadFile, File, Form, Request, HTTPException from fastapi.middleware.cors import CORSMiddleware import os from dotenv import load_dotenv from routes import routers, session_files, active_tasks, benchmark from tasks.get_available_model_provider import test_models from datetime import datetime # Load environment variables from .env file load_dotenv() # Verify environment variables are loaded hf_token = os.getenv("HF_TOKEN") if not hf_token: print("⚠️ WARNING: HF_TOKEN environment variable is not set.") else: print("ℹ️ HF_TOKEN found in environment variables") hf_organization = os.getenv("HF_ORGANIZATION") if not hf_organization: print("⚠️ WARNING: HF_ORGANIZATION environment variable is not set.") else: print(f"ℹ️ HF_ORGANIZATION found: {hf_organization}") app = FastAPI(title="Yourbench API") # Activer CORS pour permettre les requêtes depuis le frontend app.add_middleware( CORSMiddleware, allow_origins=["*"], # Dans un environnement de production, spécifiez les origines exactes allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Ajouter un gestionnaire d'événements pour afficher les session_files au démarrage @app.on_event("startup") async def startup_event(): print("\n===== Application Startup at", datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "=====\n") print(f"Initial session_files: {session_files}") # Afficher des informations détaillées sur les variables d'environnement print("\n===== Environment Variables Check =====") hf_token = os.environ.get("HF_TOKEN") if hf_token: print("✅ HF_TOKEN AVAILABLE") # Basic format validation if not hf_token.startswith("hf_"): print("⚠️ WARNING: Your HF_TOKEN does not start with 'hf_' which is unusual.") print(" Please verify its format and source.") else: print("❌ HF_TOKEN MISSING - HuggingFace models will not work correctly") print(" Please set this environment variable for proper functionality.") hf_organization = os.environ.get("HF_ORGANIZATION") if hf_organization: print(f"✅ HF_ORGANIZATION: {hf_organization}") else: print("❌ HF_ORGANIZATION MISSING") print(" This may affect billing and access to certain models.") print("\n===== Additional Environment Variables =====") # Afficher d'autres variables utiles for env_var in ["PORT", "DEBUG", "PYTHONPATH", "VIRTUAL_ENV"]: value = os.environ.get(env_var) if value: print(f"ℹ️ {env_var}: {value}") print("=======================================\n") # Tester les modèles au démarrage et afficher les résultats print("===== Testing model availability at startup =====") test_results = test_models(verbose=True) print("===== Model testing completed =====") if test_results["working_model"]: print(f"✅ Found working model: {test_results['working_model']} with provider: {test_results['provider']}") else: print("❌ WARNING: No working models found. The application might not function correctly!") print("\nPossible solutions:") print("1. Check your HF_TOKEN is valid and has appropriate permissions") print("2. Verify your internet connection") print("3. Try again later as the API service might be temporarily unavailable") print("4. Configure alternative models in config/models_config.py") # Enregistrer toutes les routes for router in routers: app.include_router(router)