Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
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 | |
# 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. Make sure it's defined in your .env file.") | |
hf_organization = os.getenv("HF_ORGANIZATION") | |
if not hf_organization: | |
print("Warning: HF_ORGANIZATION environment variable is not set. Make sure it's defined in your .env file.") | |
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 | |
async def startup_event(): | |
print("Application startup") | |
print(f"Initial session_files: {session_files}") | |
# 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!") | |
# Enregistrer toutes les routes | |
for router in routers: | |
app.include_router(router) |