46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
from fastapi import APIRouter, UploadFile, File, Form
|
|
from repository import UserRepository, CSVFileRepository, H5ModelRepository, ModelStatisticsRepository
|
|
from schemas import UserCreate, ModelStatisticsCreate
|
|
import shutil
|
|
import os
|
|
|
|
router = APIRouter()
|
|
|
|
UPLOAD_FOLDER_CSV = "uploads/csv"
|
|
os.makedirs(UPLOAD_FOLDER_CSV, exist_ok=True)
|
|
|
|
UPLOAD_FOLDER_MODELS = "uploads/models"
|
|
os.makedirs(UPLOAD_FOLDER_MODELS, exist_ok=True)
|
|
|
|
# Регистрация пользователя
|
|
@router.post("/users/")
|
|
async def create_user(user: UserCreate):
|
|
user_id = await UserRepository.create_user(user)
|
|
return {"user_id": user_id}
|
|
|
|
# Загрузка CSV файла
|
|
@router.post("/upload/csv/")
|
|
async def upload_csv(user_id: int = Form(...), file: UploadFile = File(...)):
|
|
file_path = os.path.join(UPLOAD_FOLDER_CSV, file.filename)
|
|
with open(file_path, "wb") as buffer:
|
|
shutil.copyfileobj(file.file, buffer)
|
|
|
|
await CSVFileRepository.upload_file(user_id=user_id, file_path=file_path)
|
|
return {"message": "CSV файл загружен", "file_path": file_path}
|
|
|
|
# Загрузка H5 модели
|
|
@router.post("/upload/h5/")
|
|
async def upload_h5_model(user_id: int = Form(...), file: UploadFile = File(...)):
|
|
file_path = os.path.join(UPLOAD_FOLDER_MODELS, file.filename)
|
|
with open(file_path, "wb") as buffer:
|
|
shutil.copyfileobj(file.file, buffer)
|
|
|
|
await H5ModelRepository.add_model(user_id=user_id, model_path=file_path)
|
|
return {"message": "H5 модель загружена", "file_path": file_path}
|
|
|
|
# Добавление статистики модели
|
|
@router.post("/models/statistics/")
|
|
async def add_model_statistics(stats: ModelStatisticsCreate):
|
|
await ModelStatisticsRepository.add_statistics(stats)
|
|
return {"message": "Статистика модели сохранена"}
|