main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from bs4 import BeautifulSoup
  2. import json
  3. import markdown
  4. import time
  5. import os
  6. import sys
  7. import requests
  8. from fastapi import FastAPI, Request, Depends, status
  9. from fastapi.staticfiles import StaticFiles
  10. from fastapi import HTTPException
  11. from fastapi.middleware.wsgi import WSGIMiddleware
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from starlette.exceptions import HTTPException as StarletteHTTPException
  14. from apps.ollama.main import app as ollama_app
  15. from apps.openai.main import app as openai_app
  16. from apps.litellm.main import app as litellm_app, startup as litellm_app_startup
  17. from apps.audio.main import app as audio_app
  18. from apps.images.main import app as images_app
  19. from apps.rag.main import app as rag_app
  20. from apps.web.main import app as webui_app
  21. from config import WEBUI_NAME, ENV, VERSION, CHANGELOG, FRONTEND_BUILD_DIR
  22. from constants import ERROR_MESSAGES
  23. class SPAStaticFiles(StaticFiles):
  24. async def get_response(self, path: str, scope):
  25. try:
  26. return await super().get_response(path, scope)
  27. except (HTTPException, StarletteHTTPException) as ex:
  28. if ex.status_code == 404:
  29. return await super().get_response("index.html", scope)
  30. else:
  31. raise ex
  32. app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
  33. origins = ["*"]
  34. app.add_middleware(
  35. CORSMiddleware,
  36. allow_origins=origins,
  37. allow_credentials=True,
  38. allow_methods=["*"],
  39. allow_headers=["*"],
  40. )
  41. @app.on_event("startup")
  42. async def on_startup():
  43. await litellm_app_startup()
  44. @app.middleware("http")
  45. async def check_url(request: Request, call_next):
  46. start_time = int(time.time())
  47. response = await call_next(request)
  48. process_time = int(time.time()) - start_time
  49. response.headers["X-Process-Time"] = str(process_time)
  50. return response
  51. app.mount("/api/v1", webui_app)
  52. app.mount("/litellm/api", litellm_app)
  53. app.mount("/ollama", ollama_app)
  54. app.mount("/openai/api", openai_app)
  55. app.mount("/images/api/v1", images_app)
  56. app.mount("/audio/api/v1", audio_app)
  57. app.mount("/rag/api/v1", rag_app)
  58. @app.get("/api/config")
  59. async def get_app_config():
  60. return {
  61. "status": True,
  62. "name": WEBUI_NAME,
  63. "version": VERSION,
  64. "images": images_app.state.ENABLED,
  65. "default_models": webui_app.state.DEFAULT_MODELS,
  66. "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
  67. }
  68. @app.get("/api/version")
  69. async def get_app_config():
  70. return {
  71. "version": VERSION,
  72. }
  73. @app.get("/api/changelog")
  74. async def get_app_changelog():
  75. return CHANGELOG
  76. @app.get("/api/version/updates")
  77. async def get_app_latest_release_version():
  78. try:
  79. response = requests.get(
  80. f"https://api.github.com/repos/open-webui/open-webui/releases/latest"
  81. )
  82. response.raise_for_status()
  83. latest_version = response.json()["tag_name"]
  84. return {"current": VERSION, "latest": latest_version[1:]}
  85. except Exception as e:
  86. raise HTTPException(
  87. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  88. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  89. )
  90. app.mount("/static", StaticFiles(directory="static"), name="static")
  91. app.mount("/cache", StaticFiles(directory="data/cache"), name="cache")
  92. app.mount(
  93. "/",
  94. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  95. name="spa-static-files",
  96. )