Spaces:
Sleeping
Sleeping
logging
Browse files- app/main.py +27 -1
app/main.py
CHANGED
|
@@ -3,26 +3,47 @@ from fastapi.templating import Jinja2Templates
|
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from optimum.neuron import utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Mount static files and templates
|
| 10 |
-
app.mount("/static", StaticFiles(directory=
|
| 11 |
templates = Jinja2Templates(directory="app/templates")
|
| 12 |
|
| 13 |
@app.get("/health")
|
| 14 |
async def health_check():
|
|
|
|
| 15 |
return {"status": "healthy"}
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
async def home(request: Request):
|
|
|
|
| 19 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 20 |
|
| 21 |
@app.get("/api/models")
|
| 22 |
async def get_model_list():
|
|
|
|
| 23 |
try:
|
| 24 |
# Get actual model configurations
|
| 25 |
model_list = utils.get_hub_cached_models(mode="inference")
|
|
|
|
| 26 |
|
| 27 |
# Transform the data into the expected format
|
| 28 |
models = []
|
|
@@ -40,8 +61,10 @@ async def get_model_list():
|
|
| 40 |
})
|
| 41 |
seen_models.add(full_model_id)
|
| 42 |
|
|
|
|
| 43 |
return JSONResponse(content=models)
|
| 44 |
except Exception as e:
|
|
|
|
| 45 |
return JSONResponse(
|
| 46 |
status_code=500,
|
| 47 |
content={"error": str(e)}
|
|
@@ -49,13 +72,16 @@ async def get_model_list():
|
|
| 49 |
|
| 50 |
@app.get("/api/models/{model_id}")
|
| 51 |
async def get_model_info_endpoint(model_id: str):
|
|
|
|
| 52 |
try:
|
| 53 |
configs = utils.get_hub_cached_entries(model_id=model_id, mode="inference")
|
|
|
|
| 54 |
# Return empty list if no configurations found
|
| 55 |
if not configs:
|
| 56 |
return JSONResponse(content={"configurations": []})
|
| 57 |
return JSONResponse(content={"configurations": configs})
|
| 58 |
except Exception as e:
|
|
|
|
| 59 |
return JSONResponse(
|
| 60 |
status_code=500,
|
| 61 |
content={"error": str(e)}
|
|
|
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from optimum.neuron import utils
|
| 6 |
+
import logging
|
| 7 |
+
import sys
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Configure logging
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.INFO,
|
| 13 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 14 |
+
handlers=[
|
| 15 |
+
logging.StreamHandler(sys.stdout)
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
app = FastAPI()
|
| 21 |
|
| 22 |
+
# Get the absolute path to the static directory
|
| 23 |
+
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
|
| 24 |
+
logger.info(f"Static directory path: {static_dir}")
|
| 25 |
+
|
| 26 |
# Mount static files and templates
|
| 27 |
+
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
| 28 |
templates = Jinja2Templates(directory="app/templates")
|
| 29 |
|
| 30 |
@app.get("/health")
|
| 31 |
async def health_check():
|
| 32 |
+
logger.info("Health check endpoint called")
|
| 33 |
return {"status": "healthy"}
|
| 34 |
|
| 35 |
@app.get("/")
|
| 36 |
async def home(request: Request):
|
| 37 |
+
logger.info("Home page requested")
|
| 38 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 39 |
|
| 40 |
@app.get("/api/models")
|
| 41 |
async def get_model_list():
|
| 42 |
+
logger.info("Fetching model list")
|
| 43 |
try:
|
| 44 |
# Get actual model configurations
|
| 45 |
model_list = utils.get_hub_cached_models(mode="inference")
|
| 46 |
+
logger.info(f"Found {len(model_list)} models")
|
| 47 |
|
| 48 |
# Transform the data into the expected format
|
| 49 |
models = []
|
|
|
|
| 61 |
})
|
| 62 |
seen_models.add(full_model_id)
|
| 63 |
|
| 64 |
+
logger.info(f"Returning {len(models)} unique models")
|
| 65 |
return JSONResponse(content=models)
|
| 66 |
except Exception as e:
|
| 67 |
+
logger.error(f"Error fetching models: {str(e)}", exc_info=True)
|
| 68 |
return JSONResponse(
|
| 69 |
status_code=500,
|
| 70 |
content={"error": str(e)}
|
|
|
|
| 72 |
|
| 73 |
@app.get("/api/models/{model_id}")
|
| 74 |
async def get_model_info_endpoint(model_id: str):
|
| 75 |
+
logger.info(f"Fetching configurations for model: {model_id}")
|
| 76 |
try:
|
| 77 |
configs = utils.get_hub_cached_entries(model_id=model_id, mode="inference")
|
| 78 |
+
logger.info(f"Found {len(configs)} configurations for model {model_id}")
|
| 79 |
# Return empty list if no configurations found
|
| 80 |
if not configs:
|
| 81 |
return JSONResponse(content={"configurations": []})
|
| 82 |
return JSONResponse(content={"configurations": configs})
|
| 83 |
except Exception as e:
|
| 84 |
+
logger.error(f"Error fetching configurations for model {model_id}: {str(e)}", exc_info=True)
|
| 85 |
return JSONResponse(
|
| 86 |
status_code=500,
|
| 87 |
content={"error": str(e)}
|