2024-06-11 00:12:48 +00:00
|
|
|
import black
|
2024-08-27 22:10:27 +00:00
|
|
|
import markdown
|
2024-10-12 16:12:31 +00:00
|
|
|
|
2024-12-10 08:54:13 +00:00
|
|
|
from open_webui.models.chats import ChatTitleMessagesForm
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.config import DATA_DIR, ENABLE_ADMIN_EXPORT
|
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
2024-08-27 22:10:27 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from starlette.responses import FileResponse
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.utils.misc import get_gravatar_url
|
2024-10-12 16:12:31 +00:00
|
|
|
from open_webui.utils.pdf_generator import PDFGenerator
|
2024-12-09 00:01:56 +00:00
|
|
|
from open_webui.utils.auth import get_admin_user
|
2023-12-23 23:38:52 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2024-01-27 05:38:33 +00:00
|
|
|
@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(
|
2024-10-12 16:12:31 +00:00
|
|
|
form_data: ChatTitleMessagesForm,
|
2024-04-06 08:54:59 +00:00
|
|
|
):
|
2024-10-13 07:05:28 +00:00
|
|
|
try:
|
|
|
|
pdf_bytes = PDFGenerator(form_data).generate_chat_pdf()
|
|
|
|
|
|
|
|
return Response(
|
|
|
|
content=pdf_bytes,
|
|
|
|
media_type="application/pdf",
|
|
|
|
headers={"Content-Disposition": "attachment;filename=chat.pdf"},
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
2024-04-06 08:54:59 +00:00
|
|
|
|
|
|
|
|
2024-03-02 08:33:20 +00:00
|
|
|
@router.get("/db/download")
|
|
|
|
async def download_db(user=Depends(get_admin_user)):
|
2024-04-22 18:55:46 +00:00
|
|
|
if not ENABLE_ADMIN_EXPORT:
|
2024-04-17 08:33:22 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
|
)
|
2024-12-10 08:54:13 +00:00
|
|
|
from open_webui.internal.db import engine
|
2024-06-18 13:03:31 +00:00
|
|
|
|
|
|
|
if engine.name != "sqlite":
|
2024-04-27 14:53:31 +00:00
|
|
|
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(
|
2024-06-18 13:03:31 +00:00
|
|
|
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",
|
|
|
|
)
|