"docs/zh_cn/datasets/waymo_det.md" did not exist on "786065a7cfbe977b6da1b3135cc408825f2382b6"
main.py 3.04 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
from bs4 import BeautifulSoup
import json
import markdown
4
5
import time

Timothy J. Baek's avatar
Timothy J. Baek committed
6

Timothy J. Baek's avatar
Timothy J. Baek committed
7
from fastapi import FastAPI, Request, Depends
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
from fastapi.staticfiles import StaticFiles
from fastapi import HTTPException
Timothy J. Baek's avatar
Timothy J. Baek committed
10
from fastapi.responses import JSONResponse
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
from fastapi.middleware.wsgi import WSGIMiddleware
from fastapi.middleware.cors import CORSMiddleware
13
from starlette.exceptions import HTTPException as StarletteHTTPException
Timothy J. Baek's avatar
Timothy J. Baek committed
14

Timothy J. Baek's avatar
Timothy J. Baek committed
15

Timothy J. Baek's avatar
Timothy J. Baek committed
16
17
from litellm.proxy.proxy_server import app as litellm_app

Timothy J. Baek's avatar
Timothy J. Baek committed
18
from apps.ollama.main import app as ollama_app
19
from apps.openai.main import app as openai_app
Timothy J. Baek's avatar
Timothy J. Baek committed
20
from apps.audio.main import app as audio_app
Timothy J. Baek's avatar
Timothy J. Baek committed
21
22
from apps.images.main import app as images_app
from apps.rag.main import app as rag_app
23
from apps.web.main import app as webui_app
Timothy J. Baek's avatar
Timothy J. Baek committed
24

Timothy J. Baek's avatar
Timothy J. Baek committed
25

26
from config import WEBUI_NAME, ENV, VERSION, CHANGELOG, FRONTEND_BUILD_DIR
Timothy J. Baek's avatar
Timothy J. Baek committed
27
from utils.utils import get_http_authorization_cred, get_current_user
Timothy J. Baek's avatar
Timothy J. Baek committed
28
29
30
31
32
33
34
35
36
37
38
39
40


class SPAStaticFiles(StaticFiles):
    async def get_response(self, path: str, scope):
        try:
            return await super().get_response(path, scope)
        except (HTTPException, StarletteHTTPException) as ex:
            if ex.status_code == 404:
                return await super().get_response("index.html", scope)
            else:
                raise ex


41
app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
Timothy J. Baek's avatar
Timothy J. Baek committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.middleware("http")
async def check_url(request: Request, call_next):
    start_time = int(time.time())
    response = await call_next(request)
    process_time = int(time.time()) - start_time
    response.headers["X-Process-Time"] = str(process_time)

    return response


Timothy J. Baek's avatar
Timothy J. Baek committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@litellm_app.middleware("http")
async def auth_middleware(request: Request, call_next):
    auth_header = request.headers.get("Authorization", "")

    if ENV != "dev":
        try:
            user = get_current_user(get_http_authorization_cred(auth_header))
            print(user)
        except Exception as e:
            return JSONResponse(status_code=400, content={"detail": str(e)})

    response = await call_next(request)
    return response


79
app.mount("/api/v1", webui_app)
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
app.mount("/litellm/api", litellm_app)

Timothy J. Baek's avatar
Timothy J. Baek committed
82

83
app.mount("/ollama/api", ollama_app)
84
app.mount("/openai/api", openai_app)
Timothy J. Baek's avatar
Timothy J. Baek committed
85

Timothy J. Baek's avatar
Timothy J. Baek committed
86
app.mount("/images/api/v1", images_app)
Timothy J. Baek's avatar
Timothy J. Baek committed
87
app.mount("/audio/api/v1", audio_app)
Timothy J. Baek's avatar
Timothy J. Baek committed
88
89
app.mount("/rag/api/v1", rag_app)

90

Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
@app.get("/api/config")
async def get_app_config():
Timothy J. Baek's avatar
Timothy J. Baek committed
93

Timothy J. Baek's avatar
Timothy J. Baek committed
94
95
    return {
        "status": True,
96
        "name": WEBUI_NAME,
Timothy J. Baek's avatar
Timothy J. Baek committed
97
        "version": VERSION,
Timothy J. Baek's avatar
Timothy J. Baek committed
98
99
100
101
102
103
        "images": images_app.state.ENABLED,
        "default_models": webui_app.state.DEFAULT_MODELS,
        "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
    }


Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
@app.get("/api/changelog")
async def get_app_changelog():
Timothy J. Baek's avatar
Timothy J. Baek committed
106
    return CHANGELOG
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108


109
110
111
app.mount("/static", StaticFiles(directory="static"), name="static")


112
113
app.mount(
    "/",
lucasew's avatar
lucasew committed
114
    SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
115
116
    name="spa-static-files",
)