Skip to content

Commit

Permalink
Update server.py
Browse files Browse the repository at this point in the history
接口服务允许跨域请求,接入其他服务,比如ollama或者其他大模型服务
  • Loading branch information
v3ucn authored Jul 11, 2024
1 parent c2f9254 commit a8b46cf
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions runtime/python/fastapi/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io,time
from fastapi import FastAPI, Response, File, UploadFile, Form
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware #引入 CORS中间件模块
from contextlib import asynccontextmanager
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append('{}/../../..'.format(ROOT_DIR))
Expand Down Expand Up @@ -39,6 +40,15 @@ async def lifespan(app: FastAPI):

app = FastAPI(lifespan=lifespan)

#设置允许访问的域名
origins = ["*"] #"*",即为所有,也可以改为允许的特定ip。
app.add_middleware(
CORSMiddleware,
allow_origins=origins, #设置允许的origins来源
allow_credentials=True,
allow_methods=["*"], # 设置允许跨域的http方法,比如 get、post、put等。
allow_headers=["*"]) #允许跨域的headers,可以用来鉴别来源等作用。

def buildResponse(output):
buffer = io.BytesIO()
torchaudio.save(buffer, output, 22050, format="wav")
Expand Down

0 comments on commit a8b46cf

Please sign in to comment.