Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): the online evaluation is accessible behind the proxy sub-path #3083

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions client/starwhale/api/_impl/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import json
import typing as t
import functools

Expand All @@ -14,7 +15,7 @@

from fastapi import FastAPI
from pydantic import Field
from starlette.responses import FileResponse
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles

from starwhale.utils import console
Expand Down Expand Up @@ -149,8 +150,21 @@ def spec() -> t.Union[ServiceSpec, None]:
methods=["POST"],
)

def index(opt: t.Any) -> FileResponse:
return FileResponse(os.path.join(STATIC_DIR_DEV, "client/index.html"))
def index(_: t.Any) -> HTMLResponse:
with open(os.path.join(STATIC_DIR_DEV, "client/index.html"), "r") as file:
content = file.read()
# https://github.com/star-whale/starwhale/pull/2996
root = os.getenv("SW_ONLINE_SERVING_ROOT_PATH", "")
opt = {
"root": root,
}
script = f"""<script type="text/javascript">
window.starwhaleConfig = {json.dumps(opt)}
</script>"""
content = content.replace("<!-- starwhale-client-placeholder -->", script)
# replace assets path
content = content.replace("/assets/", f"{root}/assets/")
anda-ren marked this conversation as resolved.
Show resolved Hide resolved
return HTMLResponse(content=content)

app.add_route("/", index, methods=["GET"])
app.mount("/", StaticFiles(directory=STATIC_DIR_DEV), name="assets")
Expand Down
7 changes: 5 additions & 2 deletions console/client/ServingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { IApiSchema, InferenceType, ISpecSchema } from './schemas/api'
import { useChatStore } from '@starwhale/ui/Serving/store/chat'
import ChatGroup from './components/ChatGroup'

// @ts-ignore
const rootPath = window?.starwhaleConfig?.root ?? ''

const fetchSpec = async () => {
const { data } = await axios.get<ISpecSchema>('/api/spec')
const { data } = await axios.get<ISpecSchema>(`${rootPath}/api/spec`)
return data
}

Expand All @@ -28,7 +31,7 @@ export default function ServingPage() {
job: { id: 'client' } as any,
type: apiSpec?.inference_type,
exposedLink: {
link: '',
link: `${rootPath}/`,
type: 'WEB_HANDLER',
name: 'llm_chat',
},
Expand Down
2 changes: 2 additions & 0 deletions console/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
href="https://starwhale-examples.oss-cn-beijing.aliyuncs.com/ui/iconfont/iconfont.css?v=20230928"
/>
<title>Starwhale Serving</title>
<!-- do not remove the following line -->
<!-- starwhale-client-placeholder -->
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
Loading