open-webui/backend/apps/web/routers/utils.py

188 lines
5.4 KiB
Python
Raw Permalink Normal View History

2023-12-24 00:38:52 +01:00
from fastapi import APIRouter, UploadFile, File, BackgroundTasks
from fastapi import Depends, HTTPException, status
2024-03-02 09:33:20 +01:00
from starlette.responses import StreamingResponse, FileResponse
2023-12-24 00:38:52 +01:00
from pydantic import BaseModel
import requests
import os
2023-12-24 08:40:14 +01:00
import aiohttp
2023-12-24 00:38:52 +01:00
import json
2023-12-24 08:40:14 +01:00
2024-03-02 09:33:20 +01:00
from utils.utils import get_admin_user
from utils.misc import calculate_sha256, get_gravatar_url
2023-12-24 08:40:14 +01:00
2024-03-06 20:44:00 +01:00
from config import OLLAMA_BASE_URLS, DATA_DIR, UPLOAD_DIR
2024-01-09 22:25:42 +01:00
from constants import ERROR_MESSAGES
2023-12-24 00:38:52 +01:00
router = APIRouter()
class UploadBlobForm(BaseModel):
filename: str
2023-12-24 08:05:52 +01:00
from urllib.parse import urlparse
def parse_huggingface_url(hf_url):
2023-12-24 09:27:04 +01:00
try:
# Parse the URL
parsed_url = urlparse(hf_url)
2023-12-24 08:05:52 +01:00
2023-12-24 09:27:04 +01:00
# Get the path and split it into components
path_components = parsed_url.path.split("/")
2023-12-24 08:05:52 +01:00
2023-12-24 09:27:04 +01:00
# Extract the desired output
user_repo = "/".join(path_components[1:3])
model_file = path_components[-1]
2023-12-24 08:05:52 +01:00
2023-12-24 09:27:04 +01:00
return model_file
except ValueError:
return None
2023-12-24 08:05:52 +01:00
2024-01-09 22:25:42 +01:00
async def download_file_stream(url, file_path, file_name, chunk_size=1024 * 1024):
2023-12-24 08:05:52 +01:00
done = False
if os.path.exists(file_path):
current_size = os.path.getsize(file_path)
else:
current_size = 0
headers = {"Range": f"bytes={current_size}-"} if current_size > 0 else {}
2023-12-24 09:27:04 +01:00
timeout = aiohttp.ClientTimeout(total=600) # Set the timeout
2023-12-24 08:40:14 +01:00
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.get(url, headers=headers) as response:
2024-01-09 22:25:42 +01:00
total_size = int(response.headers.get("content-length", 0)) + current_size
2023-12-24 08:40:14 +01:00
with open(file_path, "ab+") as file:
async for data in response.content.iter_chunked(chunk_size):
current_size += len(data)
file.write(data)
done = current_size == total_size
progress = round((current_size / total_size) * 100, 2)
2023-12-24 09:27:04 +01:00
yield f'data: {{"progress": {progress}, "completed": {current_size}, "total": {total_size}}}\n\n'
2023-12-24 08:40:14 +01:00
if done:
file.seek(0)
hashed = calculate_sha256(file)
file.seek(0)
2023-12-24 08:05:52 +01:00
2024-03-06 20:44:00 +01:00
url = f"{OLLAMA_BASE_URLS[0]}/blobs/sha256:{hashed}"
2023-12-24 08:40:14 +01:00
response = requests.post(url, data=file)
2023-12-24 08:05:52 +01:00
2023-12-24 08:40:14 +01:00
if response.ok:
res = {
"done": done,
"blob": f"sha256:{hashed}",
2023-12-24 09:40:59 +01:00
"name": file_name,
2023-12-24 08:40:14 +01:00
}
os.remove(file_path)
2023-12-24 08:05:52 +01:00
2023-12-24 08:40:14 +01:00
yield f"data: {json.dumps(res)}\n\n"
else:
raise "Ollama: Could not create blob, Please try again."
2023-12-24 08:05:52 +01:00
@router.get("/download")
2024-01-09 22:25:42 +01:00
async def download(
url: str,
):
2023-12-24 09:27:04 +01:00
# url = "https://huggingface.co/TheBloke/stablelm-zephyr-3b-GGUF/resolve/main/stablelm-zephyr-3b.Q2_K.gguf"
2023-12-24 09:40:59 +01:00
file_name = parse_huggingface_url(url)
2023-12-24 08:05:52 +01:00
2023-12-24 09:40:59 +01:00
if file_name:
2024-01-22 10:47:07 +01:00
file_path = f"{UPLOAD_DIR}/{file_name}"
2023-12-24 08:05:52 +01:00
2023-12-24 09:27:04 +01:00
return StreamingResponse(
2023-12-24 09:40:59 +01:00
download_file_stream(url, file_path, file_name),
media_type="text/event-stream",
2023-12-24 09:27:04 +01:00
)
else:
return None
2023-12-24 08:05:52 +01:00
2023-12-24 00:38:52 +01:00
@router.post("/upload")
2024-01-09 22:25:42 +01:00
def upload(file: UploadFile = File(...)):
2024-01-22 10:47:07 +01:00
file_path = f"{UPLOAD_DIR}/{file.filename}"
2023-12-24 00:38:52 +01:00
2024-01-09 22:25:42 +01:00
# Save file in chunks
with open(file_path, "wb+") as f:
2024-01-09 22:25:42 +01:00
for chunk in file.file:
f.write(chunk)
2023-12-24 00:38:52 +01:00
2024-01-09 22:25:42 +01:00
def file_process_stream():
total_size = os.path.getsize(file_path)
2024-01-09 22:25:42 +01:00
chunk_size = 1024 * 1024
2023-12-24 00:38:52 +01:00
try:
2024-01-09 22:25:42 +01:00
with open(file_path, "rb") as f:
total = 0
done = False
while not done:
chunk = f.read(chunk_size)
2023-12-24 00:38:52 +01:00
if not chunk:
2024-01-09 22:25:42 +01:00
done = True
continue
2023-12-24 00:38:52 +01:00
total += len(chunk)
2023-12-24 09:27:04 +01:00
progress = round((total / total_size) * 100, 2)
2023-12-24 00:38:52 +01:00
res = {
2023-12-24 09:27:04 +01:00
"progress": progress,
2023-12-24 00:38:52 +01:00
"total": total_size,
2023-12-24 09:27:04 +01:00
"completed": total,
2023-12-24 00:38:52 +01:00
}
yield f"data: {json.dumps(res)}\n\n"
if done:
2023-12-24 08:05:52 +01:00
f.seek(0)
hashed = calculate_sha256(f)
f.seek(0)
2024-03-06 20:44:00 +01:00
url = f"{OLLAMA_BASE_URLS[0]}/blobs/sha256:{hashed}"
2023-12-24 08:05:52 +01:00
response = requests.post(url, data=f)
if response.ok:
res = {
"done": done,
"blob": f"sha256:{hashed}",
2023-12-24 09:27:04 +01:00
"name": file.filename,
2023-12-24 08:05:52 +01:00
}
os.remove(file_path)
yield f"data: {json.dumps(res)}\n\n"
else:
2024-01-09 22:25:42 +01:00
raise Exception(
"Ollama: Could not create blob, Please try again."
)
2023-12-24 00:38:52 +01:00
except Exception as e:
res = {"error": str(e)}
yield f"data: {json.dumps(res)}\n\n"
2024-01-09 22:25:42 +01:00
return StreamingResponse(file_process_stream(), media_type="text/event-stream")
@router.get("/gravatar")
async def get_gravatar(
email: str,
):
return get_gravatar_url(email)
2024-03-02 09:33:20 +01:00
@router.get("/db/download")
async def download_db(user=Depends(get_admin_user)):
return FileResponse(
f"{DATA_DIR}/webui.db",
media_type="application/octet-stream",
filename="webui.db",
)