Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,512 Bytes
ffa4ae8 970eef1 7e389db 4759fe1 97bea1c 970eef1 97bea1c 970eef1 97bea1c 970eef1 3964afa 970eef1 3964afa 970eef1 3964afa 970eef1 97bea1c 970eef1 4759fe1 3964afa 97bea1c aae1c13 97bea1c aae1c13 97bea1c aae1c13 97bea1c 3964afa 97bea1c 3964afa 4759fe1 aae1c13 970eef1 3964afa 970eef1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
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")
# Enable CORS to allow requests from frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In a production environment, specify exact origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add an event handler to display session_files at startup
@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}")
# Display detailed information about environment variables
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 =====")
# Display other useful variables
for env_var in ["PORT", "DEBUG", "PYTHONPATH", "VIRTUAL_ENV"]:
value = os.environ.get(env_var)
if value:
print(f"ℹ️ {env_var}: {value}")
print("=======================================\n")
# Test models at startup and display results
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")
# Register all routes
for router in routers:
app.include_router(router) |