forked from open-webui/open-webui
feat: gguf upload
This commit is contained in:
parent
6ea9f6e198
commit
e539cf5c28
6 changed files with 457 additions and 96 deletions
|
@ -1,7 +1,7 @@
|
|||
from fastapi import FastAPI, Request, Depends, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from apps.web.routers import auths, users
|
||||
from apps.web.routers import auths, users, utils
|
||||
from config import WEBUI_VERSION, WEBUI_AUTH
|
||||
|
||||
app = FastAPI()
|
||||
|
@ -19,6 +19,7 @@ app.add_middleware(
|
|||
|
||||
app.include_router(auths.router, prefix="/auths", tags=["auths"])
|
||||
app.include_router(users.router, prefix="/users", tags=["users"])
|
||||
app.include_router(utils.router, prefix="/utils", tags=["utils"])
|
||||
|
||||
|
||||
@app.get("/")
|
||||
|
|
78
backend/apps/web/routers/utils.py
Normal file
78
backend/apps/web/routers/utils.py
Normal file
|
@ -0,0 +1,78 @@
|
|||
from fastapi import APIRouter, UploadFile, File, BackgroundTasks
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from starlette.responses import StreamingResponse
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from utils.misc import calculate_sha256
|
||||
import requests
|
||||
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
import json
|
||||
from config import OLLAMA_API_BASE_URL
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class UploadBlobForm(BaseModel):
|
||||
filename: str
|
||||
|
||||
|
||||
@router.post("/upload")
|
||||
async def upload(file: UploadFile = File(...)):
|
||||
os.makedirs("./uploads", exist_ok=True)
|
||||
file_path = os.path.join("./uploads", file.filename)
|
||||
|
||||
def file_write_stream():
|
||||
total = 0
|
||||
total_size = file.size
|
||||
chunk_size = 1024 * 1024
|
||||
|
||||
done = False
|
||||
try:
|
||||
with open(file_path, "wb") as f:
|
||||
while True:
|
||||
chunk = file.file.read(chunk_size)
|
||||
if not chunk:
|
||||
break
|
||||
f.write(chunk)
|
||||
total += len(chunk)
|
||||
done = total_size == total
|
||||
|
||||
res = {
|
||||
"total": total_size,
|
||||
"uploaded": total,
|
||||
}
|
||||
|
||||
yield f"data: {json.dumps(res)}\n\n"
|
||||
|
||||
if done:
|
||||
with open(file_path, "rb") as f:
|
||||
hashed = calculate_sha256(f)
|
||||
|
||||
f.seek(0)
|
||||
file_data = f.read()
|
||||
|
||||
url = f"{OLLAMA_API_BASE_URL}/blobs/sha256:{hashed}"
|
||||
|
||||
response = requests.post(url, data=file_data)
|
||||
|
||||
if response.ok:
|
||||
res = {
|
||||
"done": done,
|
||||
"blob": f"sha256:{hashed}",
|
||||
}
|
||||
os.remove(file_path)
|
||||
|
||||
yield f"data: {json.dumps(res)}\n\n"
|
||||
else:
|
||||
raise "Ollama: Could not create blob, Please try again."
|
||||
|
||||
except Exception as e:
|
||||
res = {"error": str(e)}
|
||||
yield f"data: {json.dumps(res)}\n\n"
|
||||
|
||||
return StreamingResponse(file_write_stream(), media_type="text/event-stream")
|
Loading…
Add table
Add a link
Reference in a new issue