main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from bs4 import BeautifulSoup
  2. import json
  3. import markdown
  4. import time
  5. from fastapi import FastAPI, Request, Depends
  6. from fastapi.staticfiles import StaticFiles
  7. from fastapi import HTTPException
  8. from fastapi.responses import JSONResponse
  9. from fastapi.middleware.wsgi import WSGIMiddleware
  10. from fastapi.middleware.cors import CORSMiddleware
  11. from starlette.exceptions import HTTPException as StarletteHTTPException
  12. from litellm.proxy.proxy_server import app as litellm_app
  13. from apps.ollama.main import app as ollama_app
  14. from apps.openai.main import app as openai_app
  15. from apps.audio.main import app as audio_app
  16. from apps.images.main import app as images_app
  17. from apps.rag.main import app as rag_app
  18. from apps.web.main import app as webui_app
  19. from config import WEBUI_NAME, ENV, VERSION, CHANGELOG, FRONTEND_BUILD_DIR
  20. from utils.utils import get_http_authorization_cred, get_current_user
  21. class SPAStaticFiles(StaticFiles):
  22. async def get_response(self, path: str, scope):
  23. try:
  24. return await super().get_response(path, scope)
  25. except (HTTPException, StarletteHTTPException) as ex:
  26. if ex.status_code == 404:
  27. return await super().get_response("index.html", scope)
  28. else:
  29. raise ex
  30. app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
  31. origins = ["*"]
  32. app.add_middleware(
  33. CORSMiddleware,
  34. allow_origins=origins,
  35. allow_credentials=True,
  36. allow_methods=["*"],
  37. allow_headers=["*"],
  38. )
  39. @app.middleware("http")
  40. async def check_url(request: Request, call_next):
  41. start_time = int(time.time())
  42. response = await call_next(request)
  43. process_time = int(time.time()) - start_time
  44. response.headers["X-Process-Time"] = str(process_time)
  45. return response
  46. @litellm_app.middleware("http")
  47. async def auth_middleware(request: Request, call_next):
  48. auth_header = request.headers.get("Authorization", "")
  49. if ENV != "dev":
  50. try:
  51. user = get_current_user(get_http_authorization_cred(auth_header))
  52. print(user)
  53. except Exception as e:
  54. return JSONResponse(status_code=400, content={"detail": str(e)})
  55. response = await call_next(request)
  56. return response
  57. app.mount("/api/v1", webui_app)
  58. app.mount("/litellm/api", litellm_app)
  59. app.mount("/ollama/api", ollama_app)
  60. app.mount("/openai/api", openai_app)
  61. app.mount("/images/api/v1", images_app)
  62. app.mount("/audio/api/v1", audio_app)
  63. app.mount("/rag/api/v1", rag_app)
  64. @app.get("/api/config")
  65. async def get_app_config():
  66. return {
  67. "status": True,
  68. "name": WEBUI_NAME,
  69. "version": VERSION,
  70. "images": images_app.state.ENABLED,
  71. "default_models": webui_app.state.DEFAULT_MODELS,
  72. "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
  73. }
  74. @app.get("/api/changelog")
  75. async def get_app_changelog():
  76. return CHANGELOG
  77. app.mount("/static", StaticFiles(directory="static"), name="static")
  78. app.mount(
  79. "/",
  80. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  81. name="spa-static-files",
  82. )