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

150 lines
4.3 KiB
Python
Raw Normal View History

from pathlib import Path
import site
2024-04-06 08:54:59 +00:00
from fastapi import APIRouter, UploadFile, File, Response
2023-12-23 23:38:52 +00:00
from fastapi import Depends, HTTPException, status
2024-03-02 08:33:20 +00:00
from starlette.responses import StreamingResponse, FileResponse
2023-12-23 23:38:52 +00:00
from pydantic import BaseModel
2024-04-04 03:35:32 +00:00
2024-04-06 08:54:59 +00:00
from fpdf import FPDF
2024-04-04 03:35:32 +00:00
import markdown
2024-06-11 00:12:48 +00:00
import black
2023-12-24 07:40:14 +00:00
2024-03-02 08:33:20 +00:00
from utils.utils import get_admin_user
from utils.misc import calculate_sha256, get_gravatar_url
2023-12-24 07:40:14 +00:00
2024-04-25 01:24:17 +00:00
from config import OLLAMA_BASE_URLS, DATA_DIR, UPLOAD_DIR, ENABLE_ADMIN_EXPORT
2024-01-09 21:25:42 +00:00
from constants import ERROR_MESSAGES
2024-08-14 12:49:18 +00:00
2023-12-23 23:38:52 +00:00
router = APIRouter()
@router.get("/gravatar")
async def get_gravatar(
email: str,
):
return get_gravatar_url(email)
2024-03-02 08:33:20 +00:00
2024-06-11 00:12:48 +00:00
class CodeFormatRequest(BaseModel):
code: str
@router.post("/code/format")
async def format_code(request: CodeFormatRequest):
try:
formatted_code = black.format_str(request.code, mode=black.Mode())
return {"code": formatted_code}
except black.NothingChanged:
return {"code": request.code}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
2024-04-04 03:35:32 +00:00
class MarkdownForm(BaseModel):
md: str
@router.post("/markdown")
async def get_html_from_markdown(
form_data: MarkdownForm,
):
return {"html": markdown.markdown(form_data.md)}
2024-04-06 08:54:59 +00:00
class ChatForm(BaseModel):
title: str
2024-08-14 12:46:31 +00:00
messages: list[dict]
2024-04-06 08:54:59 +00:00
@router.post("/pdf")
async def download_chat_as_pdf(
form_data: ChatForm,
):
pdf = FPDF()
pdf.add_page()
# When running in docker, workdir is /app/backend, so fonts is in /app/backend/static/fonts
FONTS_DIR = Path("./static/fonts")
# Non Docker Installation
# When running using `pip install` the static directory is in the site packages.
if not FONTS_DIR.exists():
FONTS_DIR = Path(site.getsitepackages()[0]) / "static/fonts"
# When running using `pip install -e .` the static directory is in the site packages.
# This path only works if `open-webui serve` is run from the root of this project.
if not FONTS_DIR.exists():
FONTS_DIR = Path("./backend/static/fonts")
2024-04-06 08:54:59 +00:00
pdf.add_font("NotoSans", "", f"{FONTS_DIR}/NotoSans-Regular.ttf")
pdf.add_font("NotoSans", "b", f"{FONTS_DIR}/NotoSans-Bold.ttf")
pdf.add_font("NotoSans", "i", f"{FONTS_DIR}/NotoSans-Italic.ttf")
pdf.add_font("NotoSansKR", "", f"{FONTS_DIR}/NotoSansKR-Regular.ttf")
pdf.add_font("NotoSansJP", "", f"{FONTS_DIR}/NotoSansJP-Regular.ttf")
2024-08-19 15:50:42 +00:00
pdf.add_font("NotoSansSC", "", f"{FONTS_DIR}/NotoSansSC-Regular.ttf")
2024-04-06 08:54:59 +00:00
pdf.set_font("NotoSans", size=12)
2024-08-19 15:50:42 +00:00
pdf.set_fallback_fonts(["NotoSansKR", "NotoSansJP", "NotoSansSC"])
2024-04-06 08:54:59 +00:00
pdf.set_auto_page_break(auto=True, margin=15)
# Adjust the effective page width for multi_cell
effective_page_width = (
pdf.w - 2 * pdf.l_margin - 10
) # Subtracted an additional 10 for extra padding
# Add chat messages
for message in form_data.messages:
role = message["role"]
content = message["content"]
2024-04-06 09:04:28 +00:00
pdf.set_font("NotoSans", "B", size=14) # Bold for the role
2024-04-06 08:54:59 +00:00
pdf.multi_cell(effective_page_width, 10, f"{role.upper()}", 0, "L")
pdf.ln(1) # Extra space between messages
pdf.set_font("NotoSans", size=10) # Regular for content
2024-04-06 09:04:28 +00:00
pdf.multi_cell(effective_page_width, 6, content, 0, "L")
pdf.ln(1.5) # Extra space between messages
2024-04-06 08:54:59 +00:00
# Save the pdf with name .pdf
pdf_bytes = pdf.output()
return Response(
content=bytes(pdf_bytes),
media_type="application/pdf",
headers={"Content-Disposition": f"attachment;filename=chat.pdf"},
)
2024-03-02 08:33:20 +00:00
@router.get("/db/download")
async def download_db(user=Depends(get_admin_user)):
if not ENABLE_ADMIN_EXPORT:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
from apps.webui.internal.db import engine
if engine.name != "sqlite":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DB_NOT_SQLITE,
)
2024-03-02 08:33:20 +00:00
return FileResponse(
engine.url.database,
2024-03-02 08:33:20 +00:00
media_type="application/octet-stream",
filename="webui.db",
)
2024-06-01 23:31:39 +00:00
@router.get("/litellm/config")
async def download_litellm_config_yaml(user=Depends(get_admin_user)):
return FileResponse(
f"{DATA_DIR}/litellm/config.yaml",
media_type="application/octet-stream",
filename="config.yaml",
)