utils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import site
  2. from pathlib import Path
  3. import black
  4. import markdown
  5. from open_webui.config import DATA_DIR, ENABLE_ADMIN_EXPORT
  6. from open_webui.env import FONTS_DIR
  7. from open_webui.constants import ERROR_MESSAGES
  8. from fastapi import APIRouter, Depends, HTTPException, Response, status
  9. from fpdf import FPDF
  10. from pydantic import BaseModel
  11. from starlette.responses import FileResponse
  12. from open_webui.utils.misc import get_gravatar_url
  13. from open_webui.utils.utils import get_admin_user
  14. router = APIRouter()
  15. @router.get("/gravatar")
  16. async def get_gravatar(
  17. email: str,
  18. ):
  19. return get_gravatar_url(email)
  20. class CodeFormatRequest(BaseModel):
  21. code: str
  22. @router.post("/code/format")
  23. async def format_code(request: CodeFormatRequest):
  24. try:
  25. formatted_code = black.format_str(request.code, mode=black.Mode())
  26. return {"code": formatted_code}
  27. except black.NothingChanged:
  28. return {"code": request.code}
  29. except Exception as e:
  30. raise HTTPException(status_code=400, detail=str(e))
  31. class MarkdownForm(BaseModel):
  32. md: str
  33. @router.post("/markdown")
  34. async def get_html_from_markdown(
  35. form_data: MarkdownForm,
  36. ):
  37. return {"html": markdown.markdown(form_data.md)}
  38. class ChatForm(BaseModel):
  39. title: str
  40. messages: list[dict]
  41. @router.post("/pdf")
  42. async def download_chat_as_pdf(
  43. form_data: ChatForm,
  44. ):
  45. global FONTS_DIR
  46. pdf = FPDF()
  47. pdf.add_page()
  48. # When running using `pip install` the static directory is in the site packages.
  49. if not FONTS_DIR.exists():
  50. FONTS_DIR = Path(site.getsitepackages()[0]) / "static/fonts"
  51. # When running using `pip install -e .` the static directory is in the site packages.
  52. # This path only works if `open-webui serve` is run from the root of this project.
  53. if not FONTS_DIR.exists():
  54. FONTS_DIR = Path("./backend/static/fonts")
  55. pdf.add_font("NotoSans", "", f"{FONTS_DIR}/NotoSans-Regular.ttf")
  56. pdf.add_font("NotoSans", "b", f"{FONTS_DIR}/NotoSans-Bold.ttf")
  57. pdf.add_font("NotoSans", "i", f"{FONTS_DIR}/NotoSans-Italic.ttf")
  58. pdf.add_font("NotoSansKR", "", f"{FONTS_DIR}/NotoSansKR-Regular.ttf")
  59. pdf.add_font("NotoSansJP", "", f"{FONTS_DIR}/NotoSansJP-Regular.ttf")
  60. pdf.add_font("NotoSansSC", "", f"{FONTS_DIR}/NotoSansSC-Regular.ttf")
  61. pdf.set_font("NotoSans", size=12)
  62. pdf.set_fallback_fonts(["NotoSansKR", "NotoSansJP", "NotoSansSC"])
  63. pdf.set_auto_page_break(auto=True, margin=15)
  64. # Adjust the effective page width for multi_cell
  65. effective_page_width = (
  66. pdf.w - 2 * pdf.l_margin - 10
  67. ) # Subtracted an additional 10 for extra padding
  68. # Add chat messages
  69. for message in form_data.messages:
  70. role = message["role"]
  71. content = message["content"]
  72. pdf.set_font("NotoSans", "B", size=14) # Bold for the role
  73. pdf.multi_cell(effective_page_width, 10, f"{role.upper()}", 0, "L")
  74. pdf.ln(1) # Extra space between messages
  75. pdf.set_font("NotoSans", size=10) # Regular for content
  76. pdf.multi_cell(effective_page_width, 6, content, 0, "L")
  77. pdf.ln(1.5) # Extra space between messages
  78. # Save the pdf with name .pdf
  79. pdf_bytes = pdf.output()
  80. return Response(
  81. content=bytes(pdf_bytes),
  82. media_type="application/pdf",
  83. headers={"Content-Disposition": "attachment;filename=chat.pdf"},
  84. )
  85. @router.get("/db/download")
  86. async def download_db(user=Depends(get_admin_user)):
  87. if not ENABLE_ADMIN_EXPORT:
  88. raise HTTPException(
  89. status_code=status.HTTP_401_UNAUTHORIZED,
  90. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  91. )
  92. from open_webui.apps.webui.internal.db import engine
  93. if engine.name != "sqlite":
  94. raise HTTPException(
  95. status_code=status.HTTP_400_BAD_REQUEST,
  96. detail=ERROR_MESSAGES.DB_NOT_SQLITE,
  97. )
  98. return FileResponse(
  99. engine.url.database,
  100. media_type="application/octet-stream",
  101. filename="webui.db",
  102. )
  103. @router.get("/litellm/config")
  104. async def download_litellm_config_yaml(user=Depends(get_admin_user)):
  105. return FileResponse(
  106. f"{DATA_DIR}/litellm/config.yaml",
  107. media_type="application/octet-stream",
  108. filename="config.yaml",
  109. )