|
@@ -1,6 +1,9 @@
|
|
|
from typing import Optional
|
|
|
import io
|
|
|
import base64
|
|
|
+import json
|
|
|
+import asyncio
|
|
|
+import logging
|
|
|
|
|
|
from open_webui.models.models import (
|
|
|
ModelForm,
|
|
@@ -12,7 +15,14 @@ from open_webui.models.models import (
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
|
|
-from fastapi import APIRouter, Depends, HTTPException, Request, status, Response
|
|
|
+from fastapi import (
|
|
|
+ APIRouter,
|
|
|
+ Depends,
|
|
|
+ HTTPException,
|
|
|
+ Request,
|
|
|
+ status,
|
|
|
+ Response,
|
|
|
+)
|
|
|
from fastapi.responses import FileResponse, StreamingResponse
|
|
|
|
|
|
|
|
@@ -20,6 +30,8 @@ from open_webui.utils.auth import get_admin_user, get_verified_user
|
|
|
from open_webui.utils.access_control import has_access, has_permission
|
|
|
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STATIC_DIR
|
|
|
|
|
|
+log = logging.getLogger(__name__)
|
|
|
+
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
@@ -93,6 +105,50 @@ async def export_models(user=Depends(get_admin_user)):
|
|
|
return Models.get_models()
|
|
|
|
|
|
|
|
|
+############################
|
|
|
+# ImportModels
|
|
|
+############################
|
|
|
+
|
|
|
+
|
|
|
+class ModelsImportForm(BaseModel):
|
|
|
+ models: list[dict]
|
|
|
+
|
|
|
+
|
|
|
+@router.post("/import", response_model=bool)
|
|
|
+async def import_models(
|
|
|
+ user: str = Depends(get_admin_user), form_data: ModelsImportForm = (...)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ data = form_data.models
|
|
|
+ if isinstance(data, list):
|
|
|
+ for model_data in data:
|
|
|
+ # Here, you can add logic to validate model_data if needed
|
|
|
+ model_id = model_data.get("id")
|
|
|
+ if model_id:
|
|
|
+ existing_model = Models.get_model_by_id(model_id)
|
|
|
+ if existing_model:
|
|
|
+ # Update existing model
|
|
|
+ model_data["meta"] = model_data.get("meta", {})
|
|
|
+ model_data["params"] = model_data.get("params", {})
|
|
|
+
|
|
|
+ updated_model = ModelForm(
|
|
|
+ **{**existing_model.model_dump(), **model_data}
|
|
|
+ )
|
|
|
+ Models.update_model_by_id(model_id, updated_model)
|
|
|
+ else:
|
|
|
+ # Insert new model
|
|
|
+ model_data["meta"] = model_data.get("meta", {})
|
|
|
+ model_data["params"] = model_data.get("params", {})
|
|
|
+ new_model = ModelForm(**model_data)
|
|
|
+ Models.insert_new_model(user_id=user.id, form_data=new_model)
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ raise HTTPException(status_code=400, detail="Invalid JSON format")
|
|
|
+ except Exception as e:
|
|
|
+ log.exception(e)
|
|
|
+ raise HTTPException(status_code=500, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
############################
|
|
|
# SyncModels
|
|
|
############################
|