thienphuc12339/SignLanguage
0
1from fastapi import FastAPI, File, UploadFile, HTTPException2from fastapi.responses import JSONResponse3from pathlib import Path4import shutil5import logging6from inference import inference, get_args7from utils import config_logger8from tools import load_pipeline9from configs import ModelConfig, InferenceConfig10 11app = FastAPI()12 13@app.post("/upload-video/")14async def upload_video(file: UploadFile = File(...)):15 if not file.filename.endswith(('.mp4', '.avi', '.mov', '.mkv')):16 raise HTTPException(status_code=400, detail="Invalid file type. Only video files are allowed.")17 18 # Save the uploaded file to a temporary location19 temp_file_path = Path(f"temp_{file.filename}")20 with temp_file_path.open("wb") as buffer:21 shutil.copyfileobj(file.file, buffer)22 23 # Load configurations24 args = get_args()25 model_config = args.model26 inference_config = args.inference27 28 # Update the source to the uploaded file29 inference_config.source = temp_file_path30 31 # Configure logger32 config_logger(inference_config.output_dir / "inference.log")33 34 # Load the pipeline35 pipeline = load_pipeline(model_config, inference_config)36 37 # Run inference38 try:39 inference(model_config, inference_config, pipeline)40 except Exception as e:41 logging.error(f"Error during inference: {str(e)}")42 raise HTTPException(status_code=500, detail="Error during video processing")43 44 # Clean up the temporary file45 temp_file_path.unlink()46 47 return JSONResponse(content={"message": "Video processed successfully"})48 49if __name__ == "__main__":50 import uvicorn51 uvicorn.run(app, host="0.0.0.0", port=8000)