Skip to content

Commit

Permalink
fix(client): the online evaluation is accessible behind the proxy sub…
Browse files Browse the repository at this point in the history
…-path
  • Loading branch information
jialeicui committed Dec 15, 2023
1 parent 0273514 commit 4bc12eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
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/")
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

0 comments on commit 4bc12eb

Please sign in to comment.